(data: unknown)
| 14 | * Returns undefined when no message-like field is present so callers can fall back. |
| 15 | */ |
| 16 | export function parseGraphErrorFromData(data: unknown): string | undefined { |
| 17 | if (!data || typeof data !== 'object') return undefined |
| 18 | |
| 19 | const root = ( |
| 20 | data as { |
| 21 | error?: { |
| 22 | code?: unknown |
| 23 | message?: unknown |
| 24 | innerError?: unknown |
| 25 | innererror?: unknown |
| 26 | details?: unknown |
| 27 | } |
| 28 | } |
| 29 | ).error |
| 30 | if (root && typeof root === 'object') { |
| 31 | const messages: string[] = [] |
| 32 | if (typeof root.message === 'string' && root.message.trim()) { |
| 33 | messages.push(root.message.trim()) |
| 34 | } |
| 35 | |
| 36 | // Walk the (possibly nested) innerError chain. Spec uses `innererror` |
| 37 | // but Graph commonly returns `innerError` — accept both. |
| 38 | let inner: any = (root as any).innererror ?? (root as any).innerError |
| 39 | let depth = 0 |
| 40 | while (inner && depth < 5) { |
| 41 | if (typeof inner.message === 'string' && inner.message.trim()) { |
| 42 | const msg = inner.message.trim() |
| 43 | if (!messages.includes(msg)) messages.push(msg) |
| 44 | } |
| 45 | inner = inner.innererror ?? inner.innerError |
| 46 | depth++ |
| 47 | } |
| 48 | |
| 49 | if (Array.isArray((root as any).details)) { |
| 50 | for (const detail of (root as any).details) { |
| 51 | if (detail && typeof detail.message === 'string' && detail.message.trim()) { |
| 52 | const msg = detail.message.trim() |
| 53 | if (!messages.includes(msg)) messages.push(msg) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if (messages.length > 0) return messages.join(' — ') |
| 59 | |
| 60 | if (typeof root.code === 'string' && root.code.trim()) { |
| 61 | return root.code.trim() |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | const topMessage = (data as { message?: unknown }).message |
| 66 | if (typeof topMessage === 'string' && topMessage.trim()) { |
| 67 | return topMessage.trim() |
| 68 | } |
| 69 | |
| 70 | return undefined |
| 71 | } |
| 72 | |
| 73 | /** |
no test coverage detected