@valbo/http-errors
v1.1.0
Published
HTTP error classes with a status, name and message
Downloads
4
Readme
@valbo/http-errors
HTTP error classes with a status, name and message.
Install
npm install @valbo/http-errors
Usage
The HttpError class extends Error and allows you to create a http error with a status, name and message:
import { HttpError } from '@valbo/http-errors';
const error = new Error(401, 'MissingCredentialsError', 'Missing Authorization header.');
console.log(`${error.status} ${error.name}: ${error.message}`);
"401 MissingCredentialsError: Missing Authorization header."
There is a subclass of HttpError for every standard HTTP error. The status and name are hardcoded, and they have a standard message that you can change:
import { NotFoundError, UnauthorizedError } from '@valbo/http-errors';
const notFound = new NotFoundError();
console.log(`${notFound.status} ${notFound.name}: ${notFound.message}`);
"404 NotFoundError: The resource could not be found."
const unauthorized = new UnauthorizedError('The token has expired.');
console.log(`${unauthorized.status} ${unauthorized.name}: ${unauthorized.message}`);
"401 UnauthorizedError: The token has expired."