Extract a human-readable error message from a WhatsApp API error payload.
(data: Record<string, unknown>, status: number)
| 34 | |
| 35 | /** Extract a human-readable error message from a WhatsApp API error payload. */ |
| 36 | function extractErrorMessage(data: Record<string, unknown>, status: number): string { |
| 37 | const error = isRecord(data.error) ? data.error : undefined |
| 38 | return ( |
| 39 | (typeof error?.message === 'string' ? error.message : undefined) || |
| 40 | (typeof error?.error_user_msg === 'string' ? error.error_user_msg : undefined) || |
| 41 | (isRecord(error?.error_data) && typeof error.error_data.details === 'string' |
| 42 | ? error.error_data.details |
| 43 | : undefined) || |
| 44 | `WhatsApp API error (${status})` |
| 45 | ) |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Transform the shared send response shape returned by every outbound message |
no test coverage detected