| 169 | } |
| 170 | |
| 171 | export function extractZoomInfoError(body: unknown, status: number): string { |
| 172 | if (body && typeof body === 'object') { |
| 173 | const obj = body as Record<string, unknown> |
| 174 | if (obj.error && typeof obj.error === 'object') { |
| 175 | const eo = obj.error as Record<string, unknown> |
| 176 | const message = typeof eo.message === 'string' ? eo.message : '' |
| 177 | const code = typeof eo.code === 'string' ? eo.code : '' |
| 178 | if (message) return code ? `[${code}] ${message}` : message |
| 179 | } |
| 180 | if (typeof obj.error === 'string' && obj.error.length > 0) { |
| 181 | const desc = typeof obj.error_description === 'string' ? `: ${obj.error_description}` : '' |
| 182 | return `${obj.error}${desc}` |
| 183 | } |
| 184 | if (typeof obj.message === 'string' && obj.message.length > 0) { |
| 185 | return obj.message |
| 186 | } |
| 187 | if (Array.isArray(obj.errors) && obj.errors.length > 0) { |
| 188 | return obj.errors |
| 189 | .map((e) => { |
| 190 | if (e && typeof e === 'object') { |
| 191 | const eo = e as Record<string, unknown> |
| 192 | const title = typeof eo.title === 'string' ? eo.title : '' |
| 193 | const detail = typeof eo.detail === 'string' ? `: ${eo.detail}` : '' |
| 194 | return `${title}${detail}`.trim() |
| 195 | } |
| 196 | return String(e) |
| 197 | }) |
| 198 | .filter(Boolean) |
| 199 | .join('; ') |
| 200 | } |
| 201 | } |
| 202 | if (typeof body === 'string' && body.length > 0) return body |
| 203 | return `ZoomInfo request failed with HTTP ${status}` |
| 204 | } |