Every error class exposes a statusCode property with the underlying HTTP status code.
import { Law4DevsError, NotFoundError, ValidationError, RateLimitError, ServerError,} from 'law4devs';
Basic try/catch
try { const article = await client.articles.get('gdpr', 999); console.log(article.title);} catch (err) { if (err instanceof NotFoundError) { console.error(`Not found (${err.statusCode}): ${err.message}`); } else if (err instanceof ValidationError) { console.error(`Invalid request (${err.statusCode}): ${err.message}`); } else if (err instanceof RateLimitError) { console.error(`Rate limited (${err.statusCode}) — all retries exhausted`); } else if (err instanceof ServerError) { console.error(`Server error (${err.statusCode}) — all retries exhausted`); } else if (err instanceof Law4DevsError) { console.error(`API error (${err.statusCode}): ${err.message}`); } else { throw err; // network error or unexpected — re-throw }}
Auto-retry behaviour
The SDK automatically retries on RateLimitError (429) and ServerError (5xx) using exponential backoff with jitter. Only after all retries are exhausted is the error thrown to your code.
// Configure retry behaviour at construction timeconst client = new Law4DevsClient({ maxRetries: 3, // default: 3 — set to 0 to disable retries timeout: 30000, // ms — each individual attempt has this timeout});
Timeouts are enforced via AbortController, so they apply per-attempt (not across all retries combined).
NotFoundError and ValidationError are never retried — they indicate client-side issues that will not resolve on retry.
TypeScript narrowing with a typed helper
Define a safeGet helper to return a typed result or a structured error without propagating exceptions: