Skip to content

Commit a7b89bc

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

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
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+
virtualError,
8686
};
8787
triggerHandlers('xhr', handlerData);
8888
}

packages/browser/src/integrations/httpclient.ts

Lines changed: 11 additions & 10 deletions
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, virtualError } = 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 || virtualError);
303303
}, false);
304304
}
305305

@@ -316,6 +316,8 @@ function _wrapXHR(client: Client, options: HttpClientOptions): void {
316316
return;
317317
}
318318

319+
const { error, virtualError } = handlerData;
320+
319321
const xhr = handlerData.xhr as SentryWrappedXMLHttpRequest & XMLHttpRequest;
320322

321323
const sentryXhrData = xhr[SENTRY_XHR_DATA_KEY];
@@ -327,7 +329,7 @@ function _wrapXHR(client: Client, options: HttpClientOptions): void {
327329
const { method, request_headers: headers } = sentryXhrData;
328330

329331
try {
330-
_xhrResponseHandler(options, xhr, method, headers, handlerData.stack);
332+
_xhrResponseHandler(options, xhr, method, headers, error || virtualError);
331333
} catch (e) {
332334
DEBUG_BUILD && logger.warn('Error while extracting response event form XHR response', e);
333335
}
@@ -362,13 +364,12 @@ function _createEvent(data: {
362364
responseCookies?: Record<string, string>;
363365
requestHeaders?: Record<string, string>;
364366
requestCookies?: Record<string, string>;
365-
stacktrace?: string;
367+
error?: unknown;
366368
}): SentryEvent {
367369
const client = getClient();
368-
const virtualStackTrace = client && data.stacktrace ? data.stacktrace : undefined;
370+
const virtualStackTrace = client && data.error && data.error instanceof Error ? data.error.stack : undefined;
369371
// Remove the first frame from the stack as it's the HttpClient call
370372
const stack = virtualStackTrace && client ? client.getOptions().stackParser(virtualStackTrace, 0, 1) : undefined;
371-
372373
const message = `HTTP Client Error with status code: ${data.status}`;
373374

374375
const event: SentryEvent = {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ export interface HandlerDataXhr {
3232
xhr: SentryWrappedXMLHttpRequest;
3333
startTimestamp?: number;
3434
endTimestamp?: number;
35-
stack?: string;
35+
error?: unknown;
36+
// This is to be consumed by the HttpClient integration
37+
virtualError?: unknown;
3638
}
3739

3840
interface SentryFetchData {
@@ -57,7 +59,8 @@ export interface HandlerDataFetch {
5759
headers: WebFetchHeaders;
5860
};
5961
error?: unknown;
60-
stack?: string;
62+
// This is to be consumed by the HttpClient integration
63+
virtualError?: unknown;
6164
}
6265

6366
export interface HandlerDataDom {

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

Lines changed: 4 additions & 5 deletions
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+
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,
@@ -104,7 +103,7 @@ function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNat
104103
// it means the error, that was caused by your fetch call did not
105104
// have a stack trace, so the SDK backfilled the stack trace so
106105
// you can see which fetch call failed.
107-
error.stack = virtualStackTrace;
106+
error.stack = virtualError.stack;
108107
addNonEnumerableProperty(error, 'framesToPop', 1);
109108
}
110109

0 commit comments

Comments
 (0)