ctx.throw() kills the server
See original GitHub issueThis server
const app = new (require('koa'))()
// first middleware
app.use(async (ctx, next) => {
next()
})
// second middleware
app.use(async ctx => {
ctx.throw(400)
})
app.listen(3000)
process.on('unhandledRejection', reason => {
throw reason
})
will blowup when request will get to the second middleware, and you will see it in the error
BadRequestError: Bad Request
at Object.throw (...\node_modules\koa\lib\context.js:91:23)
at .../server.js:10:3
at Generator.next (<anonymous>)
at step (...\server.js:3:191)
at ...\server.js:3:437
at ...\server.js:3:99
at app.use (.../server.js:9:1)
at dispatch (...\node_modules\koa-compose\index.js:44:32)
at next (...\node_modules\koa-compose\index.js:45:18)
at .../server.js:6:3
Error message blames request but the real problem is in the first middleware, where next()
doesn’t have await
before it. Lack of await
is probably a bug. The issue points are:
- Should it blowup the server?
- Every thing would be ok if second middleware would just
ctx.body = 'ok'
and no error or warning would appears. Reaction on absents ofawait
should not depend on consequent code. - Reaction should be as soon as possible. Seen the crash on
ctx.throw()
in the other part of a project is very confusing.
Edit: Possible solution could be to throw if next
has been called, middleware is resolved and next
is still pending with message says that this is the problem
Issue Analytics
- State:
- Created 7 years ago
- Comments:17 (12 by maintainers)
Top Results From Across the Web
java - Why channelInactive on server is called when the client ...
The client is killed by SIGKILL, so it should have no time to actively close this socket. The client application does not actively...
Read more >How to crash | Better world by better software - Gleb Bahmutov
When you have a long running Node.js server, it is bound to crash. Invalid input, lost connections, logic errors - there will be...
Read more >Extension kills Seq - Seq Documentation
started throwing exceptions yesterday (after updating HipChat server to 2.0.7). However, this caused Seq to fail and stop as well.
Read more >Shutdown a Spring Boot Application - Baeldung
Have a look at different ways to shut down a Spring Boot Application.
Read more >Node.js v4.2.6 Documentation
Example: sending server object; Example: sending socket object. child.stderr; child.stdin ... See also ChildProcess#kill() and ChildProcess#send() .
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
here is the problem, you need to await or return next(), when you dont do that, the promise chain is ended, just change to
lack of an await/return is a bug. awaiting or returning it should fix it. otherwise, it’s just a dangling promise