Giter Site home page Giter Site logo

simple-async-errors's Introduction

Simple Async Errors

A simple library for handling async errors in controllers. This makes it easier to create global error handlers and will stop UnhandledPromiseRejection errors from showing up anymore.

Installation

Install Async Errors using npm:

npm install --save simple-async-errors

Usage

Async functions are supported and will call a catcher for you when they throw:

const asyncErrorWrapper = require('simple-async-errors');

module.exports = asyncErrorWrapper({
  index: (req, res) => res.send('yay!')
});

Async functions on static and non-static memebers of classes can be used as well:

const asyncErrorWrapper = require('simple-async-errors');

class TestController {
  async test(req, res) {
    res.send('yay');
  }

  static async test2(req, res) {
    res.send('yay');
  }
}

module.exports = asyncErrorWrapper(TestController);

They can also all be rolled up into deeply nested objects and still work! Like when you have an index route file that exports all routes as an object. Doing this will allow you to only run the async handler in a single location instead of spread throughout all of your route files:

const asyncErrorWrapper = require('simple-async-errors');

// Error handler for all requests
const errorHandler = (req, res) => (err) => {
  switch (err.name) {
    case 'UnauthorizedError':
      return res.status(401).json({
        error: { message: err.message }
      });

    default:
      debug(err.name);
      debug(err);

      return res.status(500).json({
        error: config.debugMode ? err : 'Unknown Error'
      });
  }
};

module.exports = asyncErrorWrapper({
  TestController: require('./test'),
  HomeController: require('./home'),
}, errorHandler);

simple-async-errors's People

Contributors

nikkomiu avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.