Skip to content

Commit 8929cd3

Browse files
authored
Switch back to passing error
1 parent 33a5bfa commit 8929cd3

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function instrumentXHR(): void {
8282
endTimestamp: timestampInSeconds() * 1000,
8383
startTimestamp,
8484
xhr: xhrOpenThisArg,
85-
stack: virtualError.stack,
85+
error: virtualError,
8686
};
8787
triggerHandlers('xhr', handlerData);
8888
}

packages/browser/src/integrations/httpclient.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function _fetchResponseHandler(
7070
requestInfo: RequestInfo,
7171
response: Response,
7272
requestInit?: RequestInit,
73-
stack?: string,
73+
error?: unknown,
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: stack,
93+
error,
9494
});
9595

9696
captureEvent(event);
@@ -129,7 +129,7 @@ function _xhrResponseHandler(
129129
xhr: XMLHttpRequest,
130130
method: string,
131131
headers: Record<string, string>,
132-
stack?: string,
132+
error?: unknown,
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: stack,
165+
error,
166166
});
167167

168168
captureEvent(event);
@@ -292,14 +292,14 @@ function _wrapFetch(client: Client, options: HttpClientOptions): void {
292292
return;
293293
}
294294

295-
const { response, args } = handlerData;
295+
const { response, args, error } = handlerData;
296296
const [requestInfo, requestInit] = args as [RequestInfo, RequestInit | undefined];
297297

298298
if (!response) {
299299
return;
300300
}
301301

302-
_fetchResponseHandler(options, requestInfo, response as Response, requestInit, handlerData.stack);
302+
_fetchResponseHandler(options, requestInfo, response as Response, requestInit, error);
303303
}, false);
304304
}
305305

@@ -327,7 +327,7 @@ function _wrapXHR(client: Client, options: HttpClientOptions): void {
327327
const { method, request_headers: headers } = sentryXhrData;
328328

329329
try {
330-
_xhrResponseHandler(options, xhr, method, headers, handlerData.stack);
330+
_xhrResponseHandler(options, xhr, method, headers, handlerData.error);
331331
} catch (e) {
332332
DEBUG_BUILD && logger.warn('Error while extracting response event form XHR response', e);
333333
}
@@ -362,13 +362,12 @@ function _createEvent(data: {
362362
responseCookies?: Record<string, string>;
363363
requestHeaders?: Record<string, string>;
364364
requestCookies?: Record<string, string>;
365-
stacktrace?: string;
365+
error?: unknown;
366366
}): SentryEvent {
367367
const client = getClient();
368-
const virtualStackTrace = client && data.stacktrace ? data.stacktrace : undefined;
368+
const virtualStackTrace = client && data.error && data.error instanceof Error ? data.error.stack : undefined;
369369
// Remove the first frame from the stack as it's the HttpClient call
370370
const stack = virtualStackTrace && client ? client.getOptions().stackParser(virtualStackTrace, 0, 1) : undefined;
371-
372371
const message = `HTTP Client Error with status code: ${data.status}`;
373372

374373
const event: SentryEvent = {

packages/core/src/types-hoist/instrument.ts

+1-2
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-
stack?: string;
35+
error?: unknown;
3636
}
3737

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

6362
export interface HandlerDataDom {

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,14 @@ function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNat
4848

4949
fill(GLOBAL_OBJ, 'fetch', function (originalFetch: () => void): () => void {
5050
return function (...args: any[]): void {
51-
// We capture the stack right here and not in the Promise error callback because Safari (and probably other
51+
// We capture the error right here and not in the Promise error callback because Safari (and probably other
5252
// browsers too) will wipe the stack trace up to this point, only leaving us with this file which is useless.
5353

5454
// NOTE: If you are a Sentry user, and you are seeing this stack frame,
5555
// it means the error, that was caused by your fetch call did not
5656
// have a stack trace, so the SDK backfilled the stack trace so
5757
// you can see which fetch call failed.
5858
const virtualError = new Error();
59-
const virtualStackTrace = virtualError.stack;
6059

6160
const { method, url } = parseFetchArgs(args);
6261
const handlerData: HandlerDataFetch = {
@@ -66,7 +65,8 @@ function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNat
6665
url,
6766
},
6867
startTimestamp: timestampInSeconds() * 1000,
69-
stack: virtualStackTrace,
68+
// // Adding the error to be able to fingerprint the failed fetch event in HttpClient instrumentation
69+
error: virtualError,
7070
};
7171

7272
// if there is no callback, fetch is instrumented directly
@@ -82,7 +82,6 @@ function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNat
8282
if (onFetchResolved) {
8383
onFetchResolved(response);
8484
} else {
85-
// Adding the stacktrace to be able to fingerprint the failed fetch event in HttpClient instrumentation
8685
triggerHandlers('fetch', {
8786
...handlerData,
8887
endTimestamp: timestampInSeconds() * 1000,
@@ -96,6 +95,7 @@ function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNat
9695
triggerHandlers('fetch', {
9796
...handlerData,
9897
endTimestamp: timestampInSeconds() * 1000,
98+
// Overriding the virtualError
9999
error,
100100
});
101101

@@ -104,7 +104,7 @@ function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNat
104104
// it means the error, that was caused by your fetch call did not
105105
// have a stack trace, so the SDK backfilled the stack trace so
106106
// you can see which fetch call failed.
107-
error.stack = virtualStackTrace;
107+
error.stack = virtualError.stack;
108108
addNonEnumerableProperty(error, 'framesToPop', 1);
109109
}
110110

0 commit comments

Comments
 (0)