-
Notifications
You must be signed in to change notification settings - Fork 0
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 #48 from oasisprotocol/ml/error-bundary
Add error boundaries
- Loading branch information
Showing
10 changed files
with
138 additions
and
60 deletions.
There are no files selected for viewing
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,27 @@ | ||
import { Component, PropsWithChildren } from 'react' | ||
import { ErrorBoundaryLayout } from '../ErrorBoundaryLayout' | ||
|
||
interface Props { | ||
hasError: boolean | ||
error?: unknown | ||
} | ||
|
||
export class ErrorBoundary extends Component<PropsWithChildren, Props> { | ||
constructor(props: PropsWithChildren) { | ||
super(props) | ||
this.state = { hasError: false } | ||
} | ||
|
||
static getDerivedStateFromError(error: unknown) { | ||
console.error(error) | ||
return { hasError: true, error } | ||
} | ||
|
||
render() { | ||
if (this.state.hasError) { | ||
return <ErrorBoundaryLayout error={this.state.error} /> | ||
} | ||
|
||
return this.props.children | ||
} | ||
} |
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,9 @@ | ||
.errorAlert { | ||
margin-top: 10rem; | ||
} | ||
|
||
@media screen and (max-width: 1000px) { | ||
.errorAlert { | ||
margin-top: 0; | ||
} | ||
} |
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,17 @@ | ||
import { FC } from 'react' | ||
import { LayoutBase } from '../LayoutBase' | ||
import { StringUtils } from '../../utils/string.utils.ts' | ||
import { Alert } from '../Alert' | ||
import classes from './index.module.css' | ||
|
||
interface Props { | ||
error: unknown | ||
} | ||
|
||
export const ErrorBoundaryLayout: FC<Props> = ({ error }) => ( | ||
<LayoutBase> | ||
<Alert className={classes.errorAlert} type="error"> | ||
{StringUtils.truncate((error as Error).message ?? JSON.stringify(error))} | ||
</Alert> | ||
</LayoutBase> | ||
) |
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,18 @@ | ||
.layout { | ||
min-height: 100dvh; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: space-between; | ||
} | ||
|
||
.main { | ||
margin: 0 auto; | ||
width: 100%; | ||
max-width: 1200px; | ||
} | ||
|
||
@media screen and (max-width: 1000px) { | ||
.layout { | ||
padding: 0 1.5rem 0.25rem; | ||
} | ||
} |
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,10 @@ | ||
import { FC, PropsWithChildren } from 'react' | ||
import classes from './index.module.css' | ||
|
||
export const LayoutBase: FC<PropsWithChildren> = ({ children }) => { | ||
return ( | ||
<div className={classes.layout}> | ||
<main className={classes.main}>{children}</main> | ||
</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,8 @@ | ||
import { useRouteError } from 'react-router-dom' | ||
import { ErrorBoundaryLayout } from '../ErrorBoundaryLayout' | ||
|
||
export const RouterErrorBoundary = () => { | ||
const error = useRouteError() | ||
|
||
return <ErrorBoundaryLayout error={error} /> | ||
} |