Skip to content

Commit

Permalink
feat: add error boundary
Browse files Browse the repository at this point in the history
makes crashes graceful
  • Loading branch information
zyachel committed Jan 22, 2023
1 parent 71d1d5b commit 5cc2ef0
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 28 deletions.
45 changes: 45 additions & 0 deletions src/components/error/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React, { Component, ErrorInfo, ReactNode } from 'react';
import ErrorInfoComponent from './ErrorInfo';

type Props = {
children: ReactNode;
};
type State = {
hasError: boolean;
};

class ErrorBoundary extends Component<Props, State> {
state: State = {
hasError: false,
};

static getDerivedStateFromError(error: Error): State {
return { hasError: true };
}

componentDidCatch(error: Error, errorInfo: ErrorInfo) {
console.error('Uncaught error:', error, errorInfo);
}

resetError() {
this.setState({ hasError: false });
}

render() {
if (this.state.hasError)
return (
<ErrorInfoComponent
message='Something weird happened on your browser.'
misc={{
subtext: 'Check console for more information.',
buttonClickHandler: this.resetError.bind(this),
buttonText: 'Reload Page',
}}
/>
);

return this.props.children;
}
}

export default ErrorBoundary;
58 changes: 38 additions & 20 deletions src/components/error/ErrorInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ReactNode } from 'react';
import Link from 'next/link';
import Layout from '../../layouts/Layout';
import Meta from '../meta/Meta';
Expand All @@ -8,39 +9,56 @@ import styles from '../../styles/modules/components/error/error-info.module.scss
// description copied verbatim from https://www.gnu.org/graphics/sventsitsky-sadgnu.html
// 404 idea from ninamori.org 404 page.

const ErrorInfo = ({ message = 'Not found, sorry.', statusCode = 404 }) => {
type Props = {
message: string;
statusCode?: number;
// props specific to error boundary.
misc?: {
subtext: string;
buttonText: string;
buttonClickHandler: () => void;
};
};

const ErrorInfo = ({ message, statusCode, misc }: Props) => {
const title = statusCode ? `${message} (${statusCode})` : message;
return (
<>
<Meta
title={`${message} (${statusCode})`}
description="you encountered an error page!"
/>
<Meta title={title} description='you encountered an error page!' />
<Layout className={styles.error}>
<svg
className={styles.gnu}
focusable="false"
role="img"
aria-labelledby="gnu-title gnu-desc"
focusable='false'
role='img'
aria-labelledby='gnu-title gnu-desc'
>
<title id="gnu-title">GNU and Tux</title>
<desc id="gnu-desc">
<title id='gnu-title'>GNU and Tux</title>
<desc id='gnu-desc'>
A pencil drawing of a big gnu and a small penguin, both very sad.
GNU is despondently sitting on a bench, and Tux stands beside him,
looking down and patting him on the back.
</desc>
<use href="/svg/sadgnu.svg#sad-gnu"></use>
<use href='/svg/sadgnu.svg#sad-gnu'></use>
</svg>
<h1 className={`heading heading__primary ${styles.heading}`}>
<span>{message}</span>
<span> ({statusCode})</span>
{title}
</h1>
<p className={styles.back}>
Go back to{' '}
<Link href="/about">
<a className="link">the homepage</a>
</Link>
.
</p>
{misc ? (
<>
<p>{misc.subtext}</p>
<button className={styles.button} onClick={misc.buttonClickHandler}>
{misc.buttonText}
</button>
</>
) : (
<p>
Go back to{' '}
<Link href='/about'>
<a className='link'>the homepage</a>
</Link>
.
</p>
)}
</Layout>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/404.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ErrorInfo from '../components/error/ErrorInfo';

const Error404 = () => {
return <ErrorInfo />;
return <ErrorInfo message='Not found, sorry.' statusCode={404} />;
};

export default Error404;
2 changes: 1 addition & 1 deletion src/pages/500.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ErrorInfo from '../components/error/ErrorInfo';

const Error500 = () => {
return <ErrorInfo message="server messed up, sorry." statusCode={500} />;
return <ErrorInfo message='Server messed up, sorry.' statusCode={500} />;
};
export default Error500;
12 changes: 7 additions & 5 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import type { AppProps } from 'next/app';
import usePageLoading from '../hooks/usePageLoading';
import ProgressBar from '../components/loaders/ProgressBar';
import ErrorBoundary from '../components/error/ErrorBoundary';
import ThemeProvider from '../context/theme-context';

import '../styles/main.scss';
import { useRouter } from 'next/router';

const ModifiedApp = ({ Component, pageProps }: AppProps) => {
const { isPageLoading, key } = usePageLoading();

return (
<ThemeProvider>
{isPageLoading && <ProgressBar />}
<Component
{...pageProps}
key={key} /* passing key to force react to remound components */
/>
<ErrorBoundary>
<Component
{...pageProps}
key={key} /* passing key to force react to remount components */
/>
</ErrorBoundary>
</ThemeProvider>
);
};
Expand Down
1 change: 1 addition & 0 deletions src/styles/base/_typography.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ body {
color: var(--clr-text-accent);
font-family: var(--ff-accent);
font-weight: var(--fw-medium);
line-height: 1.2;

&__primary {
font-size: var(--fs-1);
Expand Down
12 changes: 11 additions & 1 deletion src/styles/modules/components/error/error-info.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
}

.heading {
// justify-self: center;
text-align: center;
}

.button {
align-self: end;

font: inherit;
cursor: pointer;
border: none;
background: transparent;
color: var(--clr-link);
border-bottom: 2px solid var(--clr-link);
}

0 comments on commit 5cc2ef0

Please sign in to comment.