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

ctx.throw(Boom.badRequest(error)) #10

Closed
erwan690 opened this issue Feb 28, 2019 · 4 comments
Closed

ctx.throw(Boom.badRequest(error)) #10

erwan690 opened this issue Feb 28, 2019 · 4 comments

Comments

@erwan690
Copy link

ctx.throw(Boom.badRequest(error))

returned

Content-Type | text/plain

how to change to

Content-Type | application/json

@niftylettuce
Copy link
Collaborator

niftylettuce commented Feb 28, 2019 via email

@erwan690
Copy link
Author

i do but it still return text
Content-Type | text/plain

this is set

app = new Koa()
    app.context.onerror = errorHandler
    app.context.api = true
    app.use(cors())
      .use(bodyParser())
      .use(methodOverride())
      .use(compression())
      .use(koa404Handler)
      .use(swaggerMiddleware)
      .use(router.routes())

this on the route

router.get('/400', ctx => {
    ctx.api = true
    ctx.throw(Boom.badRequest())
  })

this respond is json
image

but
Content-Type is text/plain
image

@jozsi
Copy link

jozsi commented Mar 22, 2019

Same here. I am setting the api flag on the app context:
app.context.api = true;

@niftylettuce
Copy link
Collaborator

I've fixed this in v2.0.0. Please install this version:

npm install koa-better-error-handler@latest

or

yarn add koa-better-error-handler@latest

This version has a patch in place so that if ctx.api = true, by default the content type returned in responses will be application/json instead of text/plain, as you had noted in your bug report.

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

No branches or pull requests

3 participants