Throw HTTP error status codes as Errors
npm install http-status-errorsThrow HTTP error status codes as Errors!
$ npm install --save http-status-errors
Import the errors you want to use, or all of them.
``js`
import {NotFoundError} from 'http-status-errors';
// or...
import * as errors from 'http-status-errors';
Or if you're stuck with old-fashioned JS:
`js`
var NotFoundError = require('http-status-errors').NotFoundError;
// or...
var errors = require('http-status-errors')
Once you've imported the package, just throw as normal. All constructors accept a message as the only argument, otherwise they'll use the default message for that error. All errors also have a code field which is the corresponding HTTP status code as an integer.
E.g.:
`js`
throw new NotFoundError('widget not found');
Below is the list of error names, status codes and default messages that this package supports.
| Code | Class name | Default message |
|---|---|---|
| 400 | BadRequestError | There is a problem with the submitted request. |NotAuthenticatedError
| 401 | | You are not authenticated. Please authenticate and try again. |PaymentRequiredError
| 402 | | The action you are trying to perform requires payment. |ForbiddenError
| 403 | | You are forbidden from accessing the requested resource. |NotFoundError
| 404 | | The requested resource was not found. |MethodNotAllowedError
| 405 | | The resource does not support the requested method. |NotAcceptableError
| 406 | | The resource cannot be formatted according to the requested type in the Accept header. |ConflictError
| 409 | | There is a conflict with the requested resource. |TeapotError
| 418 | | I'm a teapot... |UnavailableError
| 451 | | The requested resource is unavailable for legal reasons. |InternalServerError
| 500 | | The server has encountered an error. |NotImplementedError
| 501 | | The action you are trying to perform is not implemented. |ServiceUnavailableError` | The service is currently unavailable. |
| 503 |