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.

Cannot understand the purpose of ctx.throw's behaviour

See original GitHub issue
const Koa = require('koa')

const app = new Koa()

app.use(async ctx => {
  ctx.throw('aaa')
})
app.listen(4000)

This will throw a real javascript runtime error, and gives requester a response InternalServerError: aaa.

So, the code below looks weird:

try {
    const {username, password} = ctx.request.body
    const user = getUserFromDB(username)
    if (user.password === password) {
      ctx.status = 200
    } else {
      ctx.throw(401, 'Invalid username or password')
      // this information should return to requester directly, but in fact, it was caught by below `catch`, and return a 500 error to the requester.
    }
  } catch (e) {
    ctx.throw(500, 'some other code error')
  }

And there is anther situation: When I use supertest to test my server, when run into ctx.throw(500, 'blabla'), it will shows on test result , it is very ugly…

image

I known this is cause by I use the koa instance in my test case, but any other best practice? Thanks.

I think , ctx.throw not throw a real javascript runtime error will help above two situation

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
jfurfarocommented, Oct 4, 2017

ctx.throw() throws an error to let the error event handler take control, which by default is set by Koa in order to gracefully respond (which includes any thrown error).

The ctx.throw() method is a convenience that will set err.status to a corresponded HTTP status code (defaulting to 500), as well as set some other properties. See this doc for more info.

1reaction
kristiandupontcommented, Jan 6, 2020

For others stumbling upon this, if you are setting your code to a non-official HTTP status code, Koa will sneakily set it to 500: https://github.com/koajs/koa/blob/d48d88ee17b780c02123e6d657274cab456e943e/lib/context.js#L150

This tripped me up for a bit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding Context in Golang. Demystifying the ctx variable.
If the context is cancelled within two seconds, the ctx.Done() channel receives an empty struct. The second case will be executed and the...
Read more >
KOA-Views Suddenly Stop Working after First Route
At first I thought this was a problem being caused by ctx.redirect because they would always fail and be followed by the broken...
Read more >
Resolver mapping template changelog - AWS AppSync
If you do specify a version inside a function request mapping template, the version value will be overridden by the value of the...
Read more >
Koa - next generation web framework for node.js
app.use(async ctx => { ctx; // is the Context ctx.request; // is a Koa Request ... For example ctx.throw(400, 'name required') is equivalent...
Read more >
Handling errors in Durable Functions - Azure | Microsoft Learn
Any exception that is thrown in an activity function is marshaled back to the orchestrator function and thrown as a FunctionFailedException ...
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