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

feat(node): Add shouldHandleError option to fastify error handler #13198

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

Conversation

AbhiPrasad
Copy link
Member

resolves #13197

Aligns fastify error handler with the express one.

  1. Adds shouldHandleError to allow users to configure if errors should be captured
  2. Makes sure the default shouldHandleError only captures errors for 5xx status codes.

@AbhiPrasad AbhiPrasad requested review from mydea and a team August 2, 2024 14:41
@AbhiPrasad AbhiPrasad self-assigned this Aug 2, 2024
@AbhiPrasad AbhiPrasad requested review from lforst and removed request for a team August 2, 2024 14:41
@AbhiPrasad AbhiPrasad added the Integration: fastify Issues related to Fastify support for the Sentry Node SDK label Aug 2, 2024
Comment on lines +79 to +81
const shouldHandleError = options?.shouldHandleError || defaultShouldHandleError;

fastify.addHook('onError', async (request, reply, error) => {
Copy link

Choose a reason for hiding this comment

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

I think, since digging into this more, that my initial suggestion was incomplete - reply.status is probably going to be 200 here, and instead there is, I think, error.statusCode, according to the fastify-sentry plugin? https://github.com/immobiliare/fastify-sentry/blob/main/lib/base.js#L43-L46

Copy link
Contributor

Choose a reason for hiding this comment

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

This is not a fastify-sentry plugin, but an immobiliare plugin for fastify. Not official.

Reply and Error are not the same since the reply could have been sent before the error occurred.

* @param error Captured middleware error
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
shouldHandleError?(this: void, request: any, reply: any, error: Error): boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

It is better to use explicit types instead of any
import type { FastifyRequest, FastifyReply } from 'fastify';

Copy link
Member

Choose a reason for hiding this comment

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

for that we need to depend on the fastify package, which we cannot do in the generic node package.

Copy link
Contributor

Choose a reason for hiding this comment

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

// grumbling-mod-on
When someone was too lazy to take the framework packages out of the general node package :) Although this was proposed for version 8
// grumbling-mod-off

Perhaps, a dummy interface can be used. After all, only a little piece of it is used.
Or the generic type.

Copy link
Member

Choose a reason for hiding this comment

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

When someone was too lazy to take the framework packages out of the general node package :) Although this was proposed for version 8

It was a conscious decision not to do this.

Perhaps, a dummy interface can be used. After all, only a little piece of it is used.

Which is exactly as fragile as any, if not more, since it gives a false sense of security and disables all of the ts-eslint checking.

Copy link
Contributor

Choose a reason for hiding this comment

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

I have to disagree with you. The any can completely disable checks, but the interface will force you as a developer to add/edit the necessary entry. This means that some commands will not be called accidentally. As below, when the statusCode may be undefined.

Anyway, this is just my opinion. However, the number of open issues with the new version 8 makes it clear that it is possible. In any case, it is your conscious decision.

Copy link
Member

Choose a reason for hiding this comment

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

I think you're making wrong assumptions about how our linting works. When we define stuff as any, yes typescript will not check it anymore, however, eslint will still not let us access any fields without narrowing down and checking for their exact type first.

I encourage you to pull the repo and try it out.

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function defaultShouldHandleError(_request: any, reply: any, _error: Error): boolean {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return reply.statusCode >= 500;
Copy link
Contributor

Choose a reason for hiding this comment

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

statusCode can be undefined

Copy link
Contributor

Choose a reason for hiding this comment

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

// eslint-disable-next-line is used everywhere, it is not clear why you need eslint in the project then.

Choose a reason for hiding this comment

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

statusCode can be undefined

Could probably implement this similar to what's been done in the express integration.

@xr0master
Copy link
Contributor

@magnusburton Hi, there might be some communication issue since I'm not working on it.
Just looked at the changes.

@maxbeatty
Copy link

@AbhiPrasad thanks for taking this on!

Is there anything I can do to help get this shipped?

@maxbeatty
Copy link

@lforst is there anything I can do to help get this merged? Thanks!

@AbhiPrasad
Copy link
Member Author

I was just reminded by this PR - I had accidentally unsubscribed from it. Apologies about the delay, I'm pushing up changes now, let's get this to the finish line.

Sorry about the trouble everyone - totally my fault!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Integration: fastify Issues related to Fastify support for the Sentry Node SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sentry reports handled errors in Fastify integration
7 participants