Error Handling

Typed error hierarchy with type-guard utilities

Error Types

ClassThrown WhenKey Properties
ApiErrorServer returns non-2xx statusstatus, body
TimeoutErrorTimeout middleware firestimeout (ms)
NetworkErrorFetch 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;
            }
        ],
    },
});