-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorReporter.tsx
More file actions
136 lines (123 loc) · 4.09 KB
/
ErrorReporter.tsx
File metadata and controls
136 lines (123 loc) · 4.09 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
136
"use client";
import { useEffect, useRef } from "react";
type ReporterProps = {
/* ⎯⎯ props are only provided on the global-error page ⎯⎯ */
error?: Error & { digest?: string };
reset?: () => void;
};
export default function ErrorReporter({ error, reset }: ReporterProps) {
/* ─ instrumentation shared by every route ─ */
const lastOverlayMsg = useRef("");
const pollRef = useRef<NodeJS.Timeout>();
useEffect(() => {
const inIframe = window.parent !== window;
if (!inIframe) return;
const send = (payload: unknown) => window.parent.postMessage(payload, "*");
const onError = (e: ErrorEvent) =>
send({
type: "ERROR_CAPTURED",
error: {
message: e.message,
stack: e.error?.stack,
filename: e.filename,
lineno: e.lineno,
colno: e.colno,
source: "window.onerror",
},
timestamp: Date.now(),
});
const onReject = (e: PromiseRejectionEvent) =>
send({
type: "ERROR_CAPTURED",
error: {
message: e.reason?.message ?? String(e.reason),
stack: e.reason?.stack,
source: "unhandledrejection",
},
timestamp: Date.now(),
});
const pollOverlay = () => {
const overlay = document.querySelector("[data-nextjs-dialog-overlay]");
const node =
overlay?.querySelector(
"h1, h2, .error-message, [data-nextjs-dialog-body]"
) ?? null;
const txt = node?.textContent ?? node?.innerHTML ?? "";
if (txt && txt !== lastOverlayMsg.current) {
lastOverlayMsg.current = txt;
send({
type: "ERROR_CAPTURED",
error: { message: txt, source: "nextjs-dev-overlay" },
timestamp: Date.now(),
});
}
};
window.addEventListener("error", onError);
window.addEventListener("unhandledrejection", onReject);
pollRef.current = setInterval(pollOverlay, 1000);
return () => {
window.removeEventListener("error", onError);
window.removeEventListener("unhandledrejection", onReject);
pollRef.current && clearInterval(pollRef.current);
};
}, []);
/* ─ extra postMessage when on the global-error route ─ */
useEffect(() => {
if (!error) return;
window.parent.postMessage(
{
type: "global-error-reset",
error: {
message: error.message,
stack: error.stack,
digest: error.digest,
name: error.name,
},
timestamp: Date.now(),
userAgent: navigator.userAgent,
},
"*"
);
}, [error]);
/* ─ ordinary pages render nothing ─ */
if (!error) return null;
/* ─ global-error UI ─ */
return (
<html>
<body className="min-h-screen bg-background text-foreground flex items-center justify-center p-4">
<div className="max-w-md w-full text-center space-y-6">
<div className="space-y-2">
<h1 className="text-2xl font-bold text-destructive">
Something went wrong!
</h1>
<p className="text-muted-foreground">
An unexpected error occurred. Please try again fixing with Orchids
</p>
</div>
<div className="space-y-2">
{process.env.NODE_ENV === "development" && (
<details className="mt-4 text-left">
<summary className="cursor-pointer text-sm text-muted-foreground hover:text-foreground">
Error details
</summary>
<pre className="mt-2 text-xs bg-muted p-2 rounded overflow-auto">
{error.message}
{error.stack && (
<div className="mt-2 text-muted-foreground">
{error.stack}
</div>
)}
{error.digest && (
<div className="mt-2 text-muted-foreground">
Digest: {error.digest}
</div>
)}
</pre>
</details>
)}
</div>
</div>
</body>
</html>
);
}