Repository navigation
Expand file tree
/
Copy pathErrorBoundary.tsx
More file actions
135 lines (119 loc) · 3.3 KB
/
Copy pathErrorBoundary.tsx
File metadata and controls
135 lines (119 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"use client"
import { XIcon, WarningIcon } from "@phosphor-icons/react/ssr"
import * as Sentry from "@sentry/nextjs"
import { useTranslations } from "next-intl"
import { useState } from "react"
import { ErrorBoundary as ErrorBoundaryComp } from "react-error-boundary"
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
import { Button } from "@/components/ui/button"
import { isDevelopment } from "@/lib/general-helpers"
function ErrorBoundaryFallback({
error,
resetErrorBoundary,
customErrorTitle,
hideReset,
showErrorMessage,
}: {
readonly error: Error & { digest?: string }
readonly resetErrorBoundary: () => void
readonly customErrorTitle?: string
readonly hideReset?: boolean
readonly showErrorMessage?: boolean
}) {
const [hidden, setHidden] = useState(false)
const t = useTranslations("errors.global")
if (hidden) {
return null
}
const handleTryAgain = () => {
// Attempt to recover by trying to re-render the segment
resetErrorBoundary()
}
const isDev = isDevelopment()
return (
<Alert variant="destructive" className="relative">
<WarningIcon className="s-4" weight="bold" />
<AlertTitle>{customErrorTitle ?? t("invalidContent")}</AlertTitle>
<AlertDescription>
{(showErrorMessage || isDev) && (
<p className="mt-1 text-sm text-black">{error.message}</p>
)}
{isDev && (
<div className="mt-2 w-full overflow-x-auto bg-gray-100 p-3 text-xs">
<pre>{error.stack?.split("\n").slice(0, 5).join("\n")}</pre>
</div>
)}
{!hideReset && (
<Button
type="button"
size="sm"
variant="destructive"
onClick={handleTryAgain}
className="mt-2"
>
{t("tryAgain")}
</Button>
)}
</AlertDescription>
<span className="absolute top-2 right-2 block">
<XIcon
className="size-4 cursor-pointer"
weight="bold"
onClick={() => setHidden(true)}
/>
</span>
</Alert>
)
}
export function ErrorBoundary({
children,
hideReset,
hideFallback,
customErrorTitle,
showErrorMessage,
onReset,
onError,
}: {
readonly children: React.ReactNode
readonly hideReset?: boolean
readonly hideFallback?: boolean
readonly customErrorTitle?: string
readonly showErrorMessage?: boolean
readonly onReset?: () => void
readonly onError?: (
error: Error & { digest?: string },
info: { componentStack?: string | null }
) => void
}) {
const handleError = (
error: Error & { digest?: string },
info: { componentStack?: string | null; digest?: string | null }
) => {
const digest = error.digest ?? info.digest
if (digest === "NEXT_NOT_FOUND" || digest?.includes("404")) {
throw error
}
if (onError) {
onError(error, info)
}
Sentry.captureException(error)
}
return (
<ErrorBoundaryComp
fallbackRender={(props) =>
hideFallback ? null : (
<ErrorBoundaryFallback
{...props}
hideReset={hideReset}
customErrorTitle={customErrorTitle}
showErrorMessage={showErrorMessage}
/>
)
}
onError={handleError}
onReset={onReset}
>
{children}
</ErrorBoundaryComp>
)
}