(error: any)
| 2 | * Safely formats an error object into a readable string |
| 3 | */ |
| 4 | export function formatError(error: any): string { |
| 5 | if (error instanceof Error) { |
| 6 | return error.message; |
| 7 | } |
| 8 | |
| 9 | if (typeof error === "string") { |
| 10 | return error; |
| 11 | } |
| 12 | |
| 13 | if (error && typeof error === "object") { |
| 14 | // Try to extract common error properties |
| 15 | if (error.message) { |
| 16 | return error.message; |
| 17 | } |
| 18 | if (error.error) { |
| 19 | return formatError(error.error); |
| 20 | } |
| 21 | if (error.details) { |
| 22 | return formatError(error.details); |
| 23 | } |
| 24 | if (error.description) { |
| 25 | return error.description; |
| 26 | } |
| 27 | |
| 28 | // For API errors, try to extract meaningful info |
| 29 | if (error.status && error.error && error.error.message) { |
| 30 | return `HTTP ${error.status}: ${error.error.message}`; |
| 31 | } |
| 32 | |
| 33 | // For network errors |
| 34 | if (error.code && error.syscall) { |
| 35 | return `Network error: ${error.code} in ${error.syscall}`; |
| 36 | } |
| 37 | |
| 38 | // For errors with an errors array |
| 39 | if (error.errors && Array.isArray(error.errors)) { |
| 40 | return error.errors.join(", "); |
| 41 | } |
| 42 | |
| 43 | // Try to JSON stringify if possible |
| 44 | try { |
| 45 | return JSON.stringify(error); |
| 46 | } catch { |
| 47 | // If JSON.stringify fails, return a generic message |
| 48 | return `An error occurred: ${Object.prototype.toString.call(error)}`; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return String(error); |
| 53 | } |
| 54 | |
| 55 | // Anthropic errors are stringfied JSON objects, format them to be more user friendly |
| 56 | export function formatAnthropicError(error: any): string { |
no outgoing calls
no test coverage detected