Skip to content

Commit dacadcf

Browse files
read me updates and react sample
1 parent 38a7d31 commit dacadcf

File tree

6 files changed

+255
-68
lines changed

6 files changed

+255
-68
lines changed

example/react/README.md

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,15 @@
1-
# Getting Started with Create React App
1+
## Exceptionless React Example
22

3-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
3+
This example shows how to use the `@exceptionless/react` package. There is both a class component example (App.js) and a function component example with hooks (HooksExampleApp.js).
44

5-
## Available Scripts
5+
The package includes [error boundary support](https://reactjs.org/docs/error-boundaries.html) which means uncaught errors inside your components will automatically be sent to Exceptionless.
66

7-
In the project directory, you can run:
7+
To run locally, follow these steps:
88

9-
### `yarn start`
9+
1. `git clone https://github.com/exceptionless/Exceptionless.JavaScript`
10+
2. `cd Exceptionless.Javascript`
11+
3. `npm install`
12+
4. `cd example/react`
13+
5. `npm start`
1014

11-
Runs the app in the development mode.\
12-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13-
14-
The page will reload if you make edits.\
15-
You will also see any lint errors in the console.
16-
17-
### `yarn test`
18-
19-
Launches the test runner in the interactive watch mode.\
20-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21-
22-
### `yarn build`
23-
24-
Builds the app for production to the `build` folder.\
25-
It correctly bundles React in production mode and optimizes the build for the best performance.
26-
27-
The build is minified and the filenames include the hashes.\
28-
Your app is ready to be deployed!
29-
30-
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31-
32-
### `yarn eject`
33-
34-
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35-
36-
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37-
38-
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39-
40-
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41-
42-
## Learn More
43-
44-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45-
46-
To learn React, check out the [React documentation](https://reactjs.org/).
47-
48-
### Code Splitting
49-
50-
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51-
52-
### Analyzing the Bundle Size
53-
54-
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55-
56-
### Making a Progressive Web App
57-
58-
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59-
60-
### Advanced Configuration
61-
62-
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63-
64-
### Deployment
65-
66-
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67-
68-
### `yarn build` fails to minify
69-
70-
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
15+
Reference the main `@exceptionless/react` [README](../../packages/react/README.md) here when building your own React app.

example/react/src/App.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
color: #61dafb;
2929
}
3030

31+
.container {
32+
max-width: 85%;
33+
margin: auto;
34+
}
35+
3136
@keyframes App-logo-spin {
3237
from {
3338
transform: rotate(0deg);

example/react/src/App.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ class App extends Component {
1010
super(props);
1111
this.state = {
1212
error: false,
13+
message: "",
14+
errorInfo: "",
1315
};
1416
}
1517
async componentDidMount() {
1618
await Exceptionless.startup((c) => {
17-
c.apiKey = "YOUR API KEY";
19+
c.apiKey = "LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw";
20+
c.serverUrl = "http://localhost:5000";
1821
c.useDebugLogger();
1922

2023
c.defaultTags.push("Example", "React");
@@ -25,15 +28,59 @@ class App extends Component {
2528
this.setState({ error: true });
2629
};
2730

31+
submitMessage = () => {
32+
const message = "Hello, world!";
33+
this.setState({ message: "", errorInfo: "" });
34+
Exceptionless.submitLog(message);
35+
this.setState({ message });
36+
};
37+
38+
tryCatchExample = () => {
39+
try {
40+
this.setState({ message: "", errorInfo: "" });
41+
throw new Error("Caught in the try/catch");
42+
} catch (error) {
43+
this.setState({ errorInfo: error.message });
44+
Exceptionless.submitException(error);
45+
}
46+
};
47+
2848
renderExample = () => {
2949
if (this.state.error) {
3050
throw new Error("I crashed!");
3151
} else {
3252
return (
3353
<div className="App">
3454
<header className="App-header">
35-
<h1 className="App-title">Exceptionless React Sample</h1>
36-
<button onClick={this.throwErrorInComponent}>Throw Error</button>
55+
<div className="container">
56+
<h1 className="App-title">Exceptionless React Sample</h1>
57+
<p>
58+
By pressing the button below, an uncaught error will be thrown
59+
inside your component. This will automatically be sent to
60+
Exceptionless.
61+
</p>
62+
<button onClick={this.throwErrorInComponent}>
63+
Simulate Error
64+
</button>
65+
<p>
66+
The following buttons simulated handled events outside the
67+
component.
68+
</p>
69+
<button onClick={this.submitMessage}>Submit Message</button>
70+
{this.state.message && (
71+
<p>
72+
Message sent to Exceptionless:{" "}
73+
<code>{this.state.message}</code>
74+
</p>
75+
)}
76+
<button onClick={this.tryCatchExample}>Try/Catch Example</button>
77+
{this.state.errorInfo && (
78+
<p>
79+
Error message sent to Exceptionless:{" "}
80+
<code>{this.state.errorInfo}</code>
81+
</p>
82+
)}
83+
</div>
3784
</header>
3885
</div>
3986
);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import React, { useEffect, useState } from "react";
2+
import "./App.css";
3+
import {
4+
Exceptionless,
5+
ExceptionlessErrorBoundary,
6+
} from "@exceptionless/react";
7+
8+
const HooksExampleApp = () => {
9+
const [error, setError] = useState(false);
10+
useEffect(() => {
11+
startExceptionless();
12+
}, []);
13+
14+
const startExceptionless = async () => {
15+
await Exceptionless.startup((c) => {
16+
c.apiKey = "LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw";
17+
c.serverUrl = "http://localhost:5000";
18+
c.useDebugLogger();
19+
20+
c.defaultTags.push("Example", "React");
21+
});
22+
};
23+
24+
const throwErrorInComponent = () => {
25+
setError(true);
26+
};
27+
28+
const submitMessage = () => {
29+
Exceptionless.submitLog("Hello, world!");
30+
};
31+
32+
const tryCatchExample = () => {
33+
try {
34+
throw new Error("Caught in the try/catch");
35+
} catch (error) {
36+
Exceptionless.submitException(error);
37+
}
38+
};
39+
40+
const renderExample = () => {
41+
if (error) {
42+
throw new Error("I crashed!");
43+
} else {
44+
return (
45+
<div className="App">
46+
<header className="App-header">
47+
<div className="container">
48+
<h1 className="App-title">Exceptionless React Sample</h1>
49+
<p>
50+
By pressing the button below, an uncaught error will be thrown
51+
inside your component. This will automatically be sent to
52+
Exceptionless.
53+
</p>
54+
<button onClick={throwErrorInComponent}>Simulate Error</button>
55+
<p>
56+
The following buttons simulated handled events outside the
57+
component.
58+
</p>
59+
<button onClick={submitMessage}>Submit Message</button>
60+
<button onClick={tryCatchExample}>Try/Catch Example</button>
61+
</div>
62+
</header>
63+
</div>
64+
);
65+
}
66+
};
67+
68+
return (
69+
<ExceptionlessErrorBoundary>{renderExample()}</ExceptionlessErrorBoundary>
70+
);
71+
};
72+
73+
export default HooksExampleApp;

packages/browser/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## Exceptionless Browser
2+
3+
This package provides native JavaScript support for applications that are built in vanilla HTML and JS.
4+
5+
### Installation
6+
7+
**Package Managers**
8+
9+
`npm install @exceptionless/browser`
10+
11+
or
12+
13+
`yarn add @exceptionless/browser`
14+
15+
16+
### Configuration
17+
18+
```js
19+
await Exceptionless.startup(c => {
20+
c.useDebugLogger();
21+
c.services.log = new TextAreaLogger("logs", c.services.log);
22+
23+
c.apiKey = "LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw";
24+
c.serverUrl = "http://localhost:5000";
25+
c.updateSettingsWhenIdleInterval = 15000;
26+
c.usePersistedQueueStorage = true;
27+
c.setUserIdentity("12345678", "Blake");
28+
c.useSessions();
29+
30+
// set some default data
31+
c.defaultData["SampleUser"] = {
32+
id: 1,
33+
name: "Blake",
34+
password: "123456",
35+
passwordResetToken: "a reset token",
36+
myPasswordValue: "123456",
37+
myPassword: "123456",
38+
customValue: "Password",
39+
value: {
40+
Password: "123456"
41+
}
42+
};
43+
44+
c.defaultTags.push("Example", "JavaScript");
45+
c.settings["@@error:MediaError"] = "Off"
46+
});
47+
```

packages/react/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## Exceptionless React
2+
3+
The Exceptionless React package provides a native way to handle errors and events in React. This means errors inside your components, which tend to crash your entire app, can be sent to Exceptionless and you can be alerted. Additionally, you can use this package to catch errors throughout your non-component functions such as in Redux actions, utility functions, etc.
4+
5+
### Getting Started
6+
7+
To use this package, your must be using ES6 and Node 15+.
8+
9+
**Install**
10+
11+
NPM:
12+
13+
`npm install @exceptionless/react`
14+
15+
Yarn:
16+
17+
`yarn add @exceptionless/react`
18+
19+
### Configuration
20+
21+
Inside your `index.js` file or your `App.js` file, you can configure and start Exceptionless as follows.
22+
23+
**Class Components**
24+
25+
```jsx
26+
class App extends Component {
27+
async componentDidMount() {
28+
await Exceptionless.startup((c) => {
29+
c.apiKey = "LhhP1C9gijpSKCslHHCvwdSIz298twx271n1l6xw"; //Replace with your API key
30+
c.serverUrl = "http://localhost:5000"; //Remove if using the hosted version of Exceptionless
31+
});
32+
}
33+
34+
render() {
35+
return (
36+
<ExceptionlessErrorBoundary>
37+
<div>
38+
// YOUR APP COMPONENTS HERE
39+
</div>
40+
</ExceptionlessErrorBoundary>
41+
);
42+
}
43+
}
44+
45+
export default App;
46+
```
47+
48+
### Handling Events
49+
50+
While errors within the components themselves are automatically sent to Exceptionless, you will still want to handle events that happen outside the components.
51+
52+
Because the Exceptionless client is a singleton, it is available anywhere in your app where you import it. Here's an example from a file we'll call `utilities.js`.
53+
54+
```js
55+
export const myUtilityFunction = () => {
56+
try {
57+
// Handle successful run of code
58+
} catch(e) {
59+
// If there's an error, send it to Exceptionless
60+
Exceptionless.submitException(e);
61+
}
62+
}
63+
```
64+
65+
You can also sent events and logs that are not errors by simply calling the built-in methods on the Exceptionless class:
66+
67+
```js
68+
Exceptionless.submitLog("Hello, world!");
69+
Exceptionless.submitFeatureUsage("New Shopping Cart Feature");
70+
```

0 commit comments

Comments
 (0)