mirror of
https://github.com/Steffo99/sophon.git
synced 2024-12-22 23:04:21 +00:00
✨ Add some error handling
This commit is contained in:
parent
edd29fe753
commit
8d751429be
2 changed files with 72 additions and 4 deletions
64
frontend/src/components/ErrorBox.tsx
Normal file
64
frontend/src/components/ErrorBox.tsx
Normal 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>
|
||||
)
|
||||
}
|
|
@ -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>
|
||||
</>
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue