MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / formatAPIError

Function formatAPIError

source code/services/api/errorUtils.ts:202–262  ·  view source on GitHub ↗
(error: APIError)

Source from the content-addressed store, hash-verified

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

Callers 3

SystemAPIErrorMessageFunction · 0.85

Calls 3

sanitizeAPIErrorFunction · 0.85

Tested by

no test coverage detected