Skip to content

ramenvr/express-async-patch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

express-async-patch

Express doesn't handle async errors by default unfortunately, here's a quick and easy solution!

MIT Licensed - Have fun and use however you like!

Installation

npm i -S https://gitlab.com/Eeveeboo/express-async-patch.git
# or
yarn add https://gitlab.com/Eeveeboo/express-async-patch.git

Usage

import express from 'express';
import expressAsyncPatch from './express-async-patch';

// The patch needs to be called BEFORE you create a router or app
expressAsyncPatch((err,req,res,next)=>{
    // This is how I like to handle errors, you can implement any function you like here.
    if(req.path.indexOf("/api") == 0)
        // Send JSON error response
        res.status(501).json({
            status: false,
            message: err.message,
            data: err.stack
        });
    else
        // Send Express error response
        next(err);
});
const app = express();

// Normally this would just result in the webpage hanging
app.use(async (req,res)=>{
    throw new Error("Async Error Test.");
});

About

Fork of GitLab original

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published