1
Fork 0
mirror of https://github.com/Steffo99/bluelib.git synced 2024-12-22 19:44:21 +00:00

Add basic error boundary to catch errors

This commit is contained in:
Steffo 2021-02-02 02:39:37 +01:00
parent 77c147df97
commit a01b28bd54
Signed by: steffo
GPG key ID: 6965406171929D01
5 changed files with 106 additions and 0 deletions

View file

@ -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.");
}
<Bluelib>
<VisualErrorBoundary>
<ErrorMaker/>
</VisualErrorBoundary>
</Bluelib>
```

View file

@ -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 (
<VisualLog level={"error"}>{this.state.error.toString()}</VisualLog>
)
}
else {
return this.props.children;
}
}
}

View file

@ -0,0 +1,31 @@
A displayer of debug information.
```jsx
import Bluelib from "../Bluelib";
<Bluelib>
<div>
<VisualLog level={"debug"}>
Debug: Shields are active.
</VisualLog>
</div>
<div>
<VisualLog level={"info"}>
Info: Asteroid approaching.
</VisualLog>
</div>
<div>
<VisualLog level={"warning"}>
Warning: An asteroid damaged our shield.
</VisualLog>
</div>
<div>
<VisualLog level={"error"}>
Error: Shields are not responsive.
</VisualLog>
</div>
</Bluelib>
```

View file

@ -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 (
<Color value={COLORS[level]}>
{EMOJIS[level]} {children}
</Color>
)
}
VisualLog.propTypes = {
children: PropTypes.node,
level: PropTypes.oneOf(["error", "warning", "info", "debug"])
}

View file

@ -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";