|
1 | 1 | import { Event, SdkInfo, SentryRequest, SentryRequestType, Session, SessionAggregates } from '@sentry/types';
|
2 |
| -import { dsnToString } from '@sentry/utils'; |
| 2 | +import { dsnToString, normalize } from '@sentry/utils'; |
3 | 3 |
|
4 | 4 | import { APIDetails, getEnvelopeEndpointWithUrlEncodedAuth, getStoreEndpointWithUrlEncodedAuth } from './api';
|
5 | 5 |
|
@@ -61,8 +61,38 @@ export function eventToSentryRequest(event: Event, api: APIDetails): SentryReque
|
61 | 61 | // prevent this data from being sent to sentry
|
62 | 62 | delete event.sdkProcessingMetadata;
|
63 | 63 |
|
| 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 | + |
64 | 92 | 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, |
66 | 96 | type: eventType,
|
67 | 97 | url: useEnvelope
|
68 | 98 | ? getEnvelopeEndpointWithUrlEncodedAuth(api.dsn, api.tunnel)
|
|
0 commit comments