Handle response validation error#1328
Conversation
Call onErrorHook if response serialization throw an error
lib/reply.js
Outdated
| } catch (error) { | ||
| onErrorHook(this, error, onSendHook) | ||
| return | ||
| } |
There was a problem hiding this comment.
I think a better approach would be to reset the sent to false and the let the exception bubble up to the user.
There was a problem hiding this comment.
Do you suggest to reset sent in the catch block (instead of call onErrorHook) or set sent = true just a little before to call onSendHook?
The first approach keeps the performance degradation, so the second one appears better to me, but I haven't a deep knowledge of Fastify codebase and I cannot evaluate if this could have some side effects...
There was a problem hiding this comment.
This change is not really related to performance, but correctness. We should not be invoking here onErrorHook, but rather bubble the error to the user. We'll focus on optimizing after that change.
Note that the call to this[kReplySerializer](payload) would need to be protected in a similar way.
There was a problem hiding this comment.
I changed the way sent is set, to cover all cases without try-catches. Benchmark updated.
| const fastify = Fastify() | ||
|
|
||
| fastify.get('/', { schema: { response } }, function (req, reply) { | ||
| reply.code(200).send({ work: 'actor' }) |
There was a problem hiding this comment.
I think the exception should bubble up here. This is a developer error.
There was a problem hiding this comment.
I'm not sure I like this, in my opinion send should never throw. Otherwise, we are "suggesting" users to always put send inside a try-catch block.
There was a problem hiding this comment.
@delvedor this is a philosophical decision 😄 We can hide the error and handle it with onErrorHook (see my first commit) or we can makes the error bubble up (my second commit).
1st approach
We wouldn't handle a developer error, like @mcollina say, so Fastify shouldn't hide it. This isn't a common situation and surely caused by a bug. It have to be fixed by developer and not hidden try-catching everything.
2nd approach
By the way, a big project has surely a good amount of bugs, and it's not nice that app crashes due to a missing field. A 500 error is really welcome in production. That's my case, I find this bug due to a malformed record in my MongoDB that caused the validation failure.
What if we let developer decide? With a validator option or something similar. I'm just saying, I'm not sure this match the project philosophy.
There was a problem hiding this comment.
I had a chant with @mcollina, and I agree with him.
We must be consistent with our current behavior if you are using a standard function as handler reply.send should throw and you should handle the exception, while if you are using an async handler, we should handle the exception internally.
There was a problem hiding this comment.
an async handler will catch the exception, we have to do nothing about it, just throw.
|
I think we can move |
@mcollina I'm moving It looks like it's not necessary to set |
|
Is it passing the tests? |
The best solution is to move the |
|
I see no difference in performance between this and master. |
delvedor
left a comment
There was a problem hiding this comment.
Can you also add a test with promises?
|
@delvedor Do you mean something like this? fastify.get('/', { schema: { response } }, async function (req, reply) {
return Promise.resolve()
.then(() => reply.code(200).send({ work: 'actor' }))
.catch((error) => reply.code(500).send(error))
}) |
|
Something like the following should work. // note that I'm not using an async function
fastify.get('/', { schema: { response } }, function (req, reply) {
return Promise.resolve({ work: 'actor' })
} |
|
@delvedor done! 😄 |
* Handle response validation error Call onErrorHook if response serialization throw an error * Remove try catch and set sent just before call hooks * Move sent true inside hooks * Add test with promise for response validation error
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Fix for issue #1325 : Call onErrorHook if response serialization throw an error
My branch
master
After fix, performance are 97,2% of actual.
I'm not sure of try-catch solution, but I think it's the less impacting way on the code base. I'm open to better solutions.