(error: APIError)
| 198 | } |
| 199 | |
| 200 | export function formatAPIError(error: APIError): string { |
| 201 | // Extract connection error details from the cause chain |
| 202 | const connectionDetails = extractConnectionErrorDetails(error) |
| 203 | |
| 204 | if (connectionDetails) { |
| 205 | const { code, isSSLError } = connectionDetails |
| 206 | |
| 207 | // Handle timeout errors |
| 208 | if (code === 'ETIMEDOUT') { |
| 209 | return 'Request timed out. Check your internet connection and proxy settings' |
| 210 | } |
| 211 | |
| 212 | // Handle SSL/TLS errors with specific messages |
| 213 | if (isSSLError) { |
| 214 | switch (code) { |
| 215 | case 'UNABLE_TO_VERIFY_LEAF_SIGNATURE': |
| 216 | case 'UNABLE_TO_GET_ISSUER_CERT': |
| 217 | case 'UNABLE_TO_GET_ISSUER_CERT_LOCALLY': |
| 218 | return 'Unable to connect to API: SSL certificate verification failed. Check your proxy or corporate SSL certificates' |
| 219 | case 'CERT_HAS_EXPIRED': |
| 220 | return 'Unable to connect to API: SSL certificate has expired' |
| 221 | case 'CERT_REVOKED': |
| 222 | return 'Unable to connect to API: SSL certificate has been revoked' |
| 223 | case 'DEPTH_ZERO_SELF_SIGNED_CERT': |
| 224 | case 'SELF_SIGNED_CERT_IN_CHAIN': |
| 225 | return 'Unable to connect to API: Self-signed certificate detected. Check your proxy or corporate SSL certificates' |
| 226 | case 'ERR_TLS_CERT_ALTNAME_INVALID': |
| 227 | case 'HOSTNAME_MISMATCH': |
| 228 | return 'Unable to connect to API: SSL certificate hostname mismatch' |
| 229 | case 'CERT_NOT_YET_VALID': |
| 230 | return 'Unable to connect to API: SSL certificate is not yet valid' |
| 231 | default: |
| 232 | return `Unable to connect to API: SSL error (${code})` |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (error.message === 'Connection error.') { |
| 238 | // If we have a code but it's not SSL, include it for debugging |
| 239 | if (connectionDetails?.code) { |
| 240 | return `Unable to connect to API (${connectionDetails.code})` |
| 241 | } |
| 242 | return 'Unable to connect to API. Check your internet connection' |
| 243 | } |
| 244 | |
| 245 | // Guard: when deserialized from JSONL (e.g. --resume), the error object may |
| 246 | // be a plain object without a `.message` property. Return a safe fallback |
| 247 | // instead of undefined, which would crash callers that access `.length`. |
| 248 | if (!error.message) { |
| 249 | return ( |
| 250 | extractNestedErrorMessage(error) ?? |
| 251 | `API error (status ${error.status ?? 'unknown'})` |
| 252 | ) |
| 253 | } |
| 254 | |
| 255 | const sanitizedMessage = sanitizeAPIError(error) |
| 256 | // Use sanitized message if it's different from the original (i.e., HTML was sanitized) |
| 257 | return sanitizedMessage !== error.message && sanitizedMessage.length > 0 |
no test coverage detected