Skip to content

Errors

All errors extend HttpError and serialize to { error, status, code, details? }.

hiroki.process() catches all errors and returns the serialized form — it never throws.

Error reference

ClassStatusCode
BadRequestError400BAD_REQUEST
NotFoundError404NOT_FOUND
MethodNotAllowedError405METHOD_NOT_ALLOWED
InternalServerError500INTERNAL_ERROR
InvalidModelError400INVALID_MODEL
InvalidConditionsError400INVALID_CONDITIONS
ParamRequiredError400 / 404PARAM_REQUIRED
DocumentNotFoundError404DOCUMENT_NOT_FOUND
BodyRequiredError400BODY_REQUIRED
DisabledMethodError405METHOD_DISABLED
RouteNotFoundError404ROUTE_NOT_FOUND

Using errors in hooks and middleware

Import error classes to return typed HTTP responses from hooks or middleware:

ts
import { BadRequestError, NotFoundError } from 'hiroki';

const requireAuth: HirokiMiddleware = async (ctx, next) => {
  if (!ctx.query?.token) {
    throw new BadRequestError('Authentication required', 'UNAUTHORIZED');
  }
  return next();
};

isHttpError / hasStatus

ts
import { isHttpError, hasStatus } from 'hiroki';

if (isHttpError(err)) {
  console.log(err.status, err.code);
}

Released under the MIT License.