ctx.throw doesn’t set ctx.status
See original GitHub issueI stumbled upon this issue when trying to throw an Unauthorized
error like:
...,
async (ctx, next) => {
if (!ctx.state.user) {
ctx.throw(401, 'Unauthorized');
}
ctx.state.payload.user = ctx.state.user;
return await next();
}
...
The response’ status code always was 404. According to Koa docs the use of ctx.throw
above is equivalent to:
...,
async (ctx, next) => {
if (!ctx.state.user) {
ctx.status = 401;
throw new Error('Unauthorized');
}
ctx.state.payload.user = ctx.state.user;
return await next();
}
...
But it isn’t. Setting status first and then throwing an error results in a correct 401 Unauthorized
response as expected while using ctx.throw
doesn’t.
Looking at the relevant code there is just an http-error
created and thrown but nothing indicates that ctx.status
is being set.
throw() {
throw createError.apply(null, arguments);
},
So it seems the response comes with status overridden by Koa’s default 404, no matter what status code you pass to ctx.throw
.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:10
Top Results From Across the Web
How to make koa ctx.throw() use application/json rather than ...
I have made a custom error handler for my koa app which works beautifully (except for one sticking point) - using ctx.throw() means...
Read more >Koa - next generation web framework for node.js
Helper method to throw an error with a .status property defaulting to 500 that will allow Koa to respond appropriately. The following combinations...
Read more >Context - ThinkJS
Context, an instance provided by Koa, it is used across the whole lifecycle of user request. It is used within middleware, controller and...
Read more >koajs/koa - Gitter
Hi, having a problem with setting ctx.body = a stream (sort of). Im using csv-stringify npm module's streams support, so my code is...
Read more >Sending Response with Koa - Medium
To send the response body, we can set the body attribute of ctx . ... We can send response status codes by setting...
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
try return ctx.throw(401, ‘Unauthorized’) ??
Tried already.
createError
– imported from the third-partyhttp-errors
module – is passed the arguments array of context’s.throw
method and itself doesn’t care in which order the arguments are passed, as it extracts the values depending on their type.Found the cause of my problem already. As soon as you
catch
the error in one of the middleware functions (as I do up the stream),onerror
is called withnull
and doesn’t go ahead with default error handling. So I have to assign the error’s status code toctx.status
myself. My bad! Thanks anyways. Closing.