| | | 1 | | /** |
| | | 2 | | * Extracts a human-readable reason from an unknown thrown value. |
| | | 3 | | * |
| | | 4 | | * Digs through `AggregateError.errors` and `Error.cause` so the reason is never |
| | | 5 | | * empty — the AWS SDK v3 wraps connection failures in an `AggregateError` whose |
| | | 6 | | * own `message` is empty and whose real causes live in `errors`. |
| | | 7 | | */ |
| | | 8 | | export function describeError(error: unknown): string { |
| | 25 | 9 | | const reasons = [...new Set(collectReasons(error))]; |
| | 25 | 10 | | return reasons.length > 0 ? reasons.join('; ') : 'unknown error'; |
| | | 11 | | } |
| | | 12 | | |
| | | 13 | | function collectReasons(error: unknown): string[] { |
| | 32 | 14 | | if (error instanceof AggregateError && error.errors.length > 0) { |
| | 5 | 15 | | return error.errors.flatMap(collectReasons); |
| | | 16 | | } |
| | 27 | 17 | | if (error instanceof Error) { |
| | 22 | 18 | | if (error.message) { |
| | 21 | 19 | | return [error.message]; |
| | | 20 | | } |
| | 1 | 21 | | if (error.cause !== undefined) { |
| | 1 | 22 | | return collectReasons(error.cause); |
| | | 23 | | } |
| | 0 | 24 | | return error.name ? [error.name] : []; |
| | | 25 | | } |
| | 5 | 26 | | const text = String(error); |
| | 5 | 27 | | return text ? [text] : []; |
| | | 28 | | } |