1.1.0 • Published 5 years ago
http-json-error-handler v1.1.0
http-json-error-handler
Installation
You can download http-json-error-handler
from NPM
$ npm install http-json-error-handler --save
then in your project require http-json-error-handler
const errorHandler = require('http-json-error-handler');
or GitHub
$ git clone https://github.com/lfurnielis/http-json-error-handler.git
Guide
const errorHandler = require('http-json-error-handler');
const express = require('express');
const app = express();
// Your defined middlewares
...
...
// Your defined routes
app.get('/foo', (req, res, next) => {
const error = new Error('Missing field(s): foo');
error.status = 400;
error.code = 10401;
next(error);
});
// HTTP error handler
app.use(errorHandler({
stderr: true // Default: false
}));
Options
{
stderr: false // Log errors using console.error(), Default: false
stackerr: false // Log error.stack using console.debug() only for statusCode >= 500, Default: false
}
Definition of a "Error"
The error could contain the following fields:
Error Key | Purpose |
---|---|
message | Error details. |
status | HTTP status code for response. Default: 500 (Internal Server Error). |
code | Error code. Default: same as "status". |
HTTP JSON Error Example
{
"error": {
"code": 10401,
"message": "BAD REQUEST",
"details": "Missing field(s): foo"
}
}