29 lines
773 B
TypeScript
29 lines
773 B
TypeScript
/**
|
|
* Defines the exception that carries an HTTP error from any layer up to the
|
|
* error middleware.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import type { APIErrorCode } from "../../shared/api.ts";
|
|
|
|
/**
|
|
* Thrown by a route, a service or a remote fetch to answer the request with an
|
|
* error. The error middleware sends it as an `APIFailure`, message included,
|
|
* so the message must be fit for the user.
|
|
*/
|
|
export class APIException extends Error {
|
|
readonly code: APIErrorCode;
|
|
readonly status: number;
|
|
|
|
/**
|
|
* Creates the exception. A code goes with several statuses, as
|
|
* `UNAUTHORIZED` does with 401 and 403, so both are given.
|
|
*/
|
|
constructor(code: APIErrorCode, status: number, message: string) {
|
|
super(message);
|
|
this.code = code;
|
|
this.status = status;
|
|
}
|
|
}
|