Skip to content

Commit b78b276

Browse files
committed
Adding error reporting
1 parent 9506936 commit b78b276

File tree

7 files changed

+127
-5
lines changed

7 files changed

+127
-5
lines changed

package-lock.json

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6+
"@sentry/browser": "^5.15.4",
67
"aws-amplify": "^3.0.6",
78
"react": "^16.13.1",
89
"react-bootstrap": "^0.33.1",

src/App.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Auth } from "aws-amplify";
33
import { Link, useHistory } from "react-router-dom";
44
import { Nav, Navbar, NavItem } from "react-bootstrap";
55
import { LinkContainer } from "react-router-bootstrap";
6+
import ErrorBoundary from "./components/ErrorBoundary";
67
import { AppContext } from "./libs/contextLib";
78
import { onError } from "./libs/errorLib";
89
import Routes from "./Routes";
@@ -71,11 +72,11 @@ function App() {
7172
</Nav>
7273
</Navbar.Collapse>
7374
</Navbar>
74-
<AppContext.Provider
75-
value={{ isAuthenticated, userHasAuthenticated }}
76-
>
77-
<Routes />
78-
</AppContext.Provider>
75+
<ErrorBoundary>
76+
<AppContext.Provider value={{ isAuthenticated, userHasAuthenticated }}>
77+
<Routes />
78+
</AppContext.Provider>
79+
</ErrorBoundary>
7980
</div>
8081
)
8182
);

src/components/ErrorBoundary.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.ErrorBoundary {
2+
padding-top: 100px;
3+
text-align: center;
4+
}

src/components/ErrorBoundary.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from "react";
2+
import { logError } from "../libs/errorLib";
3+
import "./ErrorBoundary.css";
4+
5+
export default class ErrorBoundary extends React.Component {
6+
state = { hasError: false };
7+
8+
static getDerivedStateFromError(error) {
9+
return { hasError: true };
10+
}
11+
12+
componentDidCatch(error, errorInfo) {
13+
logError(error, errorInfo);
14+
}
15+
16+
render() {
17+
return this.state.hasError ? (
18+
<div className="ErrorBoundary">
19+
<h3>Sorry there was a problem loading this page</h3>
20+
</div>
21+
) : (
22+
this.props.children
23+
);
24+
}
25+
}

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import { BrowserRouter as Router } from 'react-router-dom';
55
import './index.css';
66
import App from './App';
77
import config from './config';
8+
import { initSentry } from './libs/errorLib';
89
import * as serviceWorker from './serviceWorker';
910

11+
initSentry();
12+
1013
Amplify.configure({
1114
Auth: {
1215
mandatorySignIn: true,

src/libs/errorLib.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
1+
import * as Sentry from "@sentry/browser";
2+
3+
const isLocal = process.env.NODE_ENV === "development";
4+
5+
export function initSentry() {
6+
if (isLocal) {
7+
return;
8+
}
9+
10+
Sentry.init({ dsn: "https://[email protected]/5185720" });
11+
}
12+
13+
export function logError(error, errorInfo = null) {
14+
if (isLocal) {
15+
return;
16+
}
17+
18+
Sentry.withScope((scope) => {
19+
errorInfo && scope.setExtras(errorInfo);
20+
Sentry.captureException(error);
21+
});
22+
}
23+
124
export function onError(error) {
25+
let errorInfo = {};
226
let message = error.toString();
327

428
// Auth errors
529
if (!(error instanceof Error) && error.message) {
30+
errorInfo = error;
631
message = error.message;
32+
error = new Error(message);
33+
// API errors
34+
} else if (error.config && error.config.url) {
35+
errorInfo.url = error.config.url;
736
}
837

38+
logError(error, errorInfo);
39+
940
alert(message);
1041
}

0 commit comments

Comments
 (0)