Error Handling
Typed error hierarchy with type-guard utilities
Error Types
| Class | Thrown When | Key Properties |
|---|---|---|
ApiError | Server returns non-2xx status | status, body |
TimeoutError | Timeout middleware fires | timeout (ms) |
NetworkError | Fetch itself throws (DNS, offline) | cause |
All three extend WebError, which extends Error.
Type Guards
import {
isApiError,
isTimeoutError,
isNetworkError,
isWebError,
} from '@cleverbrush/client';
try {
await client.todos.list();
} catch (err) {
if (isApiError(err)) {
// err.status, err.body
} else if (isTimeoutError(err)) {
// err.timeout
} else if (isNetworkError(err)) {
// err.cause — the original fetch error
}
}Using with the beforeError Hook
const client = createClient(api, {
hooks: {
beforeError: [
(error) => {
if (isApiError(error) && error.status === 404) {
// Transform 404s into a custom error
return new NotFoundError(error.message);
}
return error;
}
],
},
});