* Extract a human-readable message from a deserialized API error that lacks * a top-level `.message`. * * Checks two nesting levels (deeper first for specificity): * 1. `error.error.error.message` — standard Anthropic API shape * 2. `error.error.message` — Bedrock shape
(error: APIError)
| 167 | * 2. `error.error.message` — Bedrock shape |
| 168 | */ |
| 169 | function extractNestedErrorMessage(error: APIError): string | null { |
| 170 | if (!hasNestedError(error)) { |
| 171 | return null |
| 172 | } |
| 173 | |
| 174 | // Access `.error` via the narrowed type so TypeScript sees the nested shape |
| 175 | // instead of the SDK's `Object | undefined`. |
| 176 | const narrowed: NestedAPIError = error |
| 177 | const nested = narrowed.error |
| 178 | |
| 179 | // Standard Anthropic API shape: { error: { error: { message } } } |
| 180 | const deepMsg = nested?.error?.message |
| 181 | if (typeof deepMsg === 'string' && deepMsg.length > 0) { |
| 182 | const sanitized = sanitizeMessageHTML(deepMsg) |
| 183 | if (sanitized.length > 0) { |
| 184 | return sanitized |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Bedrock shape: { error: { message } } |
| 189 | const msg = nested?.message |
| 190 | if (typeof msg === 'string' && msg.length > 0) { |
| 191 | const sanitized = sanitizeMessageHTML(msg) |
| 192 | if (sanitized.length > 0) { |
| 193 | return sanitized |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return null |
| 198 | } |
| 199 | |
| 200 | export function formatAPIError(error: APIError): string { |
| 201 | // Extract connection error details from the cause chain |
no test coverage detected