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

bug when setting custom error handler #206

Open
Uzlopak opened this issue Oct 26, 2023 · 0 comments
Open

bug when setting custom error handler #206

Uzlopak opened this issue Oct 26, 2023 · 0 comments

Comments

@Uzlopak
Copy link
Contributor

Uzlopak commented Oct 26, 2023

          I had a second look at it seems we have bugs in that part, before you even touched it.

schema: Object.assign({}, opts.exposeStatusRoute.routeSchemaOpts, {

When opts.exposeStatusRoute.routeSchemaOpts is in Object.assign before our changes, than it means we can not overwrite the error statuscodes.

The same here:

opts.exposeStatusRoute.routeResponseSchemaOpts

We could set a custom error handler like this:

    fastify.setErrorHandler((err, request, reply, done) => {
      reply.status(err.statusCode)
        .send({
          code: err.code,
          message: err.message
        })
      done()
    })

a potential unit test would be:

test('check error handler', async t => {
  t.plan(1)
  const fastify = Fastify({ exposeStatusRoute: true })

  fastify.register(underPressure, {
    healthCheck: () => {
      throw new Error('Arbitrary Error')
    },    
    exposeStatusRoute: {
      routeOpts: {
        logLevel: 'silent'
      }
    }
  })

  t.equal((await fastify.inject().get('/').end()).body, '{"code":"FST_UNDER_PRESSURE","message":"Service Unavailable"}')
})

Originally posted by @Uzlopak in #198 (comment)

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

1 participant