Cannot understand the purpose of ctx.throw's behaviour
See original GitHub issueconst 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…
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:
- Created 6 years ago
- Comments:9
Top 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 >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
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 seterr.status
to a corresponded HTTP status code (defaulting to 500), as well as set some other properties. See this doc for more info.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.