|
1
|
import { ExclamationCircle, SquareTwoStack } from "@medusajs/icons" |
|
2
|
import { toast, Text, IconButton, Tooltip } from "@medusajs/ui" |
|
3
|
import copy from "copy-to-clipboard" |
|
4
|
import { useTranslation } from "react-i18next" |
|
5
|
import { Navigate, useLocation, useRouteError } from "react-router-dom" |
|
6
|
|
|
7
|
import { isFetchError } from "../../../lib/is-fetch-error" |
|
8
|
|
|
9
|
export const ErrorBoundary = () => { |
|
10
|
const error = useRouteError() |
|
11
|
const location = useLocation() |
|
12
|
const { t } = useTranslation() |
|
13
|
|
|
14
|
let code: number | null = null |
|
15
|
|
|
16
|
if (isFetchError(error)) { |
|
17
|
if (error.status === 401) { |
|
18
|
return <Navigate to="/login" state={{ from: location }} replace /> |
|
19
|
} |
|
20
|
|
|
21
|
code = error.status ?? null |
|
22
|
} |
|
23
|
|
|
24
|
// In development mode, show detailed error information |
|
25
|
const isDevelopment = process.env.NODE_ENV === "development" |
|
26
|
const errorMessage = error instanceof Error ? error.message : String(error) |
|
27
|
const errorStack = error instanceof Error ? error.stack : undefined |
|
28
|
|
|
29
|
/** |
|
30
|
* Log error in development mode. |
|
31
|
* |
|
32
|
* react-router-dom will sometimes swallow the error, |
|
33
|
* so this ensures that we always log it. |
|
34
|
*/ |
|
35
|
if (process.env.NODE_ENV === "development") { |
|
36
|
console.error(error) |
|
37
|
const fileDetails = errorStack?.split("\n")[1]?.trim() |
|
38
|
const filename = |
|
39
|
fileDetails?.match(/([^/\\?]+)(?:\?[^:]*)?:\d+:\d+\)/)?.[1] || "unknown" |
|
40
|
const lineno = |
|
41
|
fileDetails?.match(/(?:\?[^:]*)?:(\d+):\d+\)/)?.[1] || "unknown" |
|
42
|
const colno = |
|
43
|
fileDetails?.match(/(?:\?[^:]*)?:\d+:(\d+)\)/)?.[1] || "unknown" |
|
44
|
window.parent.postMessage( |
|
45
|
{ |
|
46
|
data: { |
|
47
|
type: "RUNTIME_ERROR", |
|
48
|
level: "error", |
|
49
|
message: errorMessage, |
|
50
|
stack: errorStack, |
|
51
|
logged_at: new Date().toISOString(), |
|
52
|
filename, |
|
53
|
lineno, |
|
54
|
colno, |
|
55
|
}, |
|
56
|
}, |
|
57
|
"*" |
|
58
|
) |
|
59
|
} |
|
60
|
|
|
61
|
let title: string |
|
62
|
let message: string |
|
63
|
|
|
64
|
switch (code) { |
|
65
|
case 400: |
|
66
|
title = t("errorBoundary.badRequestTitle") |
|
67
|
message = t("errorBoundary.badRequestMessage") |
|
68
|
break |
|
69
|
case 404: |
|
70
|
title = t("errorBoundary.notFoundTitle") |
|
71
|
message = t("errorBoundary.notFoundMessage") |
|
72
|
break |
|
73
|
case 500: |
|
74
|
title = t("errorBoundary.internalServerErrorTitle") |
|
75
|
message = t("errorBoundary.internalServerErrorMessage") |
|
76
|
break |
|
77
|
default: |
|
78
|
title = t("errorBoundary.defaultTitle") |
|
79
|
message = t("errorBoundary.defaultMessage") |
|
80
|
break |
|
81
|
} |
|
82
|
|
|
83
|
const handleCopyError = () => { |
|
84
|
const errorText = `Error: ${errorMessage}\n\n${ |
|
85
|
errorStack || "No stack trace available" |
|
86
|
}` |
|
87
|
const success = copy(errorText) |
|
88
|
|
|
89
|
if (success) { |
|
90
|
toast.success("Error details copied to clipboard") |
|
91
|
} else { |
|
92
|
toast.error("Failed to copy error details") |
|
93
|
} |
|
94
|
} |
|
95
|
|
|
96
|
return ( |
|
97
|
<div className="flex size-full min-h-[calc(100vh-57px-24px)] items-center justify-center"> |
|
98
|
<div className="flex flex-col gap-y-6"> |
|
99
|
<div className="text-ui-fg-subtle flex flex-col items-center gap-y-3"> |
|
100
|
<ExclamationCircle /> |
|
101
|
<div className="flex flex-col items-center justify-center gap-y-1"> |
|
102
|
<Text size="small" leading="compact" weight="plus"> |
|
103
|
{title} |
|
104
|
</Text> |
|
105
|
<Text |
|
106
|
size="small" |
|
107
|
className="text-ui-fg-muted text-balance text-center" |
|
108
|
> |
|
109
|
{message} |
|
110
|
</Text> |
|
111
|
</div> |
|
112
|
</div> |
|
113
|
{isDevelopment && errorMessage && ( |
|
114
|
<div className="bg-ui-bg-disabled border-ui-border-subtle max-h-[400px] max-w-3xl overflow-scroll rounded-lg border p-4"> |
|
115
|
<div className="flex flex-col gap-y-4"> |
|
116
|
<div className="flex items-center justify-between"> |
|
117
|
<Text size="small" weight="plus" className="text-ui-fg-base"> |
|
118
|
Error Details (Development Mode) |
|
119
|
</Text> |
|
120
|
<Tooltip content="Copy error details"> |
|
121
|
<IconButton onClick={handleCopyError} variant="transparent"> |
|
122
|
<SquareTwoStack className="text-ui-fg-muted" /> |
|
123
|
</IconButton> |
|
124
|
</Tooltip> |
|
125
|
</div> |
|
126
|
<div> |
|
127
|
<Text |
|
128
|
size="xsmall" |
|
129
|
className="text-ui-fg-subtle break-words font-mono" |
|
130
|
> |
|
131
|
{errorMessage} |
|
132
|
</Text> |
|
133
|
</div> |
|
134
|
{errorStack && ( |
|
135
|
<div> |
|
136
|
<Text |
|
137
|
size="small" |
|
138
|
weight="plus" |
|
139
|
className="text-ui-fg-base mb-2" |
|
140
|
> |
|
141
|
Stack Trace |
|
142
|
</Text> |
|
143
|
<pre className="text-ui-fg-subtle overflow-auto whitespace-pre-wrap break-words text-xs"> |
|
144
|
{errorStack} |
|
145
|
</pre> |
|
146
|
</div> |
|
147
|
)} |
|
148
|
</div> |
|
149
|
</div> |
|
150
|
)} |
|
151
|
</div> |
|
152
|
</div> |
|
153
|
) |
|
154
|
} |