1.0.0 • Published 1 year ago

express-async-error-patch v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

express-async-error-patch

let express support async middleware/router error catch

Usage

const express=require('express');
const app=express();

const asyncPatch=require('express-async-error-patch');
asyncPatch(app);
//done

Then you can handle async middleware errors in your normal error handle as same as sync middlewares.

app.get('/',async (req,res,next)=>{
	await new Promise((resolve,reject)=>{
		setTimeout(()=>{//do an async operation
			reject('test');//reject in 1 second
		},1000);
	});
});

app.use((err,req,res,next)=>{
	//the rejected promise error will be passed to this normal error handle
	console.log(err);// "test"
})

How

This patcher simply add an "await" and an "async" to the express Layer.prototype.handle_request function to let all router and middleware support async errors.

Notice

This patcher modifies just one express source function and have been tested in express 4.x, not sure will it work in lower or higher versions.