lukeed / polka

A micro web server so fast, it'll make you dance! :dancers:

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Middleware Error handling incomplete

kinsi55 opened this issue · comments

commented

As I saw in previous issues, v1 (Apparently) is supposed to catch errors thrown by middleware, however the current implementation wont be good enough for that.

In the examples, one is the first and two is a second middleware

Example handled by the current implementation:

function one(req, res, next) {
	next();
}
function two(req, res, next) {
	throw new Error("test");
}

Modified version that will not get caught:

function one(req, res, next) {
	setImmediate(next);
}
function two(req, res, next) {
	throw new Error("test");
}

As you can see, once a middleware finished in an async fashion, errors thrown by any middleware ran after it will not get caught, the reason for that is because the try/catch block will obviously be exit' since its an async operation.

To properly resolve this one would need to have the try/catch be within the wrapper loop() method.

Any update on this ? It feels awkward not being able to throw from a middleware