npm install httperrhttperr provides Error types for all HTTP error status codes.
  
  

There are several libraries that already do this, but most of them either only support a very limited number of status codes, don't capture stack traces correctly or are no longer maintained.
The biggest difference in httperr is that it lets you attach relevant information for the error in a single function call, allowing you to separate your error handling and error response logic without losing the semantics of HTTP status codes.
``sh`
npm install httperr
`sh`
git clone https://github.com/pluma/httperr.git
cd httperr
npm install
`javascript
var httperr = require('httperr');
var err = httperr404;
console.log(err);
/*
{ [NotFound: The path "/example" could not be resolved]
title: 'Not Found',
name: 'NotFound',
code: 'NOT_FOUND',
statusCode: 404,
message: 'The path "/example" could not be resolved'
}
*/
throw err;
/*
NotFound: The path "/example" could not be resolved
at ...
*/
console.log(httperr.methodNotAllowed({allowed: ['GET', 'POST']}));
/*
{ [MethodNotAllowed]
title: 'Method Not Allowed',
name: 'MethodNotAllowed',
code: 'METHOD_NOT_ALLOWED',
statusCode: 405,
message: '',
allowed: ['GET', 'POST']
}
*/
err = new httperr.NotFound();
console.log(err);
/*
{ [NotFound]
title: 'Not Found',
name: 'NotFound',
code: 'NOT_FOUND',
statusCode: 404,
message: 'The path "/example" could not be resolved'
}
*/
console.log(err instanceof httperr.NotFound); // true
console.log(err instanceof httperr.notFound); // true
console.log(err instanceof httperr['404']); // true
console.log(err instanceof httperr.MethodNotAllowed); // false
console.log(err instanceof httperr.HttpError); // true
console.log(err instanceof Error); // true
`
Creates an Error object. The new keyword is optional.
Example:
`javascript`
new httperr.NotFound({message: 'That does not exist'});
If extra is given and is an object, its properties will be copied to the new Error object before config is applied.
If config is a string, it will be treated as config.message.
If config is an Error object, it will be treated as config.cause.
If config is an object, it can have the following properties:
This property is only available for 405 Method Not Allowed errors and can be used to populate the Allow header.
This property is only available for 429 Too Many Requests and 420 Enhance Your Calm (Twitter API) errors and can be used to populate the Retry-After header.
This property is only available for 449 Retry With (Microsoft) errors and can be used to populate the response status message.
This property is only available for 451 Redirect (Microsoft) errors and can be used to populate the proprietary X-MS-Location response header.
See above.
Example:
`javascript`
httperr404;
See above.
Example:
`javascript`
httperr.notFound({message: 'This link is dead, too'})
Takes the following arguments:
argument immediately after the error is created by the factory. Can be used to process additional error-specific configuration parameters.new httperr.HttpError(config)
The base type for all
httperr error types. You probably don't want to use this directly.httperr.HttpError::toObject([skip…]):Object
Returns a JSON serializable representation of this
httperr error (including any nested Error objects).Takes the following arguments:
$3
One or more strings or regular expressions against which the property names of
Error objects including httperr errors will be matched. Any matching properties will not be copied to the returned object.Example:
`js
var err = httperr.notFound('File Not Found');
console.log(err.toObject());
/*
{
name: 'NotFound',
code: 'NOT_FOUND',
title: 'Not Found',
statusCode: 404,
message: 'File Not Found',
stack: '…'
}
*/
console.log(err.toObject('stack', /^title$/));
/*
{
name: 'NotFound',
code: 'NOT_FOUND',
statusCode: 404,
message: 'File Not Found'
}
*/
``This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.