Skip to content

Errors ​

Routup represents every failure of a request as an AppError, an HTTP error with a status. The Error Handling guide explains how errors reach error handlers and the fallback response.

AppError ​

typescript
declare class AppError extends HTTPError {
    constructor(input?: HTTPErrorInput);
}

An error with an HTTP status, based on HTTPError of @ebec/http. Its argument, HTTPErrorInput of @ebec/http, is a message string or an options object.

typescript
import { AppError } from 'routup';

throw new AppError({ status: 404, message: 'User not found' });
throw new AppError('Something failed'); // status 500
OptionTypeDefaultMeaning
statusnumber | string500Status between 400 and 599
messagestringreason phrase of statusMessage
codestring'APP_ERROR'Identifier of the error
causeunknownnoneThe underlying error
redirectURLstringnoneA URL to redirect to
errorsreadonly Error[]noneErrors of a batch
issuesreadonly Issue[][]Validation failures
messageDataRecord<string, unknown>noneValues for {name} placeholders in message; not stored on the error
stackstringthe generated traceStack trace
  • A status outside 400 to 599, or one that is not a number, becomes 500.
  • The options other than messageData become properties of the same name, with the defaults above. statusCode is a deprecated alias of status.
  • toJSON() returns name, message, code, status and an @instanceof marker list, plus cause, errors and issues when set.

createError ​

typescript
declare function createError(input: HTTPErrorInput | unknown): AppError;

Converts any value into an AppError. The pipeline applies it to every thrown value and to the argument of event.next(error); the conversion table lists the results.

typescript
import { createError } from 'routup';

createError('Bad input');             // status 500, message 'Bad input'
createError({ status: 400 });         // status 400, message 'Bad Request'
createError(new Error('Disk full'));  // status 500, cause: the Error

An AppError is returned as is, not copied.

isError ​

typescript
declare function isError(input: unknown): input is AppError;

Checks whether a value is an AppError.

typescript
import { isError } from 'routup';

if (isError(e) && e.status === 404) {
    return null;
}

It checks a marker, not instanceof, so an AppError of another copy of routup also counts. A plain Error does not; see Checking for an AppError.