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 response

See original GitHub issue

Hey guys, I saw your response for ctx.throw not supporting a custom response in #194. however I noticed, that it only errors and forces the body when you throw a 500. If I use ctx.throw(400, JSON.stringify({some: "data"})) it works just fine.

Any chance you would re-consider allowing custom responses for 500 errors too?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
sorahncommented, Nov 26, 2016

What @AliasT suggested worked.

I didn’t open that one, I just responded to it. But I wanted koa to return a 500 error, have ctx.body be JSON so that the service that is consuming it could print useful errors to the customer.

I Want to use ctx.throw because that stops the request immediatly and doesn’t traverse through any other middleware.

5reactions
AliasTcommented, Nov 25, 2016

this might work:

ctx.throw(500, JSON.stringify({some: "data"}), { expose: true })

but it seems like this feature is just designed for user-level error https://github.com/koajs/koa/blob/master/lib/context.js#L70

Read more comments on GitHub >

github_iconTop Results From Across the Web

koa/context.md at master · koajs/koa - GitHub
Many of the context's accessors and methods simply delegate to their ctx.request or ctx.response equivalents for convenience, and are otherwise identical.
Read more >
How to make koa ctx.throw() use application/json rather than ...
throw () means any stacktrace is emitted to the server logs and also any custom error message is sent in the response. The...
Read more >
Koa - next generation web framework for node.js
ctx.response. A Koa Response object. ctx.state. The recommended namespace for passing information through middleware and to your frontend views.
Read more >
Koa cheatsheet - Devhints
The one-page guide to Koa: usage, examples, links, snippets, and more.
Read more >
Sending Response with Koa - Medium
To send the response body, we can set the body attribute of ctx . For instance, we can send a response body as...
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