1.0.1 • Published 4 years ago

http-res-errors v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

NPM Version CI codecov Dependency Status Dev Dependency Status

HTTP Response Errors

Simple HTTP error response classes for any framework.

Table of Contents

Features

  • 🚀 Zero dependencies.
  • 🔎 Detect http errors with instanceof.
  • 🎉 Written in TypeScript.

Installation

npm install http-res-errors --save

Usage

Predefined Errors

http-res-errors includes predefined classes for common HTTP status codes.

import { BadRequest } from 'http-res-errors';

throw new BadRequest(`That doesn't work`);
StatusError
400BadRequest
401Unauthorized
403Forbidden
404NotFound
405MethodNotAllowed
406NotAcceptable
408RequestTimeout
409Conflict
410Gone
412PreconditionFailed
413PayloadTooLarge
415UnsupportedMediaType
418ImATeaPotSupported
421Misdirected
422UnprocessableEntity
500InternalServerError
501NotImplemented
502BadGateway
503ServiceUnavailable
504GatewayTime
505HttpVersionNotSupported

Custom Errors

You can use the base HttpError class to return any status.

import { HttpError } from 'http-res-errors';

throw new HttpError(999, 'Something crazy!');

Middleware

Provide catch and response to HTTP errors with frameworks like ExpressJS.

Note: This works with routes that return a promise, like async functions.

import { catchHttpErrors, UnprocessableEntity } from 'http-res-errors';

app.use(catchHttpErrors);

app.get('/', async (res, req) => {
  if (!res.user) {
    throw new Unauthorized('Nice try');
  }

  // ...
});

Development

npm install
npm run build
1.0.1

4 years ago

1.0.0

4 years ago