(value: unknown)
| 4 | const NESTED_ERROR_KEYS = ['message', 'error_description', 'detail', 'code', 'type'] as const; |
| 5 | |
| 6 | export function extractApiErrorMessage(value: unknown): string | undefined { |
| 7 | if (Array.isArray(value)) { |
| 8 | for (const item of value) { |
| 9 | const message = extractApiErrorMessage(item); |
| 10 | if (message !== undefined) return message; |
| 11 | } |
| 12 | return undefined; |
| 13 | } |
| 14 | |
| 15 | if (!isRecord(value)) return undefined; |
| 16 | |
| 17 | for (const key of DIRECT_ERROR_KEYS) { |
| 18 | const message = stringField(value, key); |
| 19 | if (message !== undefined) return message; |
| 20 | } |
| 21 | |
| 22 | const error = value['error']; |
| 23 | const errorString = nonEmptyString(error); |
| 24 | if (errorString !== undefined) return errorString; |
| 25 | |
| 26 | if (isRecord(error)) { |
| 27 | for (const key of NESTED_ERROR_KEYS) { |
| 28 | const message = stringField(error, key); |
| 29 | if (message !== undefined) return message; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | const errors = value['errors']; |
| 34 | if (Array.isArray(errors)) { |
| 35 | for (const item of errors) { |
| 36 | const message = extractApiErrorMessage(item); |
| 37 | if (message !== undefined) return message; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return undefined; |
| 42 | } |
| 43 | |
| 44 | export async function readApiErrorMessage( |
| 45 | response: Response, |
no test coverage detected