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

refactor: use @fastify/error #123

Merged
merged 3 commits into from
Oct 20, 2024
Merged

Conversation

JacopoPatroclo
Copy link
Contributor

The aim of this PR is to address the issue #121.

I’ve migrated the errors emitted by this plugin to use @fastify/error, which has enabled the creation of a utility function to check if the error is actually coming from this plugin by using the error code. This makes it easier for consumers to implement specific error-handling logic around it.

Checklist

Comment on lines 457 to 458
t.assert.equal(errorHandler.mock.callCount(), 1)
t.assert.strictEqual(errorHandler.mock.calls[0].error, undefined, 'errorHandler function has emitted an unexpected error')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want it to be as close as possible to how users are supposed to use this utility function, but I’m not sure this is the best way to test this case.

index.js Outdated
const NoSchemaDefinedError = createError('FST_NO_SCHEMA_DEFINED_ERROR', 'No schema defined for %s')

function isResponseValidationError (error) {
return error ? error.code === FST_RESPONSE_VALIDATION_ERROR : false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to me like a reasonable way to identify the nature of the error.

@JacopoPatroclo JacopoPatroclo changed the title Refactor: using @fastify/error refactor: use @fastify/error Oct 18, 2024
Copy link

@toomuchdesign toomuchdesign left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good addition using @fastify/error.
As I said in my comments, I'd personally leave the discussion about the opportunity of introducing isResponseValidationError to another pr.

index.js Outdated
const ReplyValidationFailError = createError(FST_RESPONSE_VALIDATION_ERROR, '%s', 500)
const NoSchemaDefinedError = createError('FST_NO_SCHEMA_DEFINED_ERROR', 'No schema defined for %s')

function isResponseValidationError (error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest reduce the scope of this PR and evaluate the implementation of such utility in another PR.

example.mjs Outdated
Comment on lines 26 to 32
app.setErrorHandler((error, _request, reply) => {
if (isResponseValidationError(error)) {
return reply.status(500).send({ ok: false })
}
reply.status(200).send({ ok: true })
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beside my suggestion of implementing isResponseValidationError with another PR, I would not provide this example since a lot of users might copy paste it and assume it's a mandatory step to register the plugin.

index.js Outdated
Comment on lines 8 to 11
const FST_RESPONSE_VALIDATION_ERROR = 'FST_RESPONSE_VALIDATION_ERROR'

const ReplyValidationFailError = createError(FST_RESPONSE_VALIDATION_ERROR, '%s', 500)
const NoSchemaDefinedError = createError('FST_NO_SCHEMA_DEFINED_ERROR', 'No schema defined for %s')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to prefix all errors with FST_RESPONSE_VALIDATION.

This is just a naming suggestion to support my point:

  • FST_RESPONSE_VALIDATION_FAILED_VALIDATION
  • FST_RESPONSE_VALIDATION_SCHEMA_NOT_DEFINED

@JacopoPatroclo
Copy link
Contributor Author

Very good addition using @fastify/error. As I said in my comments, I'd personally leave the discussion about the opportunity of introducing isResponseValidationError to another pr.

I agree. I'll open a new PR with this feature if @mcollina is fine with exposing a new utility function for it.
Meanwhile I think this one is now ready to be merged in.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit 28d38b0 into fastify:master Oct 20, 2024
11 checks passed
@Fdawgs Fdawgs linked an issue Nov 4, 2024 that may be closed by this pull request
2 tasks
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

Successfully merging this pull request may close these issues.

Identify response body validation error on setErrorHandler hook
3 participants