error handling fixes
This commit is contained in:
parent
81294f5721
commit
61d5cc75b6
1 changed files with 67 additions and 50 deletions
|
@ -69,6 +69,35 @@ export async function preChecks() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isAxiosError(error: unknown): error is { response?: { data?: { error?: string }, status?: number }, request?: unknown, message?: string } {
|
||||||
|
return typeof error === 'object' && error !== null && (
|
||||||
|
'response' in error || 'request' in error || 'message' in error
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function extractAxiosErrorMessage(error: unknown): string {
|
||||||
|
if (isAxiosError(error)) {
|
||||||
|
const err = error as Record<string, unknown>;
|
||||||
|
if (err.response && typeof err.response === 'object') {
|
||||||
|
const resp = err.response as Record<string, unknown>;
|
||||||
|
if (resp.data && typeof resp.data === 'object' && 'error' in resp.data) {
|
||||||
|
return String((resp.data as Record<string, unknown>).error);
|
||||||
|
}
|
||||||
|
if ('status' in resp && 'statusText' in resp) {
|
||||||
|
return `HTTP ${resp.status}: ${resp.statusText}`;
|
||||||
|
}
|
||||||
|
return JSON.stringify(resp.data ?? resp);
|
||||||
|
}
|
||||||
|
if (err.request) {
|
||||||
|
return 'No response received from server.';
|
||||||
|
}
|
||||||
|
if (typeof err.message === 'string') {
|
||||||
|
return err.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 'An unexpected error occurred.';
|
||||||
|
}
|
||||||
|
|
||||||
async function getResponse(prompt: string, ctx: TextContext, replyGenerating: Message, model: string) {
|
async function getResponse(prompt: string, ctx: TextContext, replyGenerating: Message, model: string) {
|
||||||
const Strings = getStrings(languageCode(ctx))
|
const Strings = getStrings(languageCode(ctx))
|
||||||
|
|
||||||
|
@ -153,27 +182,23 @@ async function getResponse(prompt: string, ctx: TextContext, replyGenerating: Me
|
||||||
success: true,
|
success: true,
|
||||||
response: fullResponse,
|
response: fullResponse,
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: unknown) {
|
||||||
let shouldPullModel = false
|
const errorMsg = extractAxiosErrorMessage(error)
|
||||||
if (error.response) {
|
console.error("[✨ AI | !] Error:", errorMsg)
|
||||||
const errData = error.response.data?.error
|
|
||||||
const errStatus = error.response.status
|
|
||||||
if (errData && (errData.includes(`model '${model}' not found`) || errStatus === 404)) {
|
|
||||||
shouldPullModel = true
|
|
||||||
} else {
|
|
||||||
console.error("[✨ AI | !] Error zone 1:", errData)
|
|
||||||
return { success: false, error: errData }
|
|
||||||
}
|
|
||||||
} else if (error.request) {
|
|
||||||
console.error("[✨ AI | !] No response received:", error.request)
|
|
||||||
return { success: false, error: "No response received from server" }
|
|
||||||
} else {
|
|
||||||
console.error("[✨ AI | !] Error zone 3:", error.message)
|
|
||||||
return { success: false, error: error.message }
|
|
||||||
}
|
|
||||||
|
|
||||||
if (shouldPullModel) {
|
// model not found or 404
|
||||||
ctx.telegram.editMessageText(ctx.chat.id, replyGenerating.message_id, undefined, `🔄 Pulling ${model} from ollama...\n\nThis may take a few minutes...`)
|
if (isAxiosError(error) && error.response && typeof error.response === 'object') {
|
||||||
|
const resp = error.response as Record<string, unknown>;
|
||||||
|
const errData = resp.data && typeof resp.data === 'object' && 'error' in resp.data ? (resp.data as Record<string, unknown>).error : undefined;
|
||||||
|
const errStatus = 'status' in resp ? resp.status : undefined;
|
||||||
|
if ((typeof errData === 'string' && errData.includes(`model '${model}' not found`)) || errStatus === 404) {
|
||||||
|
ctx.telegram.editMessageText(
|
||||||
|
ctx.chat.id,
|
||||||
|
replyGenerating.message_id,
|
||||||
|
undefined,
|
||||||
|
`🔄 *Pulling ${model} from Ollama...*\n\nThis may take a few minutes...`,
|
||||||
|
{ parse_mode: 'Markdown' }
|
||||||
|
)
|
||||||
console.log(`[✨ AI | i] Pulling ${model} from ollama...`)
|
console.log(`[✨ AI | i] Pulling ${model} from ollama...`)
|
||||||
try {
|
try {
|
||||||
await axios.post(
|
await axios.post(
|
||||||
|
@ -184,25 +209,12 @@ async function getResponse(prompt: string, ctx: TextContext, replyGenerating: Me
|
||||||
timeout: process.env.ollamaApiTimeout || 10000,
|
timeout: process.env.ollamaApiTimeout || 10000,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
} catch (e: any) {
|
} catch (e: unknown) {
|
||||||
if (e.response) {
|
const pullMsg = extractAxiosErrorMessage(e)
|
||||||
console.error("[✨ AI | !] Something went wrong:", e.response.data?.error)
|
console.error("[✨ AI | !] Pull error:", pullMsg)
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: `❌ Something went wrong while pulling ${model}, please try your command again!`,
|
error: `❌ Something went wrong while pulling ${model}: ${pullMsg}`,
|
||||||
}
|
|
||||||
} else if (e.request) {
|
|
||||||
console.error("[✨ AI | !] No response received while pulling:", e.request)
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
error: `❌ No response received while pulling ${model}, please try again!`,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.error("[✨ AI | !] Error while pulling:", e.message)
|
|
||||||
return {
|
|
||||||
success: false,
|
|
||||||
error: `❌ Error while pulling ${model}: ${e.message}`,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(`[✨ AI | i] ${model} pulled successfully`)
|
console.log(`[✨ AI | i] ${model} pulled successfully`)
|
||||||
|
@ -212,6 +224,11 @@ async function getResponse(prompt: string, ctx: TextContext, replyGenerating: Me
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: errorMsg,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default (bot: Telegraf<Context>) => {
|
export default (bot: Telegraf<Context>) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue