RestError
Provides the RestError class and associated factory methods.
Usage
$ npm install --save resterror
const RestError = ; { if val < 0 throw RestError; else return Math; } let err = RestError; console;// Error: 500 (Internal Server Error) Cannot connect to the database.
The badRequest
method and internalServerError
method are factory methods. No new
keyword is required. There is one factory method for each of the 400- and 500-series errors.
If you want to call the constructor yourself, you can:
{ if val < 0 throw 400 'Bad Request' util; else return Math; }
As you can see, using the factory method...
- is more readable,
- does not require the
new
keyword, - includes the descriptive text, and
- handles
util.format
arguments.
You can also pass in an Error
object...
let err = 'That record already exists.'; let conflict = RestError; console;// Error: 409 (Conflict) That record already exists.
This is useful when wrapping a library error into an error for your REST API.