question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ctx.throw() kills the server

See original GitHub issue

This 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:

  1. Should it blowup the server?
  2. Every thing would be ok if second middleware would just ctx.body = 'ok' and no error or warning would appears. Reaction on absents of await should not depend on consequent code.
  3. 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:closed
  • Created 7 years ago
  • Comments:17 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
EduardoRFScommented, Jan 13, 2017
// first middleware
app.use(async (ctx, next) => {
  next()
})

here is the problem, you need to await or return next(), when you dont do that, the promise chain is ended, just change to

app.use(async (ctx, next) => {
  await next()
})
// or a simple arrow function
app.use((ctx, next) => next())
2reactions
jonathanongcommented, Jan 14, 2017

lack of an await/return is a bug. awaiting or returning it should fix it. otherwise, it’s just a dangling promise

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found