(error: unknown)
| 963 | * Returns a standardized error type string suitable for Datadog tagging. |
| 964 | */ |
| 965 | export function classifyAPIError(error: unknown): string { |
| 966 | // Aborted requests |
| 967 | if (error instanceof Error && error.message === 'Request was aborted.') { |
| 968 | return 'aborted' |
| 969 | } |
| 970 | |
| 971 | // Timeout errors |
| 972 | if ( |
| 973 | error instanceof APIConnectionTimeoutError || |
| 974 | (error instanceof APIConnectionError && |
| 975 | error.message.toLowerCase().includes('timeout')) |
| 976 | ) { |
| 977 | return 'api_timeout' |
| 978 | } |
| 979 | |
| 980 | // Check for repeated 529 errors |
| 981 | if ( |
| 982 | error instanceof Error && |
| 983 | error.message.includes(REPEATED_529_ERROR_MESSAGE) |
| 984 | ) { |
| 985 | return 'repeated_529' |
| 986 | } |
| 987 | |
| 988 | // Check for emergency capacity off switch |
| 989 | if ( |
| 990 | error instanceof Error && |
| 991 | error.message.includes(CUSTOM_OFF_SWITCH_MESSAGE) |
| 992 | ) { |
| 993 | return 'capacity_off_switch' |
| 994 | } |
| 995 | |
| 996 | // Rate limiting |
| 997 | if (error instanceof APIError && error.status === 429) { |
| 998 | return 'rate_limit' |
| 999 | } |
| 1000 | |
| 1001 | // Server overload (529) |
| 1002 | if ( |
| 1003 | error instanceof APIError && |
| 1004 | (error.status === 529 || |
| 1005 | error.message?.includes('"type":"overloaded_error"')) |
| 1006 | ) { |
| 1007 | return 'server_overload' |
| 1008 | } |
| 1009 | |
| 1010 | // Prompt/content size errors |
| 1011 | if ( |
| 1012 | error instanceof Error && |
| 1013 | error.message |
| 1014 | .toLowerCase() |
| 1015 | .includes(PROMPT_TOO_LONG_ERROR_MESSAGE.toLowerCase()) |
| 1016 | ) { |
| 1017 | return 'prompt_too_long' |
| 1018 | } |
| 1019 | |
| 1020 | // PDF errors |
| 1021 | if ( |
| 1022 | error instanceof Error && |
no test coverage detected