1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-12-22 06:44:21 +00:00

Add some error handling

This commit is contained in:
Steffo 2021-09-20 17:53:54 +02:00
parent a207d55a83
commit 126451f900
2 changed files with 72 additions and 4 deletions

View file

@ -0,0 +1,64 @@
import * as React from "react"
import {Box, Heading} from "@steffo/bluelib-react";
interface ErrorBoxProps {
error?: Error,
}
export function ErrorBox({error}: ErrorBoxProps): JSX.Element | null {
if(!error) {
return null
}
return (
<Box bluelibClassNames={"color-red"}>
{error.toString()}
</Box>
)
}
interface ErrorCatcherBoxProps {
children: React.ReactNode,
}
interface ErrorCatcherBoxState {
error?: Error,
}
export class ErrorCatcherBox extends React.Component<ErrorCatcherBoxProps, ErrorCatcherBoxState> {
constructor(props: ErrorCatcherBoxProps) {
super(props)
this.state = {error: undefined}
}
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
console.warn("Caught error:", error)
this.setState(state => {
return {...state, error}
})
}
render() {
return (
<ErrorBox error={this.state.error}/>
)
}
}
export function NotFoundBox() {
return (
<Box bluelibClassNames={"color-red"}>
<Heading level={3}>
Not found
</Heading>
<p>
The page you were looking for was not found.
</p>
</Box>
)
}

View file

@ -4,6 +4,7 @@ import { LoginPage } from "./LoginPage"
import { Heading } from "@steffo/bluelib-react"
import { SelectResearchGroupPage } from "./SelectResearchGroupPage"
import {Link} from "../components/Link"
import { ErrorCatcherBox, NotFoundBox } from "../components/ErrorBox"
function Header() {
@ -20,9 +21,12 @@ export function Router() {
<Reach.Router primary={false}>
<Header default/>
</Reach.Router>
<Reach.Router>
<LoginPage path={"/"}/>
<SelectResearchGroupPage path={"/g/"}/>
</Reach.Router>
<ErrorCatcherBox>
<Reach.Router>
<LoginPage path={"/"}/>
<SelectResearchGroupPage path={"/g/"}/>
<NotFoundBox default/>
</Reach.Router>
</ErrorCatcherBox>
</>
}