| 100 | * `HttpError` without a usable body yields `fallback`. |
| 101 | */ |
| 102 | export function getApiErrorMessage(error: unknown, fallback: string): string { |
| 103 | const body = getApiErrorBody(error); |
| 104 | |
| 105 | if (body && typeof body === "object") { |
| 106 | const { |
| 107 | message, |
| 108 | exception, |
| 109 | detail, |
| 110 | error: bodyError, |
| 111 | } = body as { |
| 112 | message?: unknown; |
| 113 | exception?: unknown; |
| 114 | detail?: unknown; |
| 115 | error?: unknown; |
| 116 | }; |
| 117 | if (typeof message === "string" && message) return message; |
| 118 | const validationMessage = getValidationDetailMessage(detail); |
| 119 | const structuredDetailMessage = |
| 120 | detail && typeof detail === "object" && !Array.isArray(detail) |
| 121 | ? (detail as { message?: unknown }).message |
| 122 | : undefined; |
| 123 | const detailMessage = |
| 124 | typeof detail === "string" && detail |
| 125 | ? detail |
| 126 | : validationMessage || |
| 127 | getStructuredDetailErrorsMessage(detail) || |
| 128 | (typeof structuredDetailMessage === "string" && |
| 129 | structuredDetailMessage |
| 130 | ? structuredDetailMessage |
| 131 | : undefined); |
| 132 | if ( |
| 133 | typeof exception === "string" && |
| 134 | exception && |
| 135 | (!detailMessage || detailMessage === AGENT_SERVER_GENERIC_DETAIL) |
| 136 | ) { |
| 137 | return exception; |
| 138 | } |
| 139 | if (detailMessage) return detailMessage; |
| 140 | if (typeof bodyError === "string" && bodyError) return bodyError; |
| 141 | } |
| 142 | |
| 143 | if (isSdkHttpError(error)) return fallback; |
| 144 | if (error instanceof Error && error.message) return error.message; |
| 145 | return fallback; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Whether the server answered a failed call: there is a response body, or the |