| 1 | import { providers } from "../pages/AddNewModel/configs/providers"; |
| 2 | |
| 3 | export interface ErrorAnalysis { |
| 4 | parsedError: string; |
| 5 | statusCode?: number; |
| 6 | message?: string; |
| 7 | modelTitle: string; |
| 8 | providerName: string; |
| 9 | apiKeyUrl?: string; |
| 10 | } |
| 11 | |
| 12 | function parseErrorMessage(fullErrMsg: string): string { |
| 13 | if ( |
| 14 | !fullErrMsg || |
| 15 | typeof fullErrMsg !== "string" || |
| 16 | !fullErrMsg.includes("\n\n") |
| 17 | ) { |
| 18 | return fullErrMsg; |
| 19 | } |
| 20 | |
| 21 | const msg = fullErrMsg.split("\n\n").slice(1).join("\n\n"); |
| 22 | try { |
| 23 | const parsed = JSON.parse(msg); |
| 24 | if (parsed.error !== undefined && parsed.error !== null) { |
| 25 | return JSON.stringify(parsed.error); |
| 26 | } |
| 27 | if (parsed.message !== undefined && parsed.message !== null) { |
| 28 | return JSON.stringify(parsed.message); |
| 29 | } |
| 30 | return msg; |
| 31 | } catch (e) { |
| 32 | return msg; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | export function analyzeError( |
| 37 | error: unknown, |
| 38 | selectedModel: any, |
| 39 | ): ErrorAnalysis { |
| 40 | const errorMessage = (error as any)?.message; |
| 41 | const parsedError = parseErrorMessage( |
| 42 | typeof errorMessage === "string" ? errorMessage : "", |
| 43 | ); |
| 44 | |
| 45 | // Collect model information to display useful error info |
| 46 | let modelTitle = "Chat model"; |
| 47 | let providerName = "the model provider"; |
| 48 | let apiKeyUrl: string | undefined = undefined; |
| 49 | |
| 50 | if (selectedModel) { |
| 51 | modelTitle = selectedModel.title; |
| 52 | providerName = selectedModel.provider; |
| 53 | |
| 54 | // If there's a matching provider from add model form provider info |
| 55 | // We can get more info |
| 56 | const foundProvider = Object.values(providers).find( |
| 57 | (p) => p?.provider === selectedModel.provider, |
| 58 | ); |
| 59 | if (foundProvider) { |
| 60 | providerName = foundProvider.title; |
| 61 | if (foundProvider.apiKeyUrl) { |
| 62 | apiKeyUrl = foundProvider.apiKeyUrl; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | let message: undefined | string = undefined; |
| 68 | let statusCode: undefined | number = undefined; |
| 69 | |
| 70 | // Attempt to get error message and status code from error |
| 71 | if ( |
| 72 | error && |
| 73 | (error instanceof Error || typeof error === "object") && |
| 74 | "message" in error && |
| 75 | typeof error["message"] === "string" |
| 76 | ) { |
| 77 | message = error["message"]; |
| 78 | const parts = message?.split(" ") ?? []; |
| 79 | |
| 80 | // Handle single word case (like "404") |
| 81 | if (parts.length === 1) { |
| 82 | const trimmed = parts[0].trim(); |
| 83 | if (trimmed !== "") { |
| 84 | const code = Number(trimmed); |
| 85 | if (!Number.isNaN(code)) { |
| 86 | statusCode = code; |
| 87 | } |
| 88 | } |
| 89 | } else if (parts.length > 1) { |
| 90 | const status = parts[0] === "HTTP" ? parts[1] : parts[0]; |
| 91 | if (status) { |
| 92 | const code = Number(status); |
| 93 | if (!Number.isNaN(code)) { |
| 94 | statusCode = code; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return { |
| 101 | parsedError, |
| 102 | statusCode, |
| 103 | message, |
| 104 | modelTitle, |
| 105 | providerName, |
| 106 | apiKeyUrl, |
| 107 | }; |
| 108 | } |