-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Document Default Error Responses #198
Conversation
Include documentation for 500 & 503 responses in default schema definition This ensures that they get included in the Open API documentation generated. - chore: update tests to match updated responses
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
index.js
Outdated
code: { type: 'string', description: 'Error code associated with the failing check', example: 'FST_UNDER_PRESSURE' }, | ||
error: { type: 'string', description: 'Error thrown during health check', example: 'Service Unavailable' }, | ||
message: { type: 'string', description: 'Error message to explain health check failure', example: 'Service Unavailable' }, | ||
statusCode: { type: 'number', description: 'Code representing the error. Currently matches the HTTP response code.', example: 503 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems odd. statuscode will always be HTTP respone code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Uzlopak This is updated ✅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of doubt that this so that much correct. It is good to add the 500 and 503 status codes to the schema. But the errors are thrown so the default error handler takes as we did not specify setErrorHandler for this plugin.
So if somebody changes the default errorHandler than the schema is not correct as the error handler will be generating a different response.
So we have to add an error Handler with setErrorHandler for this plugin, to ensure that the api contract is ensured even if the defaultErrorHandler is changed.
@Eomm |
Hi @Uzlopak, thank you for taking a look. I agree that this implementation only allows for the default responses. However, I think that this is okay, since an API spec is meant to define the "schema", and not the actual response that will be returned. Since there's currently no support for defining a custom error handler, the API is always returning the same payload in case of 500 & 503 errors. If we decide to add the error handler, we would then also update the schema definition to accept a custom payload (i.e. in the same way we're combining the custom success response in the What do you think? |
But afaik specifiying a plugin specific errorhandler is possible by using the setErrorHandler method. So we could specify a custom errorhandler and uphold the api contract even if the default error handler is modified. |
Oh, I get it now. If you define another error handler, then the dev should specify the schema by himself? |
I had a second look at it seems we have bugs in that part, before you even touched it. Line 107 in 26677ab
When The same here: Line 113 in 26677ab
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"}')
}) |
@mcollina |
If what is in this pr is ok, let's land it and then you can open a fresh issue with the fix. |
Include documentation for 500 & 503 responses in default schema definition
This ensures that they get included in the Open API documentation generated.
Demo / Screenshots
Notes to Reviewer(s)
Checklist
npm run test
andnpm run benchmark
and the Code of conduct