(error: unknown)
| 1018 | * Returns a standardized error type string suitable for Datadog tagging. |
| 1019 | */ |
| 1020 | export function classifyAPIError(error: unknown): string { |
| 1021 | // Aborted requests |
| 1022 | if (error instanceof Error && error.message === 'Request was aborted.') { |
| 1023 | return 'aborted' |
| 1024 | } |
| 1025 | |
| 1026 | // Timeout errors |
| 1027 | if ( |
| 1028 | error instanceof APIConnectionTimeoutError || |
| 1029 | (error instanceof APIConnectionError && |
| 1030 | error.message.toLowerCase().includes('timeout')) |
| 1031 | ) { |
| 1032 | return 'api_timeout' |
| 1033 | } |
| 1034 | |
| 1035 | // Check for repeated 529 errors |
| 1036 | if ( |
| 1037 | error instanceof Error && |
| 1038 | error.message.includes(REPEATED_529_ERROR_MESSAGE) |
| 1039 | ) { |
| 1040 | return 'repeated_529' |
| 1041 | } |
| 1042 | |
| 1043 | // Check for emergency capacity off switch |
| 1044 | if ( |
| 1045 | error instanceof Error && |
| 1046 | error.message.includes(CUSTOM_OFF_SWITCH_MESSAGE) |
| 1047 | ) { |
| 1048 | return 'capacity_off_switch' |
| 1049 | } |
| 1050 | |
| 1051 | // Rate limiting |
| 1052 | if (error instanceof APIError && error.status === 429) { |
| 1053 | return 'rate_limit' |
| 1054 | } |
| 1055 | |
| 1056 | // Server overload (529) |
| 1057 | if ( |
| 1058 | error instanceof APIError && |
| 1059 | (error.status === 529 || |
| 1060 | error.message?.includes('"type":"overloaded_error"')) |
| 1061 | ) { |
| 1062 | return 'server_overload' |
| 1063 | } |
| 1064 | |
| 1065 | // Prompt/content size errors |
| 1066 | if ( |
| 1067 | error instanceof Error && |
| 1068 | error.message |
| 1069 | .toLowerCase() |
| 1070 | .includes(PROMPT_TOO_LONG_ERROR_MESSAGE.toLowerCase()) |
| 1071 | ) { |
| 1072 | return 'prompt_too_long' |
| 1073 | } |
| 1074 | |
| 1075 | // PDF errors |
| 1076 | if ( |
| 1077 | error instanceof Error && |
no test coverage detected