|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useRef } from "react"; |
| 4 | + |
| 5 | +type ReporterProps = { |
| 6 | + /* ⎯⎯ props are only provided on the global-error page ⎯⎯ */ |
| 7 | + error?: Error & { digest?: string }; |
| 8 | + reset?: () => void; |
| 9 | +}; |
| 10 | + |
| 11 | +export default function ErrorReporter({ error, reset }: ReporterProps) { |
| 12 | + /* ─ instrumentation shared by every route ─ */ |
| 13 | + const lastOverlayMsg = useRef(""); |
| 14 | + const pollRef = useRef<NodeJS.Timeout>(setInterval(() => {}, 2000)); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + const inIframe = window.parent !== window; |
| 18 | + if (!inIframe) return; |
| 19 | + |
| 20 | + const send = (payload: unknown) => window.parent.postMessage(payload, "*"); |
| 21 | + |
| 22 | + const onError = (e: ErrorEvent) => |
| 23 | + send({ |
| 24 | + type: "ERROR_CAPTURED", |
| 25 | + error: { |
| 26 | + message: e.message, |
| 27 | + stack: e.error?.stack, |
| 28 | + filename: e.filename, |
| 29 | + lineno: e.lineno, |
| 30 | + colno: e.colno, |
| 31 | + source: "window.onerror", |
| 32 | + }, |
| 33 | + timestamp: Date.now(), |
| 34 | + }); |
| 35 | + |
| 36 | + const onReject = (e: PromiseRejectionEvent) => |
| 37 | + send({ |
| 38 | + type: "ERROR_CAPTURED", |
| 39 | + error: { |
| 40 | + message: e.reason?.message ?? String(e.reason), |
| 41 | + stack: e.reason?.stack, |
| 42 | + source: "unhandledrejection", |
| 43 | + }, |
| 44 | + timestamp: Date.now(), |
| 45 | + }); |
| 46 | + |
| 47 | + const pollOverlay = () => { |
| 48 | + const overlay = document.querySelector("[data-nextjs-dialog-overlay]"); |
| 49 | + const node = |
| 50 | + overlay?.querySelector( |
| 51 | + "h1, h2, .error-message, [data-nextjs-dialog-body]" |
| 52 | + ) ?? null; |
| 53 | + const txt = node?.textContent ?? node?.innerHTML ?? ""; |
| 54 | + if (txt && txt !== lastOverlayMsg.current) { |
| 55 | + lastOverlayMsg.current = txt; |
| 56 | + send({ |
| 57 | + type: "ERROR_CAPTURED", |
| 58 | + error: { message: txt, source: "nextjs-dev-overlay" }, |
| 59 | + timestamp: Date.now(), |
| 60 | + }); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + window.addEventListener("error", onError); |
| 65 | + window.addEventListener("unhandledrejection", onReject); |
| 66 | + pollRef.current = setInterval(pollOverlay, 1000); |
| 67 | + |
| 68 | + return () => { |
| 69 | + window.removeEventListener("error", onError); |
| 70 | + window.removeEventListener("unhandledrejection", onReject); |
| 71 | + pollRef.current && clearInterval(pollRef.current); |
| 72 | + }; |
| 73 | + }, []); |
| 74 | + |
| 75 | + /* ─ extra postMessage when on the global-error route ─ */ |
| 76 | + useEffect(() => { |
| 77 | + if (!error) return; |
| 78 | + window.parent.postMessage( |
| 79 | + { |
| 80 | + type: "global-error-reset", |
| 81 | + error: { |
| 82 | + message: error.message, |
| 83 | + stack: error.stack, |
| 84 | + digest: error.digest, |
| 85 | + name: error.name, |
| 86 | + }, |
| 87 | + timestamp: Date.now(), |
| 88 | + userAgent: navigator.userAgent, |
| 89 | + }, |
| 90 | + "*" |
| 91 | + ); |
| 92 | + }, [error]); |
| 93 | + |
| 94 | + /* ─ ordinary pages render nothing ─ */ |
| 95 | + if (!error) return null; |
| 96 | + |
| 97 | + /* ─ global-error UI ─ */ |
| 98 | + return ( |
| 99 | + <html> |
| 100 | + <body className="min-h-screen bg-background text-foreground flex items-center justify-center p-4"> |
| 101 | + <div className="max-w-md w-full text-center space-y-6"> |
| 102 | + <div className="space-y-2"> |
| 103 | + <h1 className="text-2xl font-bold text-destructive"> |
| 104 | + Something went wrong! |
| 105 | + </h1> |
| 106 | + <p className="text-muted-foreground"> |
| 107 | + An unexpected error occurred. Please try again fixing with Orchids |
| 108 | + </p> |
| 109 | + </div> |
| 110 | + <div className="space-y-2"> |
| 111 | + {process.env.NODE_ENV === "development" && ( |
| 112 | + <details className="mt-4 text-left"> |
| 113 | + <summary className="cursor-pointer text-sm text-muted-foreground hover:text-foreground"> |
| 114 | + Error details |
| 115 | + </summary> |
| 116 | + <pre className="mt-2 text-xs bg-muted p-2 rounded overflow-auto"> |
| 117 | + {error.message} |
| 118 | + {error.stack && ( |
| 119 | + <div className="mt-2 text-muted-foreground"> |
| 120 | + {error.stack} |
| 121 | + </div> |
| 122 | + )} |
| 123 | + {error.digest && ( |
| 124 | + <div className="mt-2 text-muted-foreground"> |
| 125 | + Digest: {error.digest} |
| 126 | + </div> |
| 127 | + )} |
| 128 | + </pre> |
| 129 | + </details> |
| 130 | + )} |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + </body> |
| 134 | + </html> |
| 135 | + ); |
| 136 | +} |
0 commit comments