From a01b28bd548cacde8bc4f118c40e15ef294ab440 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 2 Feb 2021 02:39:37 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20basic=20error=20boundary=20to?= =?UTF-8?q?=20catch=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/VisualErrorBoundary/Readme.md | 15 +++++++++ src/components/VisualErrorBoundary/index.js | 25 +++++++++++++++ src/components/VisualLog/Readme.md | 31 ++++++++++++++++++ src/components/VisualLog/index.js | 33 ++++++++++++++++++++ src/components/index.js | 2 ++ 5 files changed, 106 insertions(+) create mode 100644 src/components/VisualErrorBoundary/Readme.md create mode 100644 src/components/VisualErrorBoundary/index.js create mode 100644 src/components/VisualLog/Readme.md create mode 100644 src/components/VisualLog/index.js diff --git a/src/components/VisualErrorBoundary/Readme.md b/src/components/VisualErrorBoundary/Readme.md new file mode 100644 index 0000000..e731d78 --- /dev/null +++ b/src/components/VisualErrorBoundary/Readme.md @@ -0,0 +1,15 @@ +A basic error boundary which detects errors and displays them. + +```jsx +import Bluelib from "../Bluelib"; + +function ErrorMaker() { + throw new Error("ErrorMaker threw an error."); +} + + + + + + +``` diff --git a/src/components/VisualErrorBoundary/index.js b/src/components/VisualErrorBoundary/index.js new file mode 100644 index 0000000..b092ec4 --- /dev/null +++ b/src/components/VisualErrorBoundary/index.js @@ -0,0 +1,25 @@ +import React from "react"; +import VisualLog from "../VisualLog"; + + +export default class VisualErrorBoundary extends React.Component { + constructor(props) { + super(props); + this.state = {"error": null, "errorInfo": null}; + } + + componentDidCatch(error, errorInfo) { + this.setState({"error": error, "errorInfo": errorInfo}); + } + + render() { + if(this.state.error) { + return ( + {this.state.error.toString()} + ) + } + else { + return this.props.children; + } + } +} diff --git a/src/components/VisualLog/Readme.md b/src/components/VisualLog/Readme.md new file mode 100644 index 0000000..7d1e3c1 --- /dev/null +++ b/src/components/VisualLog/Readme.md @@ -0,0 +1,31 @@ +A displayer of debug information. + +```jsx +import Bluelib from "../Bluelib"; + + +
+ + Debug: Shields are active. + +
+ +
+ + Info: Asteroid approaching. + +
+ +
+ + Warning: An asteroid damaged our shield. + +
+ +
+ + Error: Shields are not responsive. + +
+
+``` diff --git a/src/components/VisualLog/index.js b/src/components/VisualLog/index.js new file mode 100644 index 0000000..e3261a6 --- /dev/null +++ b/src/components/VisualLog/index.js @@ -0,0 +1,33 @@ +import React from "react"; +import Color from "../Color"; +import PropTypes from "prop-types"; + + +const COLORS = { + "error": "red", + "warning": "yellow", + "info": "blue", + "debug": "magenta", +} + +const EMOJIS = { + "error": "⛔️", + "warning": "⚠️", + "info": "ℹ️", + "debug": "🐛", +} + + +export default function VisualLog({children, level}) { + return ( + + {EMOJIS[level]} {children} + + ) +} + + +VisualLog.propTypes = { + children: PropTypes.node, + level: PropTypes.oneOf(["error", "warning", "info", "debug"]) +} \ No newline at end of file diff --git a/src/components/index.js b/src/components/index.js index 1e70bb4..b6dad3d 100644 --- a/src/components/index.js +++ b/src/components/index.js @@ -22,3 +22,5 @@ export {default as Table} from "./Table"; export {default as Title} from "./Title"; export {default as Underline} from "./Underline"; export {default as BaseLink} from "./BaseLink"; +export {default as VisualErrorBoundary} from "./VisualErrorBoundary"; +export {default as VisualLog} from "./VisualLog";