Skip to content

fix: run onError hooks after error handler #6146

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jean-michelet
Copy link
Member

Fixes: #6135

I'd like some feedback on this, as I'm not sure we understand how we should handle errors.
There's this test that documents that the onError hook should be called before error handlers :

test('onError hook nested', async t => {

But the doc says otherwise:
lifecycle

Comment on lines +815 to +817
() => {
reply[kReplyIsRunningOnErrorHook] = false
}
Copy link
Member Author

@jean-michelet jean-michelet May 29, 2025

Choose a reason for hiding this comment

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

Here, we could access the error if an onError hook callback throw, but we need to check that the response hasn't already been sent.
Perhaps we could also issue a warning?

@jean-michelet jean-michelet requested a review from a team May 30, 2025 08:04
Copy link
Member

@Eomm Eomm left a comment

Choose a reason for hiding this comment

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

We need to asses the error first, I tried it and the beheviour is there since v4.

The first version that I tried that run onHook later is the v3.29.0.

@jean-michelet
Copy link
Member Author

jean-michelet commented May 31, 2025

We need to asses the error first

What do you mean?

Also, even if we currently document this incorrectly and should consider it a bug fix, in practice, users might have implemented their application based on the current behavior (apparently present for years), so I wonder if this fix should be considered a breaking change.

@jean-michelet jean-michelet force-pushed the fix/error-handler-before-onerror-hook branch from a249404 to cce1f28 Compare May 31, 2025 15:04
@Eomm
Copy link
Member

Eomm commented Jun 1, 2025

What do you mean?

I mean that I would like to understand:

  • when was it introduced?
  • why did we not spot it earlier?
  • is the wanted behaviour correct?

Given that:

  • the onError hook is designed to be an informative hook, where the user should not use reply.send() in this context
  • the custom error handler is the function that should choose what to do with the error

So, if we fix this bug:

  1. we are turning off the onError function calls that are after the custom error handler
  2. other?

Given 1., are we breaking some monitoring tools or database module? (since we use in postgres, rate-limit, multipart etc...
https://github.com/search?q=org%3Afastify+%22onError%22+-path%3Atest%2F+-path%3Atypes%2F+language%3AJavaScript+-repo%3Afastify%2Ffastify&type=code

so I wonder if this fix should be considered a breaking change.

I tend to agree

@gurgunday
Copy link
Member

why did we not spot it earlier?

This is indeed interesting, I might look into this if I can find some time

@Eomm Eomm added discussion Issues or PRs with this label will never stale notable-change A change that should be explicitly outlined in release notes and migration guides labels Jun 9, 2025
@jean-michelet
Copy link
Member Author

@fastify/core

Maybe we should close this PR and update the doc, wdyt?

Copy link
Member

@gurgunday gurgunday left a comment

Choose a reason for hiding this comment

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

I still find this interesting, I want to investigate it

@jean-michelet
Copy link
Member Author

jean-michelet commented Jun 16, 2025

The issue I have is that the documentation is incorrect.
This can be frustrating, as users generally trust documentations and may end up wasting time and energy due to misleading information.

At the same time, the current implementation is likely difficult to change, and as @Eomm pointed out, the hook is mainly informational.

@gurgunday
Copy link
Member

Yeah let's update the docs in any case

I'll approve a PR that does that

Maybe keep this one open a little longer so I can have a look

emicovi added a commit to emicovi/fastify that referenced this pull request Jun 18, 2025
- Update docs to reflect that onError hook runs BEFORE error handlers
- Fix Reply Lifecycle diagram to show correct execution order
- Update TypeScript documentation and type definitions
- Resolves discrepancy between docs and actual implementation

Fixes: fastify#6135
Related: fastify#6146
Fdawgs pushed a commit that referenced this pull request Jun 23, 2025
* docs: fix onError hook execution order documentation

- Update docs to reflect that onError hook runs BEFORE error handlers
- Fix Reply Lifecycle diagram to show correct execution order
- Update TypeScript documentation and type definitions
- Resolves discrepancy between docs and actual implementation

Fixes: #6135
Related: #6146

* docs: simplify onError hook documentation text

Address review feedback from jean-michelet:
- Remove redundant explanations about hook purpose
- Simplify execution order descriptions
- Make documentation more concise while preserving key information
@jean-michelet
Copy link
Member Author

@gurgunday

Any progress in your investigation?

@gurgunday
Copy link
Member

Unfortunately I couldn't make any progress whatsoever on this - had so little time!

Feel free to take it over

@jean-michelet
Copy link
Member Author

Should we close this @fastify/core?
I don't think we can/should go back

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Issues or PRs with this label will never stale notable-change A change that should be explicitly outlined in release notes and migration guides
Projects
None yet
Development

Successfully merging this pull request may close these issues.

onError hook is called before setErrorHandler
3 participants