UNPKG

3.61 kBJavaScriptView Raw
1/**
2 * react-router v8.5.3
3 *
4 * Copyright (c) Remix Software Inc.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE.md file in the root directory of this source tree.
8 *
9 * @license MIT
10 */
11import { isRouteErrorResponse } from "../router/utils.js";
12import { RSCErrorAlreadyReportedContext } from "../context.js";
13import { useRouteError } from "../hooks.js";
14import React from "react";
15//#region lib/rsc/errorBoundaries.tsx
16var RSCRouterGlobalErrorBoundary = class extends React.Component {
17 constructor(props) {
18 super(props);
19 this.state = {
20 error: null,
21 location: props.location
22 };
23 }
24 static getDerivedStateFromError(error) {
25 return { error };
26 }
27 static getDerivedStateFromProps(props, state) {
28 if (state.location !== props.location) return {
29 error: null,
30 location: props.location
31 };
32 return {
33 error: state.error,
34 location: state.location
35 };
36 }
37 render() {
38 if (this.state.error) return /* @__PURE__ */ React.createElement(RSCDefaultRootErrorBoundaryImpl, {
39 error: this.state.error,
40 renderAppShell: true
41 });
42 else return this.props.children;
43 }
44};
45function ErrorWrapper({ renderAppShell, title, children }) {
46 if (!renderAppShell) return children;
47 return /* @__PURE__ */ React.createElement("html", { lang: "en" }, /* @__PURE__ */ React.createElement("head", null, /* @__PURE__ */ React.createElement("meta", { charSet: "utf-8" }), /* @__PURE__ */ React.createElement("meta", {
48 name: "viewport",
49 content: "width=device-width,initial-scale=1,viewport-fit=cover"
50 }), /* @__PURE__ */ React.createElement("title", null, title)), /* @__PURE__ */ React.createElement("body", null, /* @__PURE__ */ React.createElement("main", { style: {
51 fontFamily: "system-ui, sans-serif",
52 padding: "2rem"
53 } }, children)));
54}
55function RSCDefaultRootErrorBoundaryImpl({ error, renderAppShell }) {
56 if (!React.useContext(RSCErrorAlreadyReportedContext)) console.error(error);
57 let heyDeveloper = /* @__PURE__ */ React.createElement("script", { dangerouslySetInnerHTML: { __html: `
58 console.log(
59 "💿 Hey developer 👋. You can provide a way better UX than this when your app throws errors. Check out https://reactrouter.com/how-to/error-boundary for more information."
60 );
61 ` } });
62 if (isRouteErrorResponse(error)) return /* @__PURE__ */ React.createElement(ErrorWrapper, {
63 renderAppShell,
64 title: "Unhandled Thrown Response!"
65 }, /* @__PURE__ */ React.createElement("h1", { style: { fontSize: "24px" } }, error.status, " ", error.statusText), heyDeveloper);
66 let errorInstance;
67 if (error instanceof Error) errorInstance = error;
68 else {
69 let errorString = error == null ? "Unknown Error" : typeof error === "object" && "toString" in error ? error.toString() : JSON.stringify(error);
70 errorInstance = new Error(errorString);
71 }
72 return /* @__PURE__ */ React.createElement(ErrorWrapper, {
73 renderAppShell,
74 title: "Application Error!"
75 }, /* @__PURE__ */ React.createElement("h1", { style: { fontSize: "24px" } }, "Application Error"), /* @__PURE__ */ React.createElement("pre", { style: {
76 padding: "2rem",
77 background: "hsla(10, 50%, 50%, 0.1)",
78 color: "red",
79 overflow: "auto"
80 } }, errorInstance.stack), heyDeveloper);
81}
82function RSCDefaultRootErrorBoundary({ hasRootLayout }) {
83 let error = useRouteError();
84 if (hasRootLayout === void 0) throw new Error("Missing 'hasRootLayout' prop");
85 return /* @__PURE__ */ React.createElement(RSCDefaultRootErrorBoundaryImpl, {
86 renderAppShell: !hasRootLayout,
87 error
88 });
89}
90//#endregion
91export { RSCDefaultRootErrorBoundary, RSCRouterGlobalErrorBoundary };