Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

DEVPROD-356: Add Sentry grouping #2262

Merged
merged 4 commits into from
Feb 20, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/components/ErrorHandling/Sentry.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ErrorResponse } from "@apollo/client/link/error";
import {
captureException,
ErrorBoundary as SentryErrorBoundary,
getCurrentHub,
init,
Replay,
setTag,
withScope,
} from "@sentry/react";
import type { Scope, SeverityLevel } from "@sentry/react";
Expand Down Expand Up @@ -38,14 +40,35 @@ const initializeSentry = () => {

const isInitialized = () => !!getCurrentHub().getClient();

type ErrorMetadata = {
gqlErr?: ErrorResponse["graphQLErrors"][0];
operationName?: ErrorResponse["operation"]["operationName"];
variables?: ErrorResponse["operation"]["variables"];
Copy link
Contributor

Choose a reason for hiding this comment

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

These are all actually typed from
import { GraphQLError } from "graphql"; since they are properties of the GraphQL request and not specific to Apollo.

Copy link
Contributor Author

@sophstad sophstad Feb 16, 2024

Choose a reason for hiding this comment

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

This is the correct typing of Apollo's onError handlers if I'm not mistaken: https://github.com/apollographql/apollo-client/blob/17ccc42825d0b0355212f3ac823780edb467ea77/src/link/error/index.ts#L28

GraphQLError doesn't include operationName or variables: https://github.com/graphql/graphql-js/blob/9c90a23dd430ba7b9db3d566b084e9f66aded346/src/error/GraphQLError.ts#L41

These are Apollo-specific since they come from Apollo links; the GraphQL request is just a part of the data in the graphQLErrors field.

};

const sendError = (
err: Error,
severity: SeverityLevel,
metadata?: { [key: string]: any },
metadata?: ErrorMetadata,
Copy link
Contributor

Choose a reason for hiding this comment

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

This typing is technically broken since

const reportError = (
passes a generic { [key:string]: any } so this type is never actually used.

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 also not flexible enough to allow for any metadata to be passed in once the typing in reportError is fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the typing in errorReporting.ts. If you want strict typing I think we should just go all the way and update this type as more metadata fields are added rather than allow the type to be flexible.

) => {
withScope((scope) => {
setScope(scope, { level: severity, context: metadata });

const { gqlErr, operationName } = metadata ?? {};
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we improve the type safety on these by adding gqlErr and operationName as option fields in the metadata struct on line 46.


// Add additional sorting for GraphQL errors
if (operationName) {
// A custom fingerprint allows for more intelligent grouping
const fingerprint = [operationName];
if (gqlErr?.path && Array.isArray(gqlErr.path)) {
fingerprint.push(...gqlErr.path);
}
scope.setFingerprint(fingerprint);

// Apply tag, which is a searchable/filterable property
setTag("operationName", operationName);
}

captureException(err);
});
};
Expand Down
Loading