Skip to content
Prev Previous commit
clarify that tracestate.sentry should exist
  • Loading branch information
lobsterkatie committed Feb 17, 2021
commit dd450628aa24fa2f8096aab3e6fd5593cb57d735
6 changes: 5 additions & 1 deletion packages/core/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ export function transactionToSentryRequest(event: Event, api: API): SentryReques
// so we have to decode and reinflate it
let reinflatedTracestate;
try {
const encodedSentryValue = (tracestate?.sentry as string).replace('sentry=', '');
// Because transaction metadata passes through a number of locations (transactionContext, transaction, event during
// processing, event as sent), each with different requirements, all of the parts are typed as optional. That said,
// if we get to this point and either `tracestate` or `tracestate.sentry` are undefined, something's gone very wrong.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const encodedSentryValue = tracestate!.sentry!.replace('sentry=', '');
reinflatedTracestate = JSON.parse(base64ToUnicode(encodedSentryValue));
} catch (err) {
logger.warn(err);
Copy link
Contributor

Choose a reason for hiding this comment

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

This would log Cannot read property 'replace' of undefined for situations when there's simply no tracestate.sentry which might be confusing to the user.

Copy link
Member Author

@lobsterkatie lobsterkatie Feb 17, 2021

Choose a reason for hiding this comment

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

True. Originally I had it as const encodedSentryValue = tracestate!.sentry!.replace('sentry=', '');, since if we get to that point and there's no tracestate.sentry, things have gone real wrong, but then it was yelling at me about the !. But I think maybe I should change it back, to better communicate to the reader that it's not optional.

Or maybe I can write my types in a smarter way... In any case, will fix it one way or another.

UPDATE: Okay, tried with the types, but there are too many stages at which this or that property may (legitimately) not be defined, so I'm going with the !.

Expand Down