-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update error component, wrap Error Boundary modal content (#129)
* feat: update error component, wrapped modal content Signed-off-by: Innei <i@innei.in> * fix: 404 entry Signed-off-by: Innei <i@innei.in> * fix: update Signed-off-by: Innei <i@innei.in> --------- Signed-off-by: Innei <i@innei.in>
- Loading branch information
Showing
12 changed files
with
205 additions
and
89 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { repository } from "@pkg" | ||
import { attachOpenInEditor } from "@renderer/lib/dev" | ||
import type { FallbackRender } from "@sentry/react" | ||
import type { FC } from "react" | ||
|
||
import { m } from "../common/Motion" | ||
import { StyledButton } from "../ui/button" | ||
import { useCurrentModal } from "../ui/modal" | ||
import { parseError } from "./helper" | ||
|
||
export const ModalErrorFallback: FC<Parameters<FallbackRender>[0]> = ( | ||
props, | ||
) => { | ||
const { message, stack } = parseError(props.error) | ||
const modal = useCurrentModal() | ||
return ( | ||
<m.div | ||
className="flex flex-col items-center justify-center rounded-md bg-theme-modal-background-opaque p-2" | ||
exit={{ | ||
opacity: 0, | ||
scale: 0.9, | ||
}} | ||
> | ||
<div className="m-auto max-w-prose text-center"> | ||
<div className="mb-4"> | ||
<i className="i-mgc-bug-cute-re text-4xl text-red-500" /> | ||
</div> | ||
<div className="text-lg font-bold">{message}</div> | ||
{import.meta.env.DEV && stack ? ( | ||
<div className="mt-4 cursor-text overflow-auto whitespace-pre rounded-md bg-red-50 p-4 text-left font-mono text-sm text-red-600"> | ||
{attachOpenInEditor(stack)} | ||
</div> | ||
) : null} | ||
|
||
<p className="my-8"> | ||
The App has a temporary problem, click the button below to try | ||
reloading the app or another solution? | ||
</p> | ||
|
||
<div className="center gap-4"> | ||
<StyledButton onClick={() => modal.dismiss()} variant="outline"> | ||
Close Modal | ||
</StyledButton> | ||
<StyledButton | ||
onClick={() => window.location.reload()} | ||
variant="outline" | ||
> | ||
Reload | ||
</StyledButton> | ||
</div> | ||
|
||
<p className="mt-8"> | ||
Still having this issue? Please give feedback in Github, thanks! | ||
<a | ||
className="ml-2 cursor-pointer text-theme-accent-500 duration-200 hover:text-theme-accent" | ||
href={`${repository.url}/issues/new?title=${encodeURIComponent( | ||
`Error: ${message}`, | ||
)}&body=${encodeURIComponent( | ||
`### Error\n\n${message}\n\n### Stack\n\n\`\`\`\n${stack}\n\`\`\``, | ||
)}&label=bug`} | ||
target="_blank" | ||
rel="noreferrer" | ||
> | ||
Submit Issue | ||
</a> | ||
</p> | ||
</div> | ||
</m.div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const parseError = (error: unknown): { message?: string, stack?: string } => { | ||
if (error instanceof Error) { | ||
return { | ||
message: error.message, | ||
stack: error.stack, | ||
} | ||
} else { | ||
return { | ||
message: String(error), | ||
stack: undefined, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ModalErrorFallback } from "./ModalError" | ||
|
||
export enum ErrorComponentType { | ||
Modal = "Modal", | ||
} | ||
|
||
export const ErrorFallbackMap = { | ||
[ErrorComponentType.Modal]: ModalErrorFallback, | ||
} | ||
|
||
export const getErrorFallback = (type: ErrorComponentType) => ErrorFallbackMap[type] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
export const attachOpenInEditor = (stack: string) => { | ||
const lines = stack.split("\n") | ||
return lines.map((line) => { | ||
// A line like this: at App (http://localhost:5173/src/App.tsx?t=1720527056591:41:9) | ||
// Find the `localhost` part and open the file in the editor | ||
if (!line.includes("at ")) { | ||
return line | ||
} | ||
const match = line.match(/(http:\/\/localhost:\d+\/[^:]+):(\d+):(\d+)/) | ||
|
||
if (match) { | ||
const [o] = match | ||
|
||
// Find `@fs/` | ||
// Like: `http://localhost:5173/@fs/Users/innei/git/work/rss3/follow/node_modules/.vite/deps/chunk-RPCDYKBN.js?v=757920f2:11548:26` | ||
const realFsPath = o.split("@fs")[1] | ||
|
||
if (realFsPath) { | ||
return ( | ||
// Delete `v=` hash, like `v=757920f2` | ||
<div | ||
className="cursor-pointer" | ||
key={line} | ||
onClick={openInEditor.bind( | ||
null, | ||
realFsPath.replace(/\?v=[a-f0-9]+/, ""), | ||
)} | ||
> | ||
{line} | ||
</div> | ||
) | ||
} else { | ||
// at App (http://localhost:5173/src/App.tsx?t=1720527056591:41:9) | ||
const srcFsPath = o.split("/src")[1] | ||
|
||
if (srcFsPath) { | ||
const fs = srcFsPath.replace(/\?t=[a-f0-9]+/, "") | ||
|
||
return ( | ||
<div | ||
className="cursor-pointer" | ||
key={line} | ||
onClick={openInEditor.bind( | ||
null, | ||
`${APP_DEV_CWD}/src/renderer/src${fs}`, | ||
)} | ||
> | ||
{line} | ||
</div> | ||
) | ||
} | ||
} | ||
} | ||
|
||
return line | ||
}) | ||
} | ||
// http://localhost:5173/src/App.tsx?t=1720527056591:41:9 | ||
const openInEditor = (file: string) => { | ||
fetch(`/__open-in-editor?file=${encodeURIComponent(`${file}`)}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.