Skip to content

Commit

Permalink
Switch back to passing error
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan authored Nov 29, 2024
1 parent 33a5bfa commit 8929cd3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/browser-utils/src/instrument/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function instrumentXHR(): void {
endTimestamp: timestampInSeconds() * 1000,
startTimestamp,
xhr: xhrOpenThisArg,
stack: virtualError.stack,
error: virtualError,
};
triggerHandlers('xhr', handlerData);
}
Expand Down
19 changes: 9 additions & 10 deletions packages/browser/src/integrations/httpclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function _fetchResponseHandler(
requestInfo: RequestInfo,
response: Response,
requestInit?: RequestInit,
stack?: string,
error?: unknown,
): void {
if (_shouldCaptureResponse(options, response.status, response.url)) {
const request = _getRequest(requestInfo, requestInit);
Expand All @@ -90,7 +90,7 @@ function _fetchResponseHandler(
responseHeaders,
requestCookies,
responseCookies,
stacktrace: stack,
error,
});

captureEvent(event);
Expand Down Expand Up @@ -129,7 +129,7 @@ function _xhrResponseHandler(
xhr: XMLHttpRequest,
method: string,
headers: Record<string, string>,
stack?: string,
error?: unknown,
): void {
if (_shouldCaptureResponse(options, xhr.status, xhr.responseURL)) {
let requestHeaders, responseCookies, responseHeaders;
Expand Down Expand Up @@ -162,7 +162,7 @@ function _xhrResponseHandler(
// Can't access request cookies from XHR
responseHeaders,
responseCookies,
stacktrace: stack,
error,
});

captureEvent(event);
Expand Down Expand Up @@ -292,14 +292,14 @@ function _wrapFetch(client: Client, options: HttpClientOptions): void {
return;
}

const { response, args } = handlerData;
const { response, args, error } = handlerData;
const [requestInfo, requestInit] = args as [RequestInfo, RequestInit | undefined];

if (!response) {
return;
}

_fetchResponseHandler(options, requestInfo, response as Response, requestInit, handlerData.stack);
_fetchResponseHandler(options, requestInfo, response as Response, requestInit, error);
}, false);
}

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

try {
_xhrResponseHandler(options, xhr, method, headers, handlerData.stack);
_xhrResponseHandler(options, xhr, method, headers, handlerData.error);
} catch (e) {
DEBUG_BUILD && logger.warn('Error while extracting response event form XHR response', e);
}
Expand Down Expand Up @@ -362,13 +362,12 @@ function _createEvent(data: {
responseCookies?: Record<string, string>;
requestHeaders?: Record<string, string>;
requestCookies?: Record<string, string>;
stacktrace?: string;
error?: unknown;
}): SentryEvent {
const client = getClient();
const virtualStackTrace = client && data.stacktrace ? data.stacktrace : undefined;
const virtualStackTrace = client && data.error && data.error instanceof Error ? data.error.stack : undefined;
// Remove the first frame from the stack as it's the HttpClient call
const stack = virtualStackTrace && client ? client.getOptions().stackParser(virtualStackTrace, 0, 1) : undefined;

const message = `HTTP Client Error with status code: ${data.status}`;

const event: SentryEvent = {
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/types-hoist/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface HandlerDataXhr {
xhr: SentryWrappedXMLHttpRequest;
startTimestamp?: number;
endTimestamp?: number;
stack?: string;
error?: unknown;
}

interface SentryFetchData {
Expand All @@ -57,7 +57,6 @@ export interface HandlerDataFetch {
headers: WebFetchHeaders;
};
error?: unknown;
stack?: string;
}

export interface HandlerDataDom {
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/utils-hoist/instrument/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNat

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

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

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

// if there is no callback, fetch is instrumented directly
Expand All @@ -82,7 +82,6 @@ function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNat
if (onFetchResolved) {
onFetchResolved(response);
} else {
// Adding the stacktrace to be able to fingerprint the failed fetch event in HttpClient instrumentation
triggerHandlers('fetch', {
...handlerData,
endTimestamp: timestampInSeconds() * 1000,
Expand All @@ -96,6 +95,7 @@ function instrumentFetch(onFetchResolved?: (response: Response) => void, skipNat
triggerHandlers('fetch', {
...handlerData,
endTimestamp: timestampInSeconds() * 1000,
// Overriding the virtualError
error,
});

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

Expand Down

0 comments on commit 8929cd3

Please sign in to comment.