Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: Add ErrorBoundary and ShowError to handle React exceptions. (#549)

Cooper Filby25c81da8427e
diff --git a/public/components/ErrorBoundary.spec.tsx b/public/components/ErrorBoundary.spec.tsx
new file mode 100644
index 000000000..836a792e4
--- /dev/null
+++ b/public/components/ErrorBoundary.spec.tsx
@@ -0,0 +1,43 @@
+import * as React from "react";
+import { shallow } from "enzyme";
+import { ErrorBoundary } from "@fider/components";
+
+describe("<ErrorBoundary />", () => {
+ let errorMethod: () => void;
+
+ // Stub out console.error to hide noisy Virtual DOM exceptions.
+ beforeAll(() => {
+ errorMethod = console.error; // tslint:disable-line
+ console.error = () => null; // tslint:disable-line
+ });
+
+ afterAll(() => {
+ console.error = errorMethod; // tslint:disable-line
+ });
+
+ test("when no error caught", () => {
+ const errorSpy = jest.fn();
+ const wrapper = shallow(
+ <ErrorBoundary onError={errorSpy}>
+ <div id="no-error">No Error!</div>
+ </ErrorBoundary>
+ );
+
+ expect(errorSpy).not.toHaveBeenCalled();
+ });
+
+ describe("when error caught", () => {
+ test("error should be passed to onError", () => {
+ const error = new Error("Whoops!");
+ const errorSpy = jest.fn();
+ const wrapper = shallow(<ErrorBoundary onError={errorSpy} />);
+
+ const componentDidCatch = wrapper.instance().componentDidCatch;
+ if (componentDidCatch) {
+ componentDidCatch.bind(wrapper.instance())(error, {} as React.ErrorInfo);
+ }
+
+ expect(errorSpy).toHaveBeenCalledWith(error);
+ });
+ });
+});
diff --git a/public/components/ErrorBoundary.tsx b/public/components/ErrorBoundary.tsx
new file mode 100644
index 000000000..834672fca
--- /dev/null
+++ b/public/components/ErrorBoundary.tsx
@@ -0,0 +1,44 @@
+import * as React from "react";
+import { ErrorPage } from "@fider/pages";
+
+interface ErrorBoundaryProps {
+ onError?: (err: Error) => void;
+}
+
+interface ErrorBoundaryState {
+ error?: Error;
+ errorInfo?: React.ErrorInfo;
+}
+
+export class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
+ constructor(props: any) {
+ super(props);
+
+ this.state = {
+ error: undefined,
+ errorInfo: undefined
+ };
+ }
+
+ public componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
+ const onError = this.props.onError;
+ if (onError) {
+ onError(error);
+ }
+
+ this.setState({
+ error,
+ errorInfo
+ });
+ }
+
+ public render() {
+ const { error, errorInfo } = this.state;
+
+ if (error && errorInfo) {
+ return <ErrorPage error={error} errorInfo={errorInfo} />;
+ } else {
+ return this.props.children;
+ }
+ }
+}
diff --git a/public/components/index.tsx b/public/components/index.tsx
index 6321e6013..48e7ed408 100644
--- a/public/components/index.tsx
+++ b/public/components/index.tsx
@@ -1,3 +1,4 @@
+export * from "./ErrorBoundary";
export * from "./ShowPostResponse";
export * from "./ShowTag";
export * from "./SignInModal";
diff --git a/public/index.tsx b/public/index.tsx
index ef89b8757..06ed231ec 100644
--- a/public/index.tsx
+++ b/public/index.tsx
@@ -2,6 +2,7 @@ import * as React from "react";
import * as ReactDOM from "react-dom";
import { resolveRootComponent } from "@fider/router";
import { Header, Footer } from "@fider/components/common";
+import { ErrorBoundary } from "@fider/components";
import { analytics, classSet, Fider } from "@fider/services";
import { ToastContainer } from "react-toastify";
@@ -13,6 +14,13 @@ import "semantic-ui-css/components/transition.min.css";
import "react-toastify/dist/ReactToastify.css";
import "@fider/assets/styles/main.scss";
+const logProductionError = (err: Error) => {
+ if (Fider.isProduction()) {
+ console.error(err); // tslint:disable-line
+ analytics.error(err);
+ }
+};
+
window.addEventListener("error", (evt: ErrorEvent) => {
analytics.error(evt.error);
});
@@ -28,12 +36,12 @@ document.addEventListener("DOMContentLoaded", () => {
"is-staff": fider.session.isAuthenticated && fider.session.user.isCollaborator
});
ReactDOM.render(
- <>
+ <ErrorBoundary onError={logProductionError}>
<ToastContainer position="top-right" toastClassName="c-toast" />
{config.showHeader && <Header />}
{React.createElement(config.component, fider.session.props)}
{config.showHeader && <Footer />}
- </>,
+ </ErrorBoundary>,
root
);
}
diff --git a/public/pages/Error/Error.page.scss b/public/pages/Error/Error.page.scss
new file mode 100644
index 000000000..20f2fad0f
--- /dev/null
+++ b/public/pages/Error/Error.page.scss
@@ -0,0 +1,7 @@
+@import '~@fider/assets/styles/variables.scss';
+
+#p-error {
+ .error {
+ margin-top: 15px;
+ }
+}
\ No newline at end of file
diff --git a/public/pages/Error/Error.page.spec.tsx b/public/pages/Error/Error.page.spec.tsx
new file mode 100644
index 000000000..701f04cb3
--- /dev/null
+++ b/public/pages/Error/Error.page.spec.tsx
@@ -0,0 +1,26 @@
+import * as React from "react";
+import { shallow } from "enzyme";
+import { ErrorPage } from "./Error.page";
+
+describe("<ErrorPage />", () => {
+ const createFakeErrorInfo = () => ({ componentStack: "" } as React.ErrorInfo);
+
+ test("it should show the error when showError returns true", () => {
+ const error = new Error("Hello");
+ const errorInfo = createFakeErrorInfo();
+
+ const showError = () => true;
+ const wrapper = shallow(<ErrorPage error={error} errorInfo={errorInfo} showError={showError} />);
+ expect(wrapper.find("pre")).toHaveLength(1);
+ });
+
+ test("it should not show the error when showError returns false", () => {
+ const error = new Error("Hello");
+ const errorInfo = createFakeErrorInfo();
+
+ const showError = () => false;
+ const wrapper = shallow(<ErrorPage error={error} errorInfo={errorInfo} showError={showError} />);
+
+ expect(wrapper.find("pre")).toHaveLength(0);
+ });
+});
diff --git a/public/pages/Error/Error.page.tsx b/public/pages/Error/Error.page.tsx
new file mode 100644
index 000000000..1867cc744
--- /dev/null
+++ b/public/pages/Error/Error.page.tsx
@@ -0,0 +1,47 @@
+import "./Error.page.scss";
+import { Fider } from "@fider/services";
+import { TenantLogo } from "@fider/components";
+
+import * as React from "react";
+
+interface ErrorPageProps {
+ title?: string;
+ message?: string;
+ error: Error;
+ errorInfo: React.ErrorInfo;
+ // Must be a function to avoid Fider.isProduction call prior to init
+ showError?: () => boolean;
+}
+
+const defaultProps: Partial<ErrorPageProps> = {
+ title: "Whoops!",
+ message: "An unexpected rendering error has occurred.",
+ showError: () => !Fider.isProduction()
+};
+
+export const ErrorPage: React.SFC<ErrorPageProps> = ({
+ title = "Whoops!",
+ message = "An unexpected rendering error has occurred.",
+ error,
+ errorInfo,
+ showError
+}: ErrorPageProps) => {
+ return (
+ <div id="p-error" className="container failure-page">
+ <TenantLogo size={100} />
+ <div className="content">
+ <h2>{title}</h2>
+ <p>{message}</p>
+ {showError &&
+ showError() && (
+ <pre className="error">
+ {error.toString()}
+ {errorInfo.componentStack}
+ </pre>
+ )}
+ </div>
+ </div>
+ );
+};
+
+ErrorPage.defaultProps = defaultProps;
diff --git a/public/pages/Error/index.ts b/public/pages/Error/index.ts
new file mode 100644
index 000000000..3b465175c
--- /dev/null
+++ b/public/pages/Error/index.ts
@@ -0,0 +1 @@
+export * from "./Error.page";
diff --git a/public/pages/index.ts b/public/pages/index.ts
index 14898de38..8296dc886 100644
--- a/public/pages/index.ts
+++ b/public/pages/index.ts
@@ -1,5 +1,6 @@
export * from "./Home";
export * from "./Administration";
+export * from "./Error";
export * from "./SignIn";
export * from "./SignUp";
export * from "./CompleteSignInProfile";