Skip to content

Error Handling

Every non-2xx response throws an HttpError.

The shape

ts
import { HttpError } from '@bir-tan/crisp-oquent';

try {
  await someRequest();
} catch (e) {
  if (e instanceof HttpError) {
    e.status;         // number — 422, 404, 500, ...
    e.statusText;     // 'Unprocessable Entity'
    e.url;            // request URL
    e.body;           // parsed JSON (or text fallback)
  }
}

Helper getters

ts
e.isNotFound;          // status === 404
e.isUnauthorized;      // status === 401
e.isForbidden;         // status === 403
e.isValidationError;   // status === 422

Laravel validation errors

When the backend returns a 422 with the standard Laravel shape:

json
{
  "message": "The given data was invalid.",
  "errors": {
    "email": ["The email field is required."],
    "password": ["The password must be at least 8 characters."]
  }
}

Use the typed accessor:

ts
catch (e) {
  if (e instanceof HttpError && e.isValidationError) {
    const errors = e.validationErrors;   // Record<string, string[]> | null
    errors?.email;                       // ['The email field is required.']
  }
}

404 vs throw

Builder.find(id) is special-cased: a 404 returns null instead of throwing, so you can write:

ts
const user = await User.crispy().find(id);
if (user === null) {
  // not found
}

All other methods throw on 404.