(message: string)
| 352 | * @returns The extracted message from the JSON payload, or undefined if not found |
| 353 | */ |
| 354 | export function extractMessageFromJsonPayload(message: string): string | undefined { |
| 355 | // Find the first occurrence of '{' which may indicate JSON content |
| 356 | const jsonStartIndex = message.indexOf("{") |
| 357 | if (jsonStartIndex === -1) { |
| 358 | return undefined |
| 359 | } |
| 360 | |
| 361 | const potentialJson = message.slice(jsonStartIndex) |
| 362 | |
| 363 | try { |
| 364 | const parsed = JSON.parse(potentialJson) |
| 365 | |
| 366 | // Handle structure: {"error":{"message":"..."}} or {"error":{"code":"","message":"..."}} |
| 367 | if (parsed?.error?.message && typeof parsed.error.message === "string") { |
| 368 | return parsed.error.message |
| 369 | } |
| 370 | |
| 371 | // Handle structure: {"message":"..."} |
| 372 | if (parsed?.message && typeof parsed.message === "string") { |
| 373 | return parsed.message |
| 374 | } |
| 375 | } catch { |
| 376 | // JSON parsing failed - not valid JSON |
| 377 | } |
| 378 | |
| 379 | return undefined |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Extracts the most descriptive error message from an error object. |
no test coverage detected