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 doesn’t set ctx.status

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:10

github_iconTop GitHub Comments

3reactions
chanlitocommented, Aug 30, 2016

try return ctx.throw(401, ‘Unauthorized’) ??

1reaction
oliverwehncommented, Aug 30, 2016

Tried already. createError – imported from the third-party http-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 with null and doesn’t go ahead with default error handling. So I have to assign the error’s status code to ctx.status myself. My bad! Thanks anyways. Closing.

Read more comments on GitHub >

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

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