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 a7b89bc
Show file tree
Hide file tree
Showing 4 changed files with 21 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,
virtualError,
};
triggerHandlers('xhr', handlerData);
}
Expand Down
21 changes: 11 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, virtualError } = 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 || virtualError);
}, false);
}

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

const { error, virtualError } = handlerData;

const xhr = handlerData.xhr as SentryWrappedXMLHttpRequest & XMLHttpRequest;

const sentryXhrData = xhr[SENTRY_XHR_DATA_KEY];
Expand All @@ -327,7 +329,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, error || virtualError);
} catch (e) {
DEBUG_BUILD && logger.warn('Error while extracting response event form XHR response', e);
}
Expand Down Expand Up @@ -362,13 +364,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
7 changes: 5 additions & 2 deletions packages/core/src/types-hoist/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export interface HandlerDataXhr {
xhr: SentryWrappedXMLHttpRequest;
startTimestamp?: number;
endTimestamp?: number;
stack?: string;
error?: unknown;
// This is to be consumed by the HttpClient integration
virtualError?: unknown;
}

interface SentryFetchData {
Expand All @@ -57,7 +59,8 @@ export interface HandlerDataFetch {
headers: WebFetchHeaders;
};
error?: unknown;
stack?: string;
// This is to be consumed by the HttpClient integration
virtualError?: unknown;
}

export interface HandlerDataDom {
Expand Down
9 changes: 4 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
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 @@ -104,7 +103,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 a7b89bc

Please sign in to comment.