Skip to content

Commit 369cdad

Browse files
committed
Enhance error handling in root.tsx ErrorBoundary with user-facing error mapping and deduplication of error descriptions
1 parent 5ac0d36 commit 369cdad

File tree

1 file changed

+49
-8
lines changed

1 file changed

+49
-8
lines changed

apps/cyberstorm-remix/app/root.tsx

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -618,18 +618,36 @@ export function ErrorBoundary() {
618618
console.log(error);
619619
}
620620
const isResponseError = isRouteErrorResponse(error);
621+
let payload: UserFacingErrorPayload | null = null;
622+
623+
if (isResponseError) {
624+
payload = parseUserFacingErrorPayload(error.data);
625+
}
626+
627+
const statusCode = payload?.status ?? (isResponseError ? error.status : 500);
628+
const headline =
629+
payload?.headline ??
630+
(isResponseError
631+
? error.statusText || `Error ${error.status}`
632+
: "Internal server error");
633+
634+
const fallbackDescription =
635+
isResponseError && typeof error.data === "string"
636+
? dedupeDescription(headline, error.data)
637+
: undefined;
638+
639+
const description = payload?.description ?? fallbackDescription;
640+
const showDefaultFlavor = !payload && !isResponseError;
621641
return (
622642
<div className="error">
623-
<div
624-
className="error__glitch"
625-
data-text={isResponseError ? error.status : 500}
626-
>
627-
<span>{isResponseError ? error.status : 500}</span>
643+
<div className="error__glitch" data-text={statusCode}>
644+
<span>{statusCode}</span>
628645
</div>
629646
<div className="error__description">
630-
{isResponseError ? error.data : "Internal server error"}
647+
<strong>{headline}</strong>
648+
{description ? <div>{description}</div> : null}
631649
</div>
632-
{!isResponseError && (
650+
{showDefaultFlavor && (
633651
<div className="error__flavor">
634652
Beep boop. Server something error happens.
635653
</div>
@@ -638,6 +656,26 @@ export function ErrorBoundary() {
638656
);
639657
}
640658

659+
function dedupeDescription(
660+
headline: string,
661+
description: string | undefined
662+
): string | undefined {
663+
if (!description) {
664+
return undefined;
665+
}
666+
667+
const trimmedDescription = description.trim();
668+
if (!trimmedDescription) {
669+
return undefined;
670+
}
671+
672+
if (trimmedDescription.toLowerCase() === headline.trim().toLowerCase()) {
673+
return undefined;
674+
}
675+
676+
return trimmedDescription;
677+
}
678+
641679
// Temporary solution for implementing ads
642680
// REMIX TODO: Move to dynamic html
643681
function AdsInit() {
@@ -736,7 +774,10 @@ function getCommunityBreadcrumb(
736774
</span>
737775
}
738776
>
739-
<Await resolve={communityPage.data.community}>
777+
<Await
778+
resolve={communityPage.data.community}
779+
errorElement={<NimbusAwaitErrorElement />}
780+
>
740781
{(resolvedValue) => {
741782
let label = undefined;
742783
let icon = undefined;

0 commit comments

Comments
 (0)