lukeed / lukeed/polka

Middleware Error handling incomplete

Open
#134 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

has fix
Dominant language
JavaScript
Stars
5.6k
Forks
176
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at packages/polka/index.js around line 75 and inspect the wrapper loop() method. Reproduce the synchronous and setImmediate middleware examples, then trace where the try/catch exits during asynchronous execution; done means errors thrown after async next are caught in both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.