Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions packages/utils/src/envelope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TextEncoderInternal,
} from '@sentry/types';

import { normalize } from './normalize';
import { dropUndefinedKeys } from './object';

/**
Expand Down Expand Up @@ -67,9 +68,24 @@ export function serializeEnvelope(envelope: Envelope, textEncoder?: TextEncoderI
}

for (const item of items) {
const [itemHeaders, payload] = item as typeof items[number];
const [itemHeaders, payload] = item;

append(`\n${JSON.stringify(itemHeaders)}\n`);
append(typeof payload === 'string' || payload instanceof Uint8Array ? payload : JSON.stringify(payload));

if (typeof payload === 'string' || payload instanceof Uint8Array) {
append(payload);
} else {
let stringifiedPayload: string;
try {
stringifiedPayload = JSON.stringify(payload);
} catch (e) {
// In case, despite all our efforts to keep `payload` circular-dependency-free, `JSON.strinify()` still
// fails, we try again after normalizing it again with infinite normalization depth. This of course has a
// performance impact but in this case a performance hit is better than throwing.
stringifiedPayload = JSON.stringify(normalize(payload));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should throw an error to Sentry as well here? Maybe attach the event_id so folks can report back about why this is happening (like we can inspect the fully normalized payload).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to log, what parts of the payload contained the circular reference, just so we can close these gaps. We discussed some ideas:

  • Capture an additional exception here with the circ-dep path and let users report back to us.
  • Set a specific field in extra and show a warning on the Sentry product to report back to us.
  • Set a specific field in the payload with the circ-dep path and log that field in relay.

}
append(stringifiedPayload);
}
}

return typeof parts === 'string' ? parts : concatBuffers(parts);
Expand Down
15 changes: 15 additions & 0 deletions packages/utils/test/envelope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ describe('envelope', () => {
Uint8Array.from([7, 8, 9, 10, 11, 12]),
]);
});

it("doesn't throw when being passed a an envelope that contains a circular item payload", () => {
const chicken: { egg?: unknown } = {};
const egg = { chicken };
chicken.egg = chicken;

const env = createEnvelope<EventEnvelope>({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' }, [
[{ type: 'event' }, egg],
]);

const serializedEnvelope = serializeEnvelope(env, new TextEncoder());
const [, , serializedBody] = serializedEnvelope.toString().split('\n');

expect(serializedBody).toBe('{"chicken":{"egg":"[Circular ~]"}}');
});
});

describe('addItemToEnvelope()', () => {
Expand Down