[feat] send json type content when call ctx.throw
yunnysunny opened this issue · comments
白一梓 commented
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.
Siavosh Kasravi commented
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.