Skip to content

Commit

Permalink
fix: include partial GraphQL errors in breadcrumbs (#410)
Browse files Browse the repository at this point in the history
Co-authored-by: spawnia <[email protected]>
  • Loading branch information
repl-sarath and spawnia authored Dec 16, 2021
1 parent 6b759b9 commit a3d929d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/SentryLink.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ApolloError,
ApolloLink,
FetchResult,
NextLink,
Expand Down Expand Up @@ -62,6 +63,17 @@ export class SentryLink extends ApolloLink {
// We must have a breadcrumb if attachBreadcrumbs was set
(breadcrumb as GraphQLBreadcrumb).data.fetchResult = result;
}

if (
this.options.attachBreadcrumbs.includeError &&
result.errors &&
result.errors.length > 1
) {
// We must have a breadcrumb if attachBreadcrumbs was set
(breadcrumb as GraphQLBreadcrumb).data.error = new ApolloError({
graphQLErrors: result.errors,
});
}
}

originalObserver.next(result);
Expand Down
53 changes: 52 additions & 1 deletion tests/SentryLink.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ApolloLink, execute, ServerError } from '@apollo/client/core';
import {
ApolloError,
ApolloLink,
execute,
ServerError,
} from '@apollo/client/core';
import * as Sentry from '@sentry/browser';
import { Severity } from '@sentry/browser';
import { GraphQLError, parse } from 'graphql';
Expand Down Expand Up @@ -118,6 +123,52 @@ describe('SentryLink', () => {
});
});

it('should attach a breadcrumb with partial errors', (done) => {
const errors = [
new GraphQLError('partial failure'),
new GraphQLError('another failure'),
];
const result = {
data: { foo: true },
errors: errors,
};
const withPartialErrors = ApolloLink.from([
new SentryLink({
attachBreadcrumbs: { includeFetchResult: true, includeError: true },
}),
new ApolloLink(
() =>
new Observable((observer) => {
observer.next(result);
observer.complete();
}),
),
]);

execute(withPartialErrors, {
query: parse(`query PartialErrors { foo }`),
}).subscribe({
complete() {
Sentry.captureException(new Error());

const [report] = testkit.reports();
expect(report.breadcrumbs).toHaveLength(1);

const [breadcrumb] = report.breadcrumbs as Array<GraphQLBreadcrumb>;

expect(breadcrumb.category).toBe('graphql.query');
expect(breadcrumb.level).toBe(Severity.Error);
expect(breadcrumb.data.operationName).toBe('PartialErrors');
expect(breadcrumb.data.fetchResult).toBe(stringify(result));
expect(breadcrumb.data.error).toBe(
stringify(new ApolloError({ graphQLErrors: errors })),
);

done();
},
});
});

it('should mark results with errors with level error', (done) => {
const link = ApolloLink.from([
new SentryLink(),
Expand Down

0 comments on commit a3d929d

Please sign in to comment.