|
| 1 | +/** |
| 2 | + * Error Extractor Registry |
| 3 | + * |
| 4 | + * This module provides a clean, config-based approach to extracting error messages |
| 5 | + * from diverse API error response formats. |
| 6 | + * |
| 7 | + * ## Adding a new extractor |
| 8 | + * |
| 9 | + * 1. Add entry to ERROR_EXTRACTORS array below: |
| 10 | + * ```typescript |
| 11 | + * { |
| 12 | + * id: 'stripe-errors', |
| 13 | + * description: 'Stripe API error format', |
| 14 | + * examples: ['Stripe API'], |
| 15 | + * extract: (errorInfo) => errorInfo?.data?.error?.message |
| 16 | + * } |
| 17 | + * ``` |
| 18 | + * |
| 19 | + * 2. Add the ID to ErrorExtractorId constant at the bottom of this file |
| 20 | + */ |
| 21 | + |
| 22 | +export interface ErrorInfo { |
| 23 | + status?: number |
| 24 | + statusText?: string |
| 25 | + data?: any |
| 26 | +} |
| 27 | + |
| 28 | +export type ErrorExtractor = (errorInfo?: ErrorInfo) => string | null | undefined |
| 29 | + |
| 30 | +export interface ErrorExtractorConfig { |
| 31 | + /** Unique identifier for this extractor */ |
| 32 | + id: string |
| 33 | + /** Human-readable description of what API/pattern this handles */ |
| 34 | + description: string |
| 35 | + /** Example APIs that use this pattern */ |
| 36 | + examples?: string[] |
| 37 | + /** The extraction function */ |
| 38 | + extract: ErrorExtractor |
| 39 | +} |
| 40 | + |
| 41 | +const ERROR_EXTRACTORS: ErrorExtractorConfig[] = [ |
| 42 | + { |
| 43 | + id: 'graphql-errors', |
| 44 | + description: 'GraphQL errors array with message field', |
| 45 | + examples: ['Linear API', 'GitHub GraphQL'], |
| 46 | + extract: (errorInfo) => errorInfo?.data?.errors?.[0]?.message, |
| 47 | + }, |
| 48 | + { |
| 49 | + id: 'twitter-errors', |
| 50 | + description: 'X/Twitter API error detail field', |
| 51 | + examples: ['Twitter/X API'], |
| 52 | + extract: (errorInfo) => errorInfo?.data?.errors?.[0]?.detail, |
| 53 | + }, |
| 54 | + { |
| 55 | + id: 'details-array', |
| 56 | + description: 'Generic details array with message', |
| 57 | + examples: ['Various REST APIs'], |
| 58 | + extract: (errorInfo) => errorInfo?.data?.details?.[0]?.message, |
| 59 | + }, |
| 60 | + { |
| 61 | + id: 'hunter-errors', |
| 62 | + description: 'Hunter API error details', |
| 63 | + examples: ['Hunter.io API'], |
| 64 | + extract: (errorInfo) => errorInfo?.data?.errors?.[0]?.details, |
| 65 | + }, |
| 66 | + { |
| 67 | + id: 'errors-array-string', |
| 68 | + description: 'Errors array containing strings or objects with messages', |
| 69 | + examples: ['Various APIs with error arrays'], |
| 70 | + extract: (errorInfo) => { |
| 71 | + if (!Array.isArray(errorInfo?.data?.errors)) return undefined |
| 72 | + const firstError = errorInfo.data.errors[0] |
| 73 | + if (typeof firstError === 'string') return firstError |
| 74 | + return firstError?.message |
| 75 | + }, |
| 76 | + }, |
| 77 | + { |
| 78 | + id: 'telegram-description', |
| 79 | + description: 'Telegram Bot API description field', |
| 80 | + examples: ['Telegram Bot API'], |
| 81 | + extract: (errorInfo) => errorInfo?.data?.description, |
| 82 | + }, |
| 83 | + { |
| 84 | + id: 'standard-message', |
| 85 | + description: 'Standard message field in error response', |
| 86 | + examples: ['Notion', 'Discord', 'GitHub', 'Twilio', 'Slack'], |
| 87 | + extract: (errorInfo) => errorInfo?.data?.message, |
| 88 | + }, |
| 89 | + { |
| 90 | + id: 'soap-fault', |
| 91 | + description: 'SOAP/XML fault string patterns', |
| 92 | + examples: ['SOAP APIs', 'Legacy XML services'], |
| 93 | + extract: (errorInfo) => errorInfo?.data?.fault?.faultstring || errorInfo?.data?.faultstring, |
| 94 | + }, |
| 95 | + { |
| 96 | + id: 'oauth-error-description', |
| 97 | + description: 'OAuth2 error_description field', |
| 98 | + examples: ['Microsoft OAuth', 'Google OAuth', 'OAuth2 providers'], |
| 99 | + extract: (errorInfo) => errorInfo?.data?.error_description, |
| 100 | + }, |
| 101 | + { |
| 102 | + id: 'nested-error-object', |
| 103 | + description: 'Error field containing nested object or string', |
| 104 | + examples: ['Airtable', 'Google APIs'], |
| 105 | + extract: (errorInfo) => { |
| 106 | + const error = errorInfo?.data?.error |
| 107 | + if (!error) return undefined |
| 108 | + if (typeof error === 'string') return error |
| 109 | + if (typeof error === 'object') { |
| 110 | + return error.message || JSON.stringify(error) |
| 111 | + } |
| 112 | + return undefined |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + id: 'http-status-text', |
| 117 | + description: 'HTTP response status text fallback', |
| 118 | + examples: ['Generic HTTP errors'], |
| 119 | + extract: (errorInfo) => errorInfo?.statusText, |
| 120 | + }, |
| 121 | +] |
| 122 | + |
| 123 | +const EXTRACTOR_MAP = new Map<string, ErrorExtractorConfig>(ERROR_EXTRACTORS.map((e) => [e.id, e])) |
| 124 | + |
| 125 | +export function extractErrorMessageWithId( |
| 126 | + errorInfo: ErrorInfo | undefined, |
| 127 | + extractorId: string |
| 128 | +): string { |
| 129 | + const extractor = EXTRACTOR_MAP.get(extractorId) |
| 130 | + |
| 131 | + if (!extractor) { |
| 132 | + return `Request failed with status ${errorInfo?.status || 'unknown'}` |
| 133 | + } |
| 134 | + |
| 135 | + try { |
| 136 | + const message = extractor.extract(errorInfo) |
| 137 | + if (message && message.trim()) { |
| 138 | + return message |
| 139 | + } |
| 140 | + } catch (error) {} |
| 141 | + |
| 142 | + return `Request failed with status ${errorInfo?.status || 'unknown'}` |
| 143 | +} |
| 144 | + |
| 145 | +export function extractErrorMessage(errorInfo?: ErrorInfo, extractorId?: string): string { |
| 146 | + if (extractorId) { |
| 147 | + return extractErrorMessageWithId(errorInfo, extractorId) |
| 148 | + } |
| 149 | + |
| 150 | + // Backwards compatibility |
| 151 | + for (const extractor of ERROR_EXTRACTORS) { |
| 152 | + try { |
| 153 | + const message = extractor.extract(errorInfo) |
| 154 | + if (message && message.trim()) { |
| 155 | + return message |
| 156 | + } |
| 157 | + } catch (error) {} |
| 158 | + } |
| 159 | + |
| 160 | + return `Request failed with status ${errorInfo?.status || 'unknown'}` |
| 161 | +} |
| 162 | + |
| 163 | +export const ErrorExtractorId = { |
| 164 | + GRAPHQL_ERRORS: 'graphql-errors', |
| 165 | + TWITTER_ERRORS: 'twitter-errors', |
| 166 | + DETAILS_ARRAY: 'details-array', |
| 167 | + HUNTER_ERRORS: 'hunter-errors', |
| 168 | + ERRORS_ARRAY_STRING: 'errors-array-string', |
| 169 | + TELEGRAM_DESCRIPTION: 'telegram-description', |
| 170 | + STANDARD_MESSAGE: 'standard-message', |
| 171 | + SOAP_FAULT: 'soap-fault', |
| 172 | + OAUTH_ERROR_DESCRIPTION: 'oauth-error-description', |
| 173 | + NESTED_ERROR_OBJECT: 'nested-error-object', |
| 174 | + HTTP_STATUS_TEXT: 'http-status-text', |
| 175 | +} as const |
0 commit comments