( error: unknown, )
| 40 | * This function walks the cause chain to find the root error code/message. |
| 41 | */ |
| 42 | export function extractConnectionErrorDetails( |
| 43 | error: unknown, |
| 44 | ): ConnectionErrorDetails | null { |
| 45 | if (!error || typeof error !== 'object') { |
| 46 | return null |
| 47 | } |
| 48 | |
| 49 | // Walk the cause chain to find the root error with a code |
| 50 | let current: unknown = error |
| 51 | const maxDepth = 5 // Prevent infinite loops |
| 52 | let depth = 0 |
| 53 | |
| 54 | while (current && depth < maxDepth) { |
| 55 | if ( |
| 56 | current instanceof Error && |
| 57 | 'code' in current && |
| 58 | typeof current.code === 'string' |
| 59 | ) { |
| 60 | const code = current.code |
| 61 | const isSSLError = SSL_ERROR_CODES.has(code) |
| 62 | return { |
| 63 | code, |
| 64 | message: current.message, |
| 65 | isSSLError, |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // Move to the next cause in the chain |
| 70 | if ( |
| 71 | current instanceof Error && |
| 72 | 'cause' in current && |
| 73 | current.cause !== current |
| 74 | ) { |
| 75 | current = current.cause |
| 76 | depth++ |
| 77 | } else { |
| 78 | break |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return null |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns an actionable hint for SSL/TLS errors, intended for contexts outside |
no test coverage detected