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