koajs / koa

Expressive middleware for node.js using ES2017 async functions

Home Page:https://koajs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[feat] send json type content when call ctx.throw

yunnysunny opened this issue · comments

Describe the feature

Some time we want our server always reponse with json type, even if it throw custom error, for such code:

ctx.throw(403, 'need right access', {
      message: JSON.stringify({code: 1234, msg: 'you dont have right permission', userId: 'xxx'}),
      expose: true
})

But koa reponse all errors with text type, we can not change it.

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.

Guess you can put a middle there to catch the error and format the response before Koa. You can pass an object to ctx.throw() as any one of the arguments except first.