This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
97 lines (84 loc) · 2.9 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import * as Sentry from '@sentry/nextjs';
import { urlFromContext } from '@ifixit/helpers/nextjs';
import { GetServerSidePropsContext } from 'next';
import { Scope } from '@sentry/nextjs';
type Fetcher = typeof fetch;
type FetchMiddleware = (
fetcher: Fetcher,
shouldSkipRequest?: SkipRequestFn
) => Fetcher;
type FetcherParams = Parameters<Fetcher>;
type SkipRequestFn = (...args: FetcherParams) => boolean;
type CaptureWithContextFn = (
e: Error,
context: Parameters<Scope['setContext']>[1]
) => void;
const isClientSide = typeof window !== 'undefined';
const shouldIgnoreUserAgent =
isClientSide && /Yeti/.test(window.navigator.userAgent);
export const setSentryPageContext = (context: GetServerSidePropsContext) => {
Sentry.setTag('resolved_url', urlFromContext(context));
};
export const applySentryFetchMiddleware = () => {
if (isClientSide) {
window.fetch = withSentry(window.fetch, shouldSkipReporting);
} else {
global.fetch = withSentry(global.fetch, shouldSkipReporting);
}
};
export const reportException: CaptureWithContextFn = (e, context) => {
Sentry.captureException(e, (scope) => {
scope.setContext('request', context);
return scope;
});
};
const withSentry: FetchMiddleware =
(fetcher, shouldSkipRequest) => async (input, init) => {
if (shouldSkipRequest?.(input, init)) {
return fetcher(input, init);
}
const context = {
// Underscore sorts the resource first in Sentry's UI
_resource: input,
headers: init?.headers,
method: init?.method,
// Parse to pretty print GraphQL queries
body: init?.body ? JSON.parse(String(init?.body)) : undefined,
};
try {
const response = await fetcher(input, init);
if (
!shouldIgnoreUserAgent &&
response.status >= 400 &&
response.status !== 401 &&
response.status !== 404
) {
const msg = `fetch() HTTP error: ${response.status} ${response.statusText}`;
Sentry.captureException(new Error(msg), (scope) => {
scope.setContext('request', context);
return scope;
});
console.error(msg, context);
}
return response;
} catch (error) {
// We don't want to hear about network errors in Sentry
console.error(error, context);
throw error;
}
};
const shouldSkipReporting: SkipRequestFn = (input, init) => {
const url = getRequestUrl(input);
// We have custom logic in place for reporting errors only after React Query retries have failed,
// so we don't want to report errors for the cart API here.
return url.includes('/store/user/cart');
};
const getRequestUrl = (input: RequestInfo | URL) => {
if (typeof input === 'string') {
return input;
}
if (input instanceof URL) {
return input.href;
}
return input.url;
};