diff --git a/webapp/src/App.tsx b/webapp/src/App.tsx index d015157..b3dfdff 100644 --- a/webapp/src/App.tsx +++ b/webapp/src/App.tsx @@ -2,16 +2,19 @@ import './App.css'; import { Routes, Route, BrowserRouter } from "react-router-dom"; import { AutomationRoute } from 'routes/automation'; import { useConnectedApiService } from 'apiService'; +import { ErrorBoundary } from 'components/ErrorBoundary'; function App() { const api = useConnectedApiService(); return (
- - - } /> - - + + + + } /> + + +
); } diff --git a/webapp/src/apiService/base.ts b/webapp/src/apiService/base.ts index 2529dd9..c9deebe 100644 --- a/webapp/src/apiService/base.ts +++ b/webapp/src/apiService/base.ts @@ -1,14 +1,15 @@ import { APIRequest, APIResponse } from './types'; export interface API { - makeCall: (req: APIRequest) => Promise>; + makeCall: (req: APIRequest) => Promise>; } export const makeRemoteAPI = (baseURL: string): API => ({ async makeCall({ - path, - method = "POST", - data={} - }) { + path, + method = "POST", + data = {} + }) { + try { const reply = await fetch(baseURL + path, { method, headers: { @@ -30,5 +31,12 @@ export const makeRemoteAPI = (baseURL: string): API => ({ console.error(response) } return response + } catch (err) { + console.error(err) + return { + ok: false, + error: `${err} ${err?.stack}`, + } } + } }) diff --git a/webapp/src/components/ErrorBoundary/index.css b/webapp/src/components/ErrorBoundary/index.css new file mode 100644 index 0000000..077db8f --- /dev/null +++ b/webapp/src/components/ErrorBoundary/index.css @@ -0,0 +1,7 @@ +.error-boundary { + padding: 2em 5%; +} + +h1, h2 { + margin: 0; +} \ No newline at end of file diff --git a/webapp/src/components/ErrorBoundary/index.stories.tsx b/webapp/src/components/ErrorBoundary/index.stories.tsx new file mode 100644 index 0000000..2bfc1db --- /dev/null +++ b/webapp/src/components/ErrorBoundary/index.stories.tsx @@ -0,0 +1,34 @@ +import React, { FC } from "react"; +import { ComponentMeta, ComponentStory } from '@storybook/react'; +import { ErrorBoundary } from "."; + + +export default { + title: 'ErrorBoundary', + component: ErrorBoundary, + parameters: { actions: { argTypesRegex: '^on.*' } }, + args: { + } +} as ComponentMeta + +export const ErrorThrown: ComponentStory = args => { + const BadComponent: FC = () => { + throw new Error("!!!") + } + return
+ + + +
+} + +export const NoError: ComponentStory = args => { + const GoodComponent: FC = () => { + return All Good. + } + return
+ + + +
+} \ No newline at end of file diff --git a/webapp/src/components/ErrorBoundary/index.tsx b/webapp/src/components/ErrorBoundary/index.tsx new file mode 100644 index 0000000..c35b1df --- /dev/null +++ b/webapp/src/components/ErrorBoundary/index.tsx @@ -0,0 +1,44 @@ +import "./index.css"; +import { Component, ErrorInfo } from "react"; + +type State = { + hasError: false; +} | { + hasError: true, + error: any, + errorInfo?: ErrorInfo, +} +export class ErrorBoundary extends Component<{}, State> { + + state: State = { + hasError: false + } + + static getDerivedStateFromError(error: any): State { + return { error, hasError: true, }; + } + + componentDidCatch(error: Error, errorInfo: ErrorInfo) { + this.setState({ + hasError: true, + error, + errorInfo, + }) + } + + render() { + if (this.state.hasError) { + return
+

An Error has occured.

+

{String(this.state.error)}

+ {JSON.stringify(this.state.error, null, 4)} +
+ {this.state.errorInfo && this.state.errorInfo.componentStack.split('\n').map((s, i) => <> + {s}
+ )} +
+ } + + return this.props.children; + } +} \ No newline at end of file