New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

express-simple-errors

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-simple-errors

Simple error handling middleware for NodeJS

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Express Simple Errors

Codeship Status for kellyjandrews/express-simple-errors Jest Code Coverage semantic-release

Using Error Handling Middleware

import express from 'express';
import ErrorHandler, { errors } from 'express-simple-errors';

const app = express();
const errorHandler = new ErrorHandler();

app.use(errorHandler.middleware());

The middleware method will handle NotFound errors, all errors from routing, and then send the response. You can use these as one simple function, or you can use them individually.

import express from 'express';
import ErrorHandler, { errors } from 'express-simple-errors';

const app = express();
const errorHandler = new ErrorHandler();

app.use(errorHandler.handleNotFound()); //creates new NotFound() error
app.use(errorHandler.handleError()); // builds error response and stores it in res.locals.errors
app.use(errorHandler.sendResponse()); //sends error code and response

You could inject middleware after the handleError method. The original err object will be intact for you to consume before passing to sendResponse.

The stackTrace is included with the default handler method. You can turn this off when you initialize.

const errorHandler = new ErrorHandler({stackTrace: false});

Customizing the Response Object

You can modify the default handler, or create a custom one for more specific needs. The handlers are called based on the errors name property. All error classes included with this module are "Error", and the handler used will be "Default".

const defaultHandler = (err, stack) => {
  const res = {};
  res.status= err.name;
  res.message= err.message;
  res.code= err.code;
  if (stack) res.stackTrace= err.stack;
  return res;
};

Each handler will be passed two parameters - the err object, and the stackTrace flag.

To modify the Default handler, or create a custom handler, you can use the setHandler method.


errors.setHandler('Default', (err, stack) => {
  return {
    status: 'error',
    data: err.message
  }
});

errors.setHandler('Custom', (err, stack) => ('You have encountered an error.')});

Creating Error Responses

import { errors } from 'express-simple-errors';

// basic example
function foo(bar) {
  if !(bar) throw new errors.Conflict();
}

//custom message
function foo(bar) {
  if !(bar) throw new errors.Conflict('You are missing something!');
}

//used as route middleware functions
function checkUser(req, res, next) {
  try {
    const user = //do stuff to check for unser;
    if (!user) return next(new errors.NotFound('This user does not exist'));
    next();
  } catch(e) {
    next(e);
  }
}

router.route(/)
  .get(checkUser)

Supported Error Codes

  • 400 BadRequest
  • 401 Unauthorized
  • 403 Forbidden
  • 404 NotFound
  • 409 Conflict
  • 415 UnsupportedMediaType

Keywords

FAQs

Package last updated on 15 Feb 2017

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc