-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from Olog/CSSTUDIO-2002-handle-404
CSSTUDIO-2002: handle app errors and unknown routes
- Loading branch information
Showing
21 changed files
with
219 additions
and
103 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,88 @@ | ||
import { Button, styled } from "@mui/material"; | ||
import ErrorPage from "./ErrorPage"; | ||
import { useCallback } from "react"; | ||
import { useEffectOnMount } from "hooks/useMountEffects"; | ||
import { ErrorBoundary, useErrorBoundary } from "react-error-boundary"; | ||
import theme from "config/theme"; | ||
|
||
function ReturnHomeButton({ resetErrorBoundary }) { | ||
return ( | ||
<Button | ||
variant="contained" | ||
onClick={resetErrorBoundary} | ||
aria-label="Reload Page and navigate home" | ||
> | ||
Return to Home | ||
</Button> | ||
); | ||
} | ||
|
||
/** | ||
* ErrorBoundary calls resetErrorBoundary prop of this component | ||
* @param {string} error error message | ||
* @param {string} supportHref app support link | ||
* @param {string} resetErrorBoundary callback for reseting error boundary | ||
* @param {string} error error object raised by the application | ||
* @returns | ||
*/ | ||
function AwSnap({ error, supportHref, resetErrorBoundary }) { | ||
console.error("AwSnap", error); | ||
|
||
return ( | ||
<ErrorPage | ||
title="Whoops! Something went wrong" | ||
titleProps={{ component: "h1", variant: "h2" }} | ||
details={error.stack} | ||
supportHref={supportHref} | ||
// Navigation etc features don't work after error boundary has been breached | ||
// So we must override the secondary action with a button that resets the boundary | ||
SecondaryActionComponent={ | ||
<ReturnHomeButton resetErrorBoundary={resetErrorBoundary} /> | ||
} | ||
/> | ||
); | ||
} | ||
|
||
function WindowErrorRedirect({ children }) { | ||
const { showBoundary } = useErrorBoundary(); | ||
const onError = useCallback( | ||
(event) => { | ||
showBoundary(event.error ?? event.reason); | ||
}, | ||
[showBoundary] | ||
); | ||
|
||
useEffectOnMount(() => { | ||
window.addEventListener("error", onError); | ||
window.addEventListener("unhandledrejection", onError); | ||
return function cleanup() { | ||
window.removeEventListener("error", onError); | ||
window.removeEventListener("unhandledrejection", onError); | ||
}; | ||
}); | ||
|
||
return children; | ||
} | ||
|
||
export const AppErrorBoundary = styled(({ children, supportHref }) => { | ||
|
||
const onReset=() => { | ||
window.location.href = "/"; | ||
}; | ||
|
||
return ( | ||
<ErrorBoundary | ||
FallbackComponent={({resetErrorBoundary, error }) => ( | ||
<AwSnap | ||
supportHref={supportHref} | ||
resetErrorBoundary={resetErrorBoundary} | ||
error={error} | ||
/> | ||
)} | ||
onReset={onReset} | ||
> | ||
<WindowErrorRedirect>{children}</WindowErrorRedirect> | ||
</ErrorBoundary> | ||
); | ||
})(theme); | ||
|
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,20 @@ | ||
import { ErrorPage } from "./ErrorPage"; | ||
|
||
export const ServerErrorPage = ({ message, status, supportHref }) => { | ||
// Define some fallback messages if none provided | ||
if (!message) { | ||
if (status?.toString().startsWith("5")) { | ||
message = "Whoops, looks like you broke the internet!"; | ||
} else { | ||
message = "Page not found"; | ||
} | ||
} | ||
|
||
return ( | ||
<ErrorPage | ||
title={message} | ||
subtitle={status} | ||
supportHref={supportHref} | ||
/> | ||
); | ||
}; |
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
File renamed without changes.
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,23 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
function useMountEffect(effect, deps = [], afterMount) { | ||
const didMountRef = useRef(false); | ||
|
||
useEffect(() => { | ||
let cleanup; | ||
if (didMountRef.current === afterMount) { | ||
cleanup = effect(); | ||
} | ||
didMountRef.current = true; | ||
return cleanup; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, deps); | ||
} | ||
|
||
export function useEffectOnMount(effect) { | ||
useMountEffect(effect, [], false); | ||
} | ||
|
||
export function useEffectAfterMount(effect, deps) { | ||
useMountEffect(effect, deps, true); | ||
} |
Oops, something went wrong.