Skip to content

Commit 30860c8

Browse files
committed
Error handle patched logic of span.end
1 parent bf7633a commit 30860c8

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

packages/react/src/reactrouter-compat-utils/instrumentation.tsx

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -761,34 +761,39 @@ function patchSpanEnd(
761761
const originalEnd = span.end.bind(span);
762762

763763
span.end = function patchedEnd(...args) {
764-
// Only update if the span source is not already 'route' (i.e., it hasn't been parameterized yet)
765-
const spanJson = spanToJSON(span);
766-
const currentSource = spanJson.data?.[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
767-
if (currentSource !== 'route') {
768-
// Last chance to update the transaction name with the latest route info
769-
// Use the live global allRoutes Set to include any lazy routes loaded after patching
770-
const currentAllRoutes = Array.from(allRoutes);
771-
const branches = _matchRoutes(
772-
currentAllRoutes.length > 0 ? currentAllRoutes : routes,
773-
location,
774-
basename,
775-
) as unknown as RouteMatch[];
776-
777-
if (branches) {
778-
const [name, source] = resolveRouteNameAndSource(
779-
location,
780-
routes,
764+
try {
765+
// Only update if the span source is not already 'route' (i.e., it hasn't been parameterized yet)
766+
const spanJson = spanToJSON(span);
767+
const currentSource = spanJson.data?.[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
768+
if (currentSource !== 'route') {
769+
// Last chance to update the transaction name with the latest route info
770+
// Use the live global allRoutes Set to include any lazy routes loaded after patching
771+
const currentAllRoutes = Array.from(allRoutes);
772+
const branches = _matchRoutes(
781773
currentAllRoutes.length > 0 ? currentAllRoutes : routes,
782-
branches,
774+
location,
783775
basename,
784-
);
776+
) as unknown as RouteMatch[];
785777

786-
// Only update if we have a valid name
787-
if (name && (spanType === 'pageload' || !spanJson.timestamp)) {
788-
span.updateName(name);
789-
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
778+
if (branches) {
779+
const [name, source] = resolveRouteNameAndSource(
780+
location,
781+
routes,
782+
currentAllRoutes.length > 0 ? currentAllRoutes : routes,
783+
branches,
784+
basename,
785+
);
786+
787+
// Only update if we have a valid name
788+
if (name && (spanType === 'pageload' || !spanJson.timestamp)) {
789+
span.updateName(name);
790+
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
791+
}
790792
}
791793
}
794+
} catch (error) {
795+
// Silently catch errors to ensure span.end() is always called
796+
DEBUG_BUILD && debug.warn(`Error updating span details before ending: ${error}`);
792797
}
793798

794799
return originalEnd(...args);

0 commit comments

Comments
 (0)