From 962e6e576cf933f68ac777772c54c9f357e1bc80 Mon Sep 17 00:00:00 2001 From: Jason Fischer Date: Tue, 4 Oct 2022 13:15:49 -0700 Subject: [PATCH] Add ErrorBoundary component - wrap a myriad of components in ErrorBoundary resolves #270 --- src/components/bookmarks/group.jsx | 3 ++- src/components/errorboundry.jsx | 41 ++++++++++++++++++++++++++++++ src/components/services/group.jsx | 3 ++- src/components/services/list.jsx | 3 ++- src/pages/index.jsx | 9 ++++--- 5 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 src/components/errorboundry.jsx diff --git a/src/components/bookmarks/group.jsx b/src/components/bookmarks/group.jsx index af5100816..d4c056635 100644 --- a/src/components/bookmarks/group.jsx +++ b/src/components/bookmarks/group.jsx @@ -1,10 +1,11 @@ +import ErrorBoundary from "components/errorboundry"; import List from "components/bookmarks/list"; export default function BookmarksGroup({ group }) { return (

{group.name}

- +
); } diff --git a/src/components/errorboundry.jsx b/src/components/errorboundry.jsx new file mode 100644 index 000000000..ffee89ffd --- /dev/null +++ b/src/components/errorboundry.jsx @@ -0,0 +1,41 @@ +import React from 'react'; + +export default class ErrorBoundary extends React.Component { + constructor(props) { + super(props); + this.state = { error: null, errorInfo: null }; + } + + componentDidCatch(error, errorInfo) { + // Catch errors in any components below and re-render with error message + this.setState({ + error, + errorInfo + }) + + // You can also log error messages to an error reporting service here + // eslint-disable-next-line no-console + console.error(error, errorInfo); + } + + render() { + const { error, errorInfo } = this.state; + if (errorInfo) { + // Error path + return ( +
+

Something went wrong.

+
+ {error && error.toString()} +
+ {errorInfo.componentStack} +
+
+ ); + } + + // Normally, just render children + const { children } = this.props; + return children; + } +} \ No newline at end of file diff --git a/src/components/services/group.jsx b/src/components/services/group.jsx index daae19094..373023c13 100644 --- a/src/components/services/group.jsx +++ b/src/components/services/group.jsx @@ -1,5 +1,6 @@ import classNames from "classnames"; +import ErrorBoundary from "components/errorboundry"; import List from "components/services/list"; export default function ServicesGroup({ services, layout }) { @@ -12,7 +13,7 @@ export default function ServicesGroup({ services, layout }) { )} >

{services.name}

- + ); } diff --git a/src/components/services/list.jsx b/src/components/services/list.jsx index 80b45592a..a42a64a10 100644 --- a/src/components/services/list.jsx +++ b/src/components/services/list.jsx @@ -1,5 +1,6 @@ import classNames from "classnames"; +import ErrorBoundary from "components/errorboundry"; import Item from "components/services/item"; const columnMap = [ @@ -23,7 +24,7 @@ export default function List({ services, layout }) { )} > {services.map((service) => ( - + ))} ); diff --git a/src/pages/index.jsx b/src/pages/index.jsx index 2a1c6e7d4..c9661a074 100644 --- a/src/pages/index.jsx +++ b/src/pages/index.jsx @@ -8,6 +8,7 @@ import { useEffect, useContext, useState } from "react"; import { BiError } from "react-icons/bi"; import { serverSideTranslations } from "next-i18next/serverSideTranslations"; +import ErrorBoundary from "components/errorboundry"; import ServicesGroup from "components/services/group"; import BookmarksGroup from "components/bookmarks/group"; import Widget from "components/widgets/widget"; @@ -191,14 +192,14 @@ function Home({ initialSettings }) { {widgets .filter((widget) => !rightAlignedWidgets.includes(widget.type)) .map((widget, i) => ( - + ))}
{widgets .filter((widget) => rightAlignedWidgets.includes(widget.type)) .map((widget, i) => ( - + ))}
@@ -208,7 +209,7 @@ function Home({ initialSettings }) { {services && (
{services.map((group) => ( - + ))}
)} @@ -216,7 +217,7 @@ function Home({ initialSettings }) { {bookmarks && (
{bookmarks.map((group) => ( - + ))}
)}