Skip to content

Simplify SentryLink #483

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 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
102 changes: 45 additions & 57 deletions src/SentryLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import type { SeverityLevel } from '@sentry/core';
import { Observable } from 'zen-observable-ts';

import { GraphQLBreadcrumb, makeBreadcrumb } from './breadcrumb';
import { makeBreadcrumb } from './breadcrumb';
import { FullOptions, SentryLinkOptions, withDefaults } from './options';
import {
attachBreadcrumbToSentry,
Expand All @@ -29,9 +29,9 @@ export class SentryLink extends ApolloLink {
operation: Operation,
forward: NextLink,
): Observable<FetchResult> | null {
const options = this.options;
const { options } = this;

if (!(options.shouldHandleOperation?.(operation) ?? true)) {
if (options.shouldHandleOperation && !options.shouldHandleOperation(operation)) {
return forward(operation);
}

Expand All @@ -43,10 +43,12 @@ export class SentryLink extends ApolloLink {
setFingerprint(operation);
}

const attachBreadcrumbs = options.attachBreadcrumbs;
const breadcrumb = attachBreadcrumbs
? makeBreadcrumb(operation, options)
: undefined;
const { attachBreadcrumbs } = options;
if (!attachBreadcrumbs) {
return forward(operation);
}

const breadcrumb = makeBreadcrumb(operation, options);

// While this could be done more simplistically by simply subscribing,
// wrapping the observer in our own observer ensures we get the results
Expand All @@ -56,72 +58,58 @@ export class SentryLink extends ApolloLink {
return new Observable<FetchResult>((originalObserver) => {
const subscription = forward(operation).subscribe({
next: (result) => {
if (attachBreadcrumbs) {
// We must have a breadcrumb if attachBreadcrumbs was set
(breadcrumb as GraphQLBreadcrumb).level = severityForResult(result);
breadcrumb.level = severityForResult(result);

if (attachBreadcrumbs.includeFetchResult) {
// We must have a breadcrumb if attachBreadcrumbs was set
(breadcrumb as GraphQLBreadcrumb).data.fetchResult = result;
}
if (attachBreadcrumbs.includeFetchResult) {
breadcrumb.data.fetchResult = result;
}

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

originalObserver.next(result);
},
complete: () => {
if (attachBreadcrumbs) {
attachBreadcrumbToSentry(
operation,
// We must have a breadcrumb if attachBreadcrumbs was set
breadcrumb as GraphQLBreadcrumb,
options,
);
}
attachBreadcrumbToSentry(
operation,
breadcrumb,
options,
);

originalObserver.complete();
},
error: (error) => {
if (attachBreadcrumbs) {
// We must have a breadcrumb if attachBreadcrumbs was set
(breadcrumb as GraphQLBreadcrumb).level = 'error';

let scrubbedError;
if (isServerError(error)) {
const { result, response, ...rest } = error;
scrubbedError = rest;

if (attachBreadcrumbs.includeFetchResult) {
// We must have a breadcrumb if attachBreadcrumbs was set
(breadcrumb as GraphQLBreadcrumb).data.fetchResult = result;
}
} else {
scrubbedError = error;
}
breadcrumb.level = 'error';

let scrubbedError;
if (isServerError(error)) {
const { result, response, ...rest } = error;
scrubbedError = rest;

if (attachBreadcrumbs.includeError) {
// We must have a breadcrumb if attachBreadcrumbs was set
(breadcrumb as GraphQLBreadcrumb).data.error = scrubbedError;
if (attachBreadcrumbs.includeFetchResult) {
breadcrumb.data.fetchResult = result;
}
} else {
scrubbedError = error;
}

attachBreadcrumbToSentry(
operation,
// We must have a breadcrumb if attachBreadcrumbs was set
breadcrumb as GraphQLBreadcrumb,
options,
);
if (attachBreadcrumbs.includeError) {
breadcrumb.data.error = scrubbedError;
}

attachBreadcrumbToSentry(
operation,
breadcrumb,
options,
);

originalObserver.error(error);
},
});
Expand Down