@valbo/http-errors

HTTP error classes with a status, name and message.

Usage no npm install needed!

<script type="module">
  import valboHttpErrors from 'https://cdn.skypack.dev/@valbo/http-errors';
</script>

README

@valbo/http-errors

HTTP error classes with a status, name and message.

npm (scoped) semantic-release Build Status Coverage Status Known Vulnerabilities

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."