(err: unknown, fallback: string)
| 24 | | { kind: 'title-too-long'; limit: number } |
| 25 | | { kind: 'message-empty' } |
| 26 | | { kind: 'server'; message: string } |
| 27 | | { kind: 'fallback' }; |
| 28 | |
| 29 | /** |
| 30 | * Classify an error thrown by `KiloChatClient`. |
| 31 | * |
| 32 | * - Non-{@link KiloChatApiError} (network/abort/timeout) → `fallback`. |
| 33 | * - 401/403 → `not-allowed` (avoids leaking server phrasing). |
| 34 | * - 4xx with a zod `issues` array → classify the first known issue; otherwise |
| 35 | * surface the server's `error` string when present as `server`. |
| 36 | * - 5xx / unknown → `fallback`. |
| 37 | * |
| 38 | * Callers that need custom branching (e.g. 409 edit conflicts) should check |
| 39 | * `err instanceof KiloChatApiError` themselves before delegating here. |
| 40 | */ |
| 41 | export function classifyKiloChatError(err: unknown): KiloChatErrorKind { |
| 42 | if (!(err instanceof KiloChatApiError)) return { kind: 'fallback' }; |
| 43 | |
| 44 | if (err.status === 401 || err.status === 403) return { kind: 'not-allowed' }; |
| 45 | if (err.status >= 500) return { kind: 'fallback' }; |
| 46 | |
| 47 | const body = err.body; |
| 48 | if (!body || typeof body !== 'object') return { kind: 'fallback' }; |
no test coverage detected