( error: unknown, provider: string, context?: ErrorFormattingContext )
| 384 | |
| 385 | // Format an error into a user-friendly structure |
| 386 | export function formatUserFriendlyError( |
| 387 | error: unknown, |
| 388 | provider: string, |
| 389 | context?: ErrorFormattingContext |
| 390 | ): FormattedError { |
| 391 | const billingUrl = PROVIDER_BILLING_URLS[provider] || null; |
| 392 | |
| 393 | // Handle our custom error types first |
| 394 | if (error instanceof InsufficientCreditsError) { |
| 395 | return { |
| 396 | title: 'Insufficient Credits', |
| 397 | message: `Your ${provider} account has insufficient credits or quota.`, |
| 398 | helpUrl: billingUrl, |
| 399 | suggestion: 'Add credits to your account to continue using the service.' |
| 400 | }; |
| 401 | } |
| 402 | |
| 403 | if (error instanceof RateLimitError) { |
| 404 | const retryMsg = error.retryAfter |
| 405 | ? `Please wait ${error.retryAfter} seconds before retrying.` |
| 406 | : 'Please wait a moment before retrying.'; |
| 407 | return { |
| 408 | title: 'Rate Limit Exceeded', |
| 409 | message: `You've made too many requests to ${provider}.`, |
| 410 | helpUrl: billingUrl, |
| 411 | suggestion: retryMsg |
| 412 | }; |
| 413 | } |
| 414 | |
| 415 | if (error instanceof ServiceUnavailableError) { |
| 416 | return { |
| 417 | title: 'Service Unavailable', |
| 418 | message: getServiceUnavailableMessage(provider, context), |
| 419 | helpUrl: null, |
| 420 | suggestion: 'Please try again in a few moments.' |
| 421 | }; |
| 422 | } |
| 423 | |
| 424 | if (error instanceof AuthenticationError) { |
| 425 | return { |
| 426 | title: 'Authentication Failed', |
| 427 | message: `Your ${provider} API key is invalid or expired.`, |
| 428 | helpUrl: billingUrl, |
| 429 | suggestion: 'Run `oco setup` to configure a valid API key.' |
| 430 | }; |
| 431 | } |
| 432 | |
| 433 | if (error instanceof ModelNotFoundError) { |
| 434 | return { |
| 435 | title: 'Model Not Found', |
| 436 | message: `The model '${error.modelName}' is not available for ${provider}.`, |
| 437 | helpUrl: null, |
| 438 | suggestion: 'Run `oco setup` to select a valid model.' |
| 439 | }; |
| 440 | } |
| 441 | |
| 442 | // Detect error type from raw errors |
| 443 | if (isInsufficientCreditsError(error)) { |
no test coverage detected
searching dependent graphs…