Skip to content

Commit be5df1c

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

File tree

2 files changed

+31
-7
lines changed

2 files changed

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

0 commit comments

Comments
 (0)