(error: unknown, context?: string)
| 515 | * Useful for catch blocks where the error type is unknown. |
| 516 | */ |
| 517 | export function wrapError(error: unknown, context?: string): ComponentError { |
| 518 | // Already a ComponentError |
| 519 | if (isComponentError(error)) { |
| 520 | return error; |
| 521 | } |
| 522 | |
| 523 | // Standard Error - try to classify |
| 524 | if (error instanceof Error) { |
| 525 | const message = context |
| 526 | ? `${context}: ${error.message}` |
| 527 | : error.message; |
| 528 | |
| 529 | // Check for common network error patterns |
| 530 | if ( |
| 531 | error.message.includes('ENOTFOUND') || |
| 532 | error.message.includes('ECONNREFUSED') || |
| 533 | error.message.includes('ENETUNREACH') || |
| 534 | error.message.includes('ETIMEDOUT') || |
| 535 | error.message.includes('socket hang up') || |
| 536 | error.message.includes('network') || |
| 537 | error.name === 'FetchError' |
| 538 | ) { |
| 539 | return NetworkError.from(error); |
| 540 | } |
| 541 | |
| 542 | // Check for abort/timeout patterns |
| 543 | if ( |
| 544 | error.name === 'AbortError' || |
| 545 | error.message.includes('aborted') || |
| 546 | error.message.includes('timeout') |
| 547 | ) { |
| 548 | return new TimeoutError(message, 0, { cause: error }); |
| 549 | } |
| 550 | |
| 551 | // Default to ServiceError (retryable) for unknown errors |
| 552 | return new ServiceError(message, { cause: error }); |
| 553 | } |
| 554 | |
| 555 | // Unknown error type |
| 556 | const message = context |
| 557 | ? `${context}: ${String(error)}` |
| 558 | : String(error); |
| 559 | return new ServiceError(message); |
| 560 | } |
| 561 | |
| 562 | // ───────────────────────────────────────────────────────────────────────────── |
| 563 | // Error Type Names (for non-retryable error type configuration) |
no test coverage detected