|
| 1 | +/** |
| 2 | + * @file A global error boundary designed to work with React Router. |
| 3 | + * |
| 4 | + * This is not documented well, but because of React Router works, it will |
| 5 | + * automatically intercept any render errors produced in routes, and will |
| 6 | + * "swallow" them, preventing the errors from bubbling up to any error |
| 7 | + * boundaries above the router. The global error boundary must be explicitly |
| 8 | + * bound to a route to work as expected. |
| 9 | + */ |
| 10 | +import type { Interpolation } from "@emotion/react"; |
| 11 | +import Link from "@mui/material/Link"; |
| 12 | +import { Button } from "components/Button/Button"; |
| 13 | +import { CoderIcon } from "components/Icons/CoderIcon"; |
| 14 | +import { useEmbeddedMetadata } from "hooks/useEmbeddedMetadata"; |
| 15 | +import { type FC, useState } from "react"; |
| 16 | +import { Helmet } from "react-helmet-async"; |
| 17 | +import { |
| 18 | + type ErrorResponse, |
| 19 | + isRouteErrorResponse, |
| 20 | + useLocation, |
| 21 | + useRouteError, |
| 22 | +} from "react-router-dom"; |
| 23 | + |
| 24 | +const errorPageTitle = "Something went wrong"; |
| 25 | + |
| 26 | +// Mocking React Router's error-handling logic is a pain; the next best thing is |
| 27 | +// to split it off from the rest of the code, and pass the value via props |
| 28 | +export const GlobalErrorBoundary: FC = () => { |
| 29 | + const error = useRouteError(); |
| 30 | + return <GlobalErrorBoundaryInner error={error} />; |
| 31 | +}; |
| 32 | + |
| 33 | +type GlobalErrorBoundaryInnerProps = Readonly<{ error: unknown }>; |
| 34 | +export const GlobalErrorBoundaryInner: FC<GlobalErrorBoundaryInnerProps> = ({ |
| 35 | + error, |
| 36 | +}) => { |
| 37 | + const [showErrorMessage, setShowErrorMessage] = useState(false); |
| 38 | + const { metadata } = useEmbeddedMetadata(); |
| 39 | + const location = useLocation(); |
| 40 | + |
| 41 | + const coderVersion = metadata["build-info"].value?.version; |
| 42 | + const isRenderableError = |
| 43 | + error instanceof Error || isRouteErrorResponse(error); |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className="bg-surface-primary text-center w-full h-full flex justify-center items-center"> |
| 47 | + <Helmet> |
| 48 | + <title>{errorPageTitle}</title> |
| 49 | + </Helmet> |
| 50 | + |
| 51 | + <main className="flex gap-6 w-full max-w-prose p-4 flex-col flex-nowrap"> |
| 52 | + <div className="flex gap-2 flex-col items-center"> |
| 53 | + <CoderIcon className="w-11 h-11" /> |
| 54 | + |
| 55 | + <div className="text-content-primary flex flex-col gap-1"> |
| 56 | + <h1 className="text-2xl font-normal m-0">{errorPageTitle}</h1> |
| 57 | + <p className="leading-6 m-0"> |
| 58 | + Please try reloading the page. If reloading does not work, you can |
| 59 | + ask for help in the{" "} |
| 60 | + <Link |
| 61 | + href="https://discord.gg/coder" |
| 62 | + target="_blank" |
| 63 | + rel="noreferer" |
| 64 | + > |
| 65 | + Coder Discord community |
| 66 | + <span className="sr-only"> (link opens in a new tab)</span> |
| 67 | + </Link>{" "} |
| 68 | + or{" "} |
| 69 | + <Link |
| 70 | + target="_blank" |
| 71 | + rel="noreferer" |
| 72 | + href={publicGithubIssueLink( |
| 73 | + coderVersion, |
| 74 | + location.pathname, |
| 75 | + error, |
| 76 | + )} |
| 77 | + > |
| 78 | + open an issue on GitHub |
| 79 | + <span className="sr-only"> (link opens in a new tab)</span> |
| 80 | + </Link> |
| 81 | + . |
| 82 | + </p> |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + |
| 86 | + <div className="flex flex-row flex-nowrap justify-center gap-4"> |
| 87 | + <Button asChild className="min-w-32 font-medium"> |
| 88 | + <Link href={location.pathname}>Reload page</Link> |
| 89 | + </Button> |
| 90 | + |
| 91 | + {isRenderableError && ( |
| 92 | + <Button |
| 93 | + variant="outline" |
| 94 | + className="min-w-32" |
| 95 | + onClick={() => setShowErrorMessage(!showErrorMessage)} |
| 96 | + > |
| 97 | + {showErrorMessage ? "Hide error" : "Show error"} |
| 98 | + </Button> |
| 99 | + )} |
| 100 | + </div> |
| 101 | + |
| 102 | + {isRenderableError && showErrorMessage && <ErrorStack error={error} />} |
| 103 | + </main> |
| 104 | + </div> |
| 105 | + ); |
| 106 | +}; |
| 107 | + |
| 108 | +type ErrorStackProps = Readonly<{ error: Error | ErrorResponse }>; |
| 109 | +const ErrorStack: FC<ErrorStackProps> = ({ error }) => { |
| 110 | + return ( |
| 111 | + <aside className="p-4 text-left rounded-md border-[1px] border-content-tertiary border-solid"> |
| 112 | + {isRouteErrorResponse(error) ? ( |
| 113 | + <> |
| 114 | + <h2 className="text-base font-bold text-content-primary m-0"> |
| 115 | + HTTP {error.status} - {error.statusText} |
| 116 | + </h2> |
| 117 | + <pre className="m-0 py-2 px-0 overflow-x-auto text-xs"> |
| 118 | + <code data-testid="code">{serializeDataAsJson(error.data)}</code> |
| 119 | + </pre> |
| 120 | + </> |
| 121 | + ) : ( |
| 122 | + <> |
| 123 | + <h2 className="text-base font-bold text-content-primary m-0"> |
| 124 | + {error.name} |
| 125 | + </h2> |
| 126 | + <p data-testid="description" className="pb-4 leading-5 m-0"> |
| 127 | + {error.message} |
| 128 | + </p> |
| 129 | + {error.stack && ( |
| 130 | + <pre className="m-0 py-2 px-0 overflow-x-auto text-xs"> |
| 131 | + <code data-testid="code">{error.stack}</code> |
| 132 | + </pre> |
| 133 | + )} |
| 134 | + </> |
| 135 | + )} |
| 136 | + </aside> |
| 137 | + ); |
| 138 | +}; |
| 139 | + |
| 140 | +function serializeDataAsJson(data: unknown): string | null { |
| 141 | + try { |
| 142 | + return JSON.stringify(data, null, 2); |
| 143 | + } catch { |
| 144 | + return null; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +function publicGithubIssueLink( |
| 149 | + coderVersion: string | undefined, |
| 150 | + pathName: string, |
| 151 | + error: unknown, |
| 152 | +): string { |
| 153 | + const baseLink = "https://github.com/coder/coder/issues/new"; |
| 154 | + |
| 155 | + // Anytime you see \`\`\`txt, that's wrapping the text in a GitHub codeblock |
| 156 | + let printableError: string; |
| 157 | + if (error instanceof Error) { |
| 158 | + printableError = [ |
| 159 | + `${error.name}: ${error.message}`, |
| 160 | + error.stack ? `\`\`\`txt\n${error.stack}\n\`\`\`` : "No stack", |
| 161 | + ].join("\n"); |
| 162 | + } else if (isRouteErrorResponse(error)) { |
| 163 | + const serialized = serializeDataAsJson(error.data); |
| 164 | + printableError = [ |
| 165 | + `HTTP ${error.status} - ${error.statusText}`, |
| 166 | + serialized ? `\`\`\`txt\n${serialized}\n\`\`\`` : "(No data)", |
| 167 | + ].join("\n"); |
| 168 | + } else { |
| 169 | + printableError = "No error message available"; |
| 170 | + } |
| 171 | + |
| 172 | + const messageBody = `\ |
| 173 | +**Version** |
| 174 | +${coderVersion ?? "-- Set version --"} |
| 175 | +
|
| 176 | +**Path** |
| 177 | +\`${pathName}\` |
| 178 | +
|
| 179 | +**Error** |
| 180 | +${printableError}`; |
| 181 | + |
| 182 | + return `${baseLink}?body=${encodeURIComponent(messageBody)}`; |
| 183 | +} |
0 commit comments