Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 tasks done
yunnysunny opened this issue Mar 28, 2023 · 1 comment
Closed
2 tasks done

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

yunnysunny opened this issue Mar 28, 2023 · 1 comment

Comments

@yunnysunny
Copy link

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.
@siakc
Copy link

siakc commented Dec 22, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants