Skip to content

Commit 05e3351

Browse files
committed
Pass stacktrace string instead of error.
1 parent d58da1c commit 05e3351

File tree

4 files changed

+37
-48
lines changed

4 files changed

+37
-48
lines changed

packages/browser-utils/src/instrument/xhr.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,14 @@ type WindowWithXhr = Window & { XMLHttpRequest?: typeof XMLHttpRequest };
1515
* Use at your own risk, this might break without changelog notice, only used internally.
1616
* @hidden
1717
*/
18-
export function addXhrInstrumentationHandler(
19-
handler: (data: HandlerDataXhr) => void,
20-
httpClientInstrumented?: boolean,
21-
): void {
18+
export function addXhrInstrumentationHandler(handler: (data: HandlerDataXhr) => void): void {
2219
const type = 'xhr';
2320
addHandler(type, handler);
24-
maybeInstrument(type, () => instrumentXHR(httpClientInstrumented));
21+
maybeInstrument(type, instrumentXHR);
2522
}
2623

2724
/** Exported only for tests. */
28-
export function instrumentXHR(httpClientInstrumented: boolean = false): void {
25+
export function instrumentXHR(): void {
2926
if (!(WINDOW as WindowWithXhr).XMLHttpRequest) {
3027
return;
3128
}
@@ -85,7 +82,7 @@ export function instrumentXHR(httpClientInstrumented: boolean = false): void {
8582
endTimestamp: timestampInSeconds() * 1000,
8683
startTimestamp,
8784
xhr: xhrOpenThisArg,
88-
error: httpClientInstrumented ? virtualError : undefined,
85+
stack: virtualError.stack,
8986
};
9087
triggerHandlers('xhr', handlerData);
9188
}

packages/browser/src/integrations/httpclient.ts

+17-21
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function _fetchResponseHandler(
7070
requestInfo: RequestInfo,
7171
response: Response,
7272
requestInit?: RequestInit,
73-
error?: unknown,
73+
stack?: string,
7474
): void {
7575
if (_shouldCaptureResponse(options, response.status, response.url)) {
7676
const request = _getRequest(requestInfo, requestInit);
@@ -90,7 +90,7 @@ function _fetchResponseHandler(
9090
responseHeaders,
9191
requestCookies,
9292
responseCookies,
93-
stacktrace: error instanceof Error ? error.stack : undefined,
93+
stacktrace: stack,
9494
});
9595

9696
captureEvent(event);
@@ -129,7 +129,7 @@ function _xhrResponseHandler(
129129
xhr: XMLHttpRequest,
130130
method: string,
131131
headers: Record<string, string>,
132-
error?: unknown,
132+
stack?: string,
133133
): void {
134134
if (_shouldCaptureResponse(options, xhr.status, xhr.responseURL)) {
135135
let requestHeaders, responseCookies, responseHeaders;
@@ -162,7 +162,7 @@ function _xhrResponseHandler(
162162
// Can't access request cookies from XHR
163163
responseHeaders,
164164
responseCookies,
165-
stacktrace: error instanceof Error ? error.stack : undefined,
165+
stacktrace: stack,
166166
});
167167

168168
captureEvent(event);
@@ -287,24 +287,20 @@ function _wrapFetch(client: Client, options: HttpClientOptions): void {
287287
return;
288288
}
289289

290-
addFetchInstrumentationHandler(
291-
handlerData => {
292-
if (getClient() !== client) {
293-
return;
294-
}
290+
addFetchInstrumentationHandler(handlerData => {
291+
if (getClient() !== client) {
292+
return;
293+
}
295294

296-
const { response, args } = handlerData;
297-
const [requestInfo, requestInit] = args as [RequestInfo, RequestInit | undefined];
295+
const { response, args } = handlerData;
296+
const [requestInfo, requestInit] = args as [RequestInfo, RequestInit | undefined];
298297

299-
if (!response) {
300-
return;
301-
}
298+
if (!response) {
299+
return;
300+
}
302301

303-
_fetchResponseHandler(options, requestInfo, response as Response, requestInit, handlerData.error);
304-
},
305-
false,
306-
true,
307-
);
302+
_fetchResponseHandler(options, requestInfo, response as Response, requestInit, handlerData.stack);
303+
}, false);
308304
}
309305

310306
/**
@@ -331,11 +327,11 @@ function _wrapXHR(client: Client, options: HttpClientOptions): void {
331327
const { method, request_headers: headers } = sentryXhrData;
332328

333329
try {
334-
_xhrResponseHandler(options, xhr, method, headers, handlerData.error);
330+
_xhrResponseHandler(options, xhr, method, headers, handlerData.stack);
335331
} catch (e) {
336332
DEBUG_BUILD && logger.warn('Error while extracting response event form XHR response', e);
337333
}
338-
}, true);
334+
});
339335
}
340336

341337
/**

packages/core/src/utils-hoist/instrument/fetch.ts

+14-19
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ type FetchResource = string | { toString(): string } | { url: string };
2121
export function addFetchInstrumentationHandler(
2222
handler: (data: HandlerDataFetch) => void,
2323
skipNativeFetchCheck?: boolean,
24-
httpClientInstrumented?: boolean,
2524
): void {
2625
const type = 'fetch';
2726
addHandler(type, handler);
28-
maybeInstrument(type, () => instrumentFetch(undefined, skipNativeFetchCheck, httpClientInstrumented));
27+
maybeInstrument(type, () => instrumentFetch(undefined, skipNativeFetchCheck));
2928
}
3029

3130
/**
@@ -42,17 +41,23 @@ export function addFetchEndInstrumentationHandler(handler: (data: HandlerDataFet
4241
maybeInstrument(type, () => instrumentFetch(streamHandler));
4342
}
4443

45-
function instrumentFetch(
46-
onFetchResolved?: (response: Response) => void,
47-
skipNativeFetchCheck: boolean = false,
48-
httpClientInstrumented: boolean = false,
49-
): void {
44+
function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNativeFetchCheck: boolean = false): void {
5045
if (skipNativeFetchCheck && !supportsNativeFetch()) {
5146
return;
5247
}
5348

5449
fill(GLOBAL_OBJ, 'fetch', function (originalFetch: () => void): () => void {
5550
return function (...args: any[]): void {
51+
// We capture the stack right here and not in the Promise error callback because Safari (and probably other
52+
// browsers too) will wipe the stack trace up to this point, only leaving us with this file which is useless.
53+
54+
// NOTE: If you are a Sentry user, and you are seeing this stack frame,
55+
// it means the error, that was caused by your fetch call did not
56+
// have a stack trace, so the SDK backfilled the stack trace so
57+
// you can see which fetch call failed.
58+
const virtualError = new Error();
59+
const virtualStackTrace = virtualError.stack;
60+
5661
const { method, url } = parseFetchArgs(args);
5762
const handlerData: HandlerDataFetch = {
5863
args,
@@ -61,27 +66,18 @@ function instrumentFetch(
6166
url,
6267
},
6368
startTimestamp: timestampInSeconds() * 1000,
69+
stack: virtualStackTrace,
6470
};
6571

6672
// if there is no callback, fetch is instrumented directly
6773
// if httpClientInstrumented is true, we are in the HttpClient instrumentation
6874
// and we may need to capture the stacktrace even when the fetch promise is resolved
69-
if (!onFetchResolved && !httpClientInstrumented) {
75+
if (!onFetchResolved) {
7076
triggerHandlers('fetch', {
7177
...handlerData,
7278
});
7379
}
7480

75-
// We capture the stack right here and not in the Promise error callback because Safari (and probably other
76-
// browsers too) will wipe the stack trace up to this point, only leaving us with this file which is useless.
77-
78-
// NOTE: If you are a Sentry user, and you are seeing this stack frame,
79-
// it means the error, that was caused by your fetch call did not
80-
// have a stack trace, so the SDK backfilled the stack trace so
81-
// you can see which fetch call failed.
82-
const virtualError = new Error();
83-
const virtualStackTrace = virtualError.stack;
84-
8581
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
8682
return originalFetch.apply(GLOBAL_OBJ, args).then(
8783
async (response: Response) => {
@@ -93,7 +89,6 @@ function instrumentFetch(
9389
...handlerData,
9490
endTimestamp: timestampInSeconds() * 1000,
9591
response,
96-
error: httpClientInstrumented ? virtualError : undefined,
9792
});
9893
}
9994

packages/types/src/instrument.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface HandlerDataXhr {
3232
xhr: SentryWrappedXMLHttpRequest;
3333
startTimestamp?: number;
3434
endTimestamp?: number;
35-
error?: unknown;
35+
stack?: string;
3636
}
3737

3838
interface SentryFetchData {
@@ -57,6 +57,7 @@ export interface HandlerDataFetch {
5757
headers: WebFetchHeaders;
5858
};
5959
error?: unknown;
60+
stack?: string;
6061
}
6162

6263
export interface HandlerDataDom {

0 commit comments

Comments
 (0)