🚀 DAY 5 OF LAUNCH WEEK:Introducing Webhook Events for Alert Changes.Learn more →
Socket
Book a DemoInstallSign in
Socket

@elunic/express-error-handler

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@elunic/express-error-handler

A simple, minimalistic error handler middleware for Express

latest
Source
npmnpm
Version
0.2.7
Version published
Maintainers
1
Created
Source

@elunic/express-error-handler

Build Status

A simple, minimalistic error handler middleware for Express.

Support http-errors and has very partial support for some errors from common-errors.

Using this error handler will output nicely formatted JSON describing the type of error that occured and, optionally, the stack and error message.

If the handled error is an error from the http-errors module, or has a statusCode property, the status code (and, for http-errors, the correct type) will be included:

const httpErrors = require('http-errors');

app.get('/errorGet', (req, res) => {
    throw new httpErrors.NotFound('File not found');
})

Response body:

// STATUS CODE: 404
{
  error: {
    type: 'NotFoundError',
    message: 'File not found',
    stack: '<stack>'
  }
}

Table of Contents

Installation

$ npm install @elunic/express-error-handler

Usage

Example:

const app = require('express')();
const errorHandler = require('@elunic/express-error-handler')();

app.get('/get', (req, res) => {
    // ...
});

// ... further route handlers ...

// Use AFTER all routes/other middlewares whose errors you wish to handle.
// Routes/middlewares added after this point will not have their errors handled.
app.use(errorHandler);

app.listen(80);
Full output including stack and message

Example with full stack output (absolutely not recommended for production!):

const errorHandler = require('@elunic/express-error-handler')({
    full: true,
});
Callback

Example callback:

const errorHandler = require('@elunic/express-error-handler')({
    onError: (err) => {
        console.error('Express stack error', err);
    }
});

License

(The MIT License)

Copyright (c) 2018 elunic <wh@elunic.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

FAQs

Package last updated on 19 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts