(
error: unknown,
options: { includeRawError?: boolean } = {},
)
| 380 | } |
| 381 | |
| 382 | export function getErrorObject( |
| 383 | error: unknown, |
| 384 | options: { includeRawError?: boolean } = {}, |
| 385 | ): ErrorObject { |
| 386 | if (error instanceof Error) { |
| 387 | const extError = error as Error & Partial<ExtendedErrorProperties> |
| 388 | |
| 389 | // Extract responseBody - could be string or object |
| 390 | let responseBody: string | undefined |
| 391 | if (extError.responseBody !== undefined) { |
| 392 | responseBody = safeStringify(extError.responseBody) |
| 393 | } |
| 394 | |
| 395 | // Extract requestBodyValues - typically an object, stringify for logging |
| 396 | let requestBodyValues: string | undefined |
| 397 | if ( |
| 398 | extError.requestBodyValues !== undefined && |
| 399 | typeof extError.requestBodyValues === 'object' |
| 400 | ) { |
| 401 | requestBodyValues = safeStringify(extError.requestBodyValues) |
| 402 | } |
| 403 | |
| 404 | // Extract cause - recursively convert to ErrorObject if present |
| 405 | let cause: ErrorObject | undefined |
| 406 | if (extError.cause !== undefined) { |
| 407 | cause = getErrorObject(extError.cause, options) |
| 408 | } |
| 409 | |
| 410 | return { |
| 411 | name: error.name, |
| 412 | message: error.message, |
| 413 | stack: error.stack, |
| 414 | status: typeof extError.status === 'number' ? extError.status : undefined, |
| 415 | statusCode: |
| 416 | typeof extError.statusCode === 'number' |
| 417 | ? extError.statusCode |
| 418 | : undefined, |
| 419 | code: typeof extError.code === 'string' ? extError.code : undefined, |
| 420 | rawError: options.includeRawError ? safeStringify(error) : undefined, |
| 421 | // API error fields |
| 422 | responseBody, |
| 423 | url: typeof extError.url === 'string' ? extError.url : undefined, |
| 424 | isRetryable: |
| 425 | typeof extError.isRetryable === 'boolean' |
| 426 | ? extError.isRetryable |
| 427 | : undefined, |
| 428 | requestBodyValues, |
| 429 | cause, |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | return { |
| 434 | name: 'Error', |
| 435 | message: `${error}`, |
| 436 | } |
| 437 | } |
no test coverage detected