Skip to content

Commit a810f03

Browse files
committed
renormalize only if stringifying goes wrong
1 parent 2fcf480 commit a810f03

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

packages/core/src/baseclient.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,6 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
428428
normalized.contexts.trace = event.contexts.trace;
429429
}
430430

431-
const { _experiments = {} } = this.getOptions();
432-
if (_experiments.ensureNoCircularStructures) {
433-
return normalize(normalized);
434-
}
435-
436431
return normalized;
437432
}
438433

packages/core/src/request.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Event, SdkInfo, SentryRequest, SentryRequestType, Session, SessionAggregates } from '@sentry/types';
2-
import { dsnToString } from '@sentry/utils';
2+
import { dsnToString, normalize } from '@sentry/utils';
33

44
import { APIDetails, getEnvelopeEndpointWithUrlEncodedAuth, getStoreEndpointWithUrlEncodedAuth } from './api';
55

@@ -61,8 +61,38 @@ export function eventToSentryRequest(event: Event, api: APIDetails): SentryReque
6161
// prevent this data from being sent to sentry
6262
delete event.sdkProcessingMetadata;
6363

64+
// This is a temporary hack in order to debug a serialization error - see
65+
// https://github.com/getsentry/sentry-javascript/issues/2809 and
66+
// https://github.com/getsentry/sentry-javascript/pull/4425. TL;DR: even though we normalize all events (which should
67+
// prevent this), something is causing `JSON.stringify` to throw a circular reference error.
68+
const withSDKInfo = sdkInfo ? enhanceEventWithSdkInfo(event, api.metadata.sdk) : event;
69+
let body;
70+
try {
71+
// 99.9% of events should get through just fine - no change in behavior for them
72+
body = JSON.stringify(withSDKInfo);
73+
} catch (err) {
74+
// Record data about the error without replacing original event data, then force renormalization
75+
event.tags = { ...event.tags, JSONStringifyError: true };
76+
event.extra = { ...event.extra, JSONStringifyError: err };
77+
try {
78+
body = JSON.stringify(normalize(withSDKInfo));
79+
} catch (newErr) {
80+
// At this point even renormalization hasn't worked, meaning something about the event data has gone very wrong.
81+
// Time to cut our losses and record only the new error. With luck, even in the problematic cases we're trying to
82+
// debug with this hack, we won't ever land here.
83+
const innerErr = newErr as Error;
84+
body = JSON.stringify({
85+
message: `JSON.stringify error after renormalization`,
86+
// setting `extra: { innerErr }` here for some reason results in an empty object, so unpack manually
87+
extra: { message: innerErr.message, stack: innerErr.stack },
88+
});
89+
}
90+
}
91+
6492
const req: SentryRequest = {
65-
body: JSON.stringify(sdkInfo ? enhanceEventWithSdkInfo(event, api.metadata.sdk) : event),
93+
// this is the relevant line of code before the hack, to make it easy to undo said hack once we've solved the mystery
94+
// body: JSON.stringify(sdkInfo ? enhanceEventWithSdkInfo(event, api.metadata.sdk) : event),
95+
body,
6696
type: eventType,
6797
url: useEnvelope
6898
? getEnvelopeEndpointWithUrlEncodedAuth(api.dsn, api.tunnel)

0 commit comments

Comments
 (0)