docs(api): document apiClient error.status / errorCode / details surface
All checks were successful
CI / ruff (push) Successful in 16s
CI / pytest (push) Successful in 2h48m39s
CI / validate (push) Successful in 33s
CI / dependency-scanning (push) Successful in 36s
CI / docs (push) Successful in 57s
CI / deploy (push) Successful in 1m53s

Adds a "Frontend Error Handling (apiClient)" section to the error
handling guide. Callers can branch on `error.errorCode` and read
`error.details` to localise toasts instead of rendering the raw
English `message`, as already done in the loyalty terminal cooldown
handler.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-24 00:32:42 +02:00
parent d03b96da1c
commit 82939c0005

View File

@@ -284,6 +284,33 @@ async def invalid_token_handler(request, exc):
)
```
## Frontend Error Handling (apiClient)
When a request fails, the shared `apiClient` (`static/shared/js/api-client.js`) throws an `Error` with three structured properties populated from the JSON response, in addition to the `message`:
| Property | Source | Example |
|----------|--------|---------|
| `error.status` | HTTP status code | `429` |
| `error.errorCode` | Server `error_code` field | `"POINTS_COOLDOWN"` |
| `error.details` | Server `details` object (may be `null`) | `{ cooldown_minutes: 5, cooldown_ends: "..." }` |
This lets callers branch on a machine-readable code and localise the toast instead of rendering the raw English `message`. Pattern:
```js
try {
await apiClient.post('/store/loyalty/transactions', payload);
} catch (error) {
if (error.errorCode === 'POINTS_COOLDOWN' || error.errorCode === 'STAMP_COOLDOWN') {
const minutes = error.details?.cooldown_minutes ?? '';
Utils.showToast(I18n.t('loyalty.store.terminal.cooldown_wait_minutes', { minutes }), 'warning');
} else {
Utils.showToast(I18n.t('loyalty.store.terminal.transaction_failed', { message: error.message }), 'error');
}
}
```
Generic FastAPI errors that don't carry `error_code` / `details` will leave those properties `undefined` / `null`, so a `?.` chain is the right guard.
## Related Documentation
- [Authentication](authentication.md) - Authentication-related exceptions