express-async-error-patch

1.0.0 • Public • Published

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.

Package Sidebar

Install

npm i express-async-error-patch

Weekly Downloads

37

Version

1.0.0

License

MIT

Unpacked Size

3.24 kB

Total Files

4

Last publish

Collaborators

  • luojia