You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- There are numerous options to how the library
can be consumed and used within a ReactJs application
and the examples/instructions given in the README.md
are merely examples of how the stackdriver-error-js
library can be instrumented within a ReactJs
application.
- Once the library is published to npm, the instructions
to import the module will be updated accordingly.
- Additition of a best practices section which helps
guide users on how the library can be used in conjunction
with Webpack and as a standalone modular utility.
Addresses #11
Copy file name to clipboardExpand all lines: README.md
+81Lines changed: 81 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -119,7 +119,88 @@ Uncaught exception in angular expressions will now be reported to Stackdriver Er
119
119
120
120
If you wish, you can manually delegate exceptions, e.g. `try { ... } catch(e) { $exceptionHandler(e); }` or simply `$exceptionHandler('Something broke!');`.
121
121
122
+
## Setup for ReactJS
122
123
124
+
Follow the general instructions denoted in _Setup for JavaScript_ to load and initialize the library.
125
+
126
+
There is nothing specific that needs to be done with React, other than making sure to initialize the library in your root entry point(typically `index.js`).
127
+
128
+
## Best Practices
129
+
130
+
### Only reporting in the production environment with Webpack
131
+
132
+
If using webpack and the `DefinePlugin`, it is advisable to wrap the initialization logic to only occur in your production environment. Otherwise, with local development you will receive 403s if you restricted your API key to your production environment(which is _HIGHLY_ recommended). The code for this would look something along these lines:
If you would like to use the error logger throughout your application, there are many options that exist. The simplest is to pull the initialization logic into its own file and reference it as necessary throughout your application as a module. An example would be as follows:
166
+
167
+
```javascript
168
+
// errorHandlerUtility.js
169
+
170
+
constenvironment=process.env.NODE_ENV;
171
+
172
+
let errorHandler;
173
+
174
+
if (environment ==='production') {
175
+
176
+
errorHandler =newStackdriverErrorReporter();
177
+
errorHandler.start({
178
+
key:'<my-project-id>',
179
+
projectId:'<my-project-id>',
180
+
service:'<my-service>', // (optional)
181
+
version:'<my-service-version>'// (optional)
182
+
});
183
+
184
+
} else {
185
+
errorHandler =console.error;
186
+
}
187
+
188
+
exportdefaulterrorHandler;
189
+
190
+
```
191
+
192
+
Consumption of the errorHandlerUtility would essentially follow the following pattern:
0 commit comments