koajs / koajs/compose

Abnormal middleware errors occurred and KOA could not catch error events. How about the following changes

Open
#146 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1k
Forks
144
PR merge metrics
No merged PRs in 30d

Description

```
function compose (middleware) {
if(!Array.isArray(middleware)) throw TypeError('Middleware stack must be an array!')
for (const fn of middleware) {
if(typeof fn !== 'function') throw new TypeError('Middleware must be composed of functions!')
}

let index = -1 //

/**
* @param {Object} context
* @return {Promise}
* @api compose
*/
return function (context, next) {
return new Promise((resolve, reject) => {
function dispatch (i) { //
if (i <= index) {
console.warn('[warn]next() called multiple times')
return //reject(new Error('next() called multiple times'))
}

index=i
let fn=middleware[i]
if(i===middleware.length) fn=next //
if(!fn) return resolve()

try {
return resolve(fn(context,dispatch.bind(null,i+1)))
} catch (error) {
return reject(error)
}

}
dispatch(0)
})

}

}
```

Contributor guide

Open the contributing guide

Research direction

Start with the compose function shown in the issue and reproduce the abnormal middleware error behavior. No repository files or tests are named, and the issue does not define the expected error-handling behavior beyond saying that Koa cannot catch error events, so the acceptance criteria need clarification before implementation.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.