1.0.0 • Published 5 years ago

heror v1.0.0

Weekly downloads
6
License
MIT
Repository
github
Last release
5 years ago

heror

Build Coverage DevDependencies LatestVersion License

HTTP error classes

Install

yarn add heror

Class

new BadRequestError(message: string = 'Bad Request', data?: any)
  • message - error message
  • data - optional input that you can use for additional information regarding the error

Function

createError(statusCode: number, error: string)
  • statusCode - http error status code
  • error - official http error type/message

List of error classes

We provide some common http errors below. If your case is not covered by the errors. You could create new error using createError function. Please see usage examples.

Status CodeError Class Name
400BadRequestError
401UnauthorizedError
403ForbiddenError
404NotFoundError
405MethodNotAllowedError
406NotAcceptableError
408RequestTimeoutError
409ConflictError
412PreconditionFailedError
422UnprocessableEntityError
500InternalServerError
501NotImplementedError
503ServiceUnavailableError
507InsufficientStorageError
508NotExtendedError

Usage Examples

import { BadRequestError, UnauthorizedError, createError } from 'heror';

const URITooLongError = createError(414, 'URI Too Long');

async function userController(req, res, next) {
  if (!req.auth) {
    throw new UnauthorizedError('Authentication is required', {
      expose: true,
      details: {}
    });
  }
  /*
    it will throw error instance
    {
      statusCode: 401,
      error: 'Unauthorized',
      message: 'Authentication is required',
      data: {
        expose: true,
        details: {}
      },
      ...nativeErrorProperties
    }
  /*

  if (!req.params.id) throw new BadRequestError();
  /*
    it will throw error instance
    {
      statusCode: 400,
      error: 'Bad Request',
      message: 'Bad Request',
      ...nativeErrorProperties
    }
  */

  if (req.uri.length > 100) {
    throw new URITooLongError('Max URI Length is 100 characters', {
      uriLength: req.uri.length
    });
  }
  /*
    it will throw error instance
    {
      statusCode: 414,
      error: 'URI Too Long',
      message: 'Max URI Length is 100 characters',
      data: {
        uriLength: 111
      },
      ...nativeErrorProperties
    }
  /*

}

License

MIT