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
Tidy up markdown formatting, including moving some links to the end
in reference form.
Rework the setup examples and show ES6 form as an option.
Reword some text for clarity.
[![Build Status][travis-ci status image]][travis-ci status link]
6
+
[![Dependency Status][david-dm status image]][david-dm status link]
7
7
8
-
This **experimental** library provides Stackdriver Error Reporting support for client-side web JavaScript applications.
9
-
[Stackdriver Error Reporting](https://cloud.google.com/error-reporting/) is a feature of Google Cloud Platform that allows in-depth monitoring and viewing of errors reported by applications running in almost any environment. For server-side Node.js error reporting, use [this other library](https://github.com/GoogleCloudPlatform/cloud-errors-nodejs).
8
+
This **experimental** library provides Stackdriver Error Reporting support for client-side web JavaScript applications. [Stackdriver Error Reporting](https://cloud.google.com/error-reporting/) is a feature of Google Cloud Platform that allows in-depth monitoring and viewing of errors reported by applications running in almost any environment. For server-side Node.js error reporting, use [cloud-errors-nodejs](https://github.com/GoogleCloudPlatform/cloud-errors-nodejs) instead.
10
9
11
10
Here's an introductory video:
12
11
13
-
[](https://www.youtube.com/watch?v=cVpWVD75Hs8)
12
+
[![Learn about Error Reporting in Stackdriver][video thumbnail]][video link]
14
13
15
14
## Prerequisites
16
15
17
16
1. You need a [Google Cloud project](https://console.cloud.google.com).
18
-
1.[Enable the Stackdriver Error Reporting API](https://console.cloud.google.com/apis/api/clouderrorreporting.googleapis.com/overview) for your project. We highly recommend to restrict the usage of the key to your website URL only using an 'HTTP referrer' restriction.
19
-
1. Create a browser API key:
20
17
21
-
- Follow [these instructions](https://support.google.com/cloud/answer/6158862) to get an API key for your project.
22
-
- Recommended: Use **Application restrictions** to restrict this key to your website.
23
-
- Recommended: Use **API restrictions** to limit this key to the *Stackdriver Error Reporting API*.
18
+
2.[Enable the Stackdriver Error Reporting API](https://console.cloud.google.com/apis/api/clouderrorreporting.googleapis.com/overview) for your project. We highly recommend to restrict the usage of the key to your website URL only using an 'HTTP referrer' restriction.
24
19
25
-
If API keys are not an option for your team, you can [use a custom url](#configuring-without-an-api-key) to send your errors to your backend.
20
+
3. Create a browser API key:
21
+
- Follow [using api keys instructions](https://support.google.com/cloud/answer/6158862) to get an API key for your project.
22
+
- Recommended: Use **Application restrictions** to restrict this key to your website.
23
+
- Recommended: Use **API restrictions** to limit this key to the *Stackdriver Error Reporting API*.
24
+
25
+
If API keys are not an option for your team, [use a custom url](
26
+
#configuring-without-an-api-key) to send your errors to your backend.
26
27
27
28
## Quickstart
28
29
29
-
**Load and initialize the experimental library**
30
+
The library can either be used as a standalone script, or incorporated as a module into a larger javascript application.
30
31
31
-
Add this line in your HTML code, before `</head>` and replace `<my-api-key>` and `<my-project-id>` with your API key and Google Cloud project ID string:
32
+
For use in any HTML page or without a specific framework, include the standalone script from CDN and set up the error handler in a page load event. For instance, use include the following HTML in the page `<head>` and replace `<my-api-key>` and `<my-project-id>` with your API key and Google Cloud project ID string:
32
33
33
34
```HTML
34
-
<!-- Warning: This is an experimental library, do not use it on production environments -->
35
+
<!-- Warning: Experimental library, do not use in production environments.-->
Open Stackdriver Error Reporting at https://console.cloud.google.com/errors to view the error and opt-in to notifications on new errors.
60
+
Open [Stackdriver Error Reporting](https://console.cloud.google.com/errors) to view the error and opt-in to notifications on new errors.
57
61
58
62
59
63
## Setup for JavaScript
60
64
61
-
### Download the module
65
+
### Installing
62
66
63
67
We recommend using npm: `npm install stackdriver-errors-js --save`.
64
68
65
69
### Initialization
66
70
67
-
Here are all the initialization options available:
71
+
Create a file that is included in your application entry point and has access to variables `myApiKey` and `myProjectId`. For ES6 projects it can be in the form:
// The following optional arguments can also be provided:
83
+
84
+
// service: myServiceName,
85
+
// Name of the service reporting the error, defaults to 'web'.
86
+
87
+
// version: myServiceVersion,
88
+
// Version identifier of the service reporting the error.
89
+
90
+
// reportUncaughtExceptions: false
91
+
// Set to false to prevent reporting unhandled exceptions.
92
+
93
+
// reportUnhandledPromiseRejections: false
94
+
// Set to false to prevent reporting unhandled promise rejections.
95
+
96
+
// disabled: true
97
+
// Set to true to not send error reports, this can be used when developing locally.
98
+
99
+
// context: {user: 'user1'}
100
+
// You can set the user later using setUser()
85
101
});
86
-
</script>
87
102
```
88
103
89
-
### Usage
104
+
Note this uses the ES6 import syntax, if your project does not use a compilation step, instead the source with dependencies and polyfills bundled can be used directly:
90
105
91
-
Unhandled exception will now automatically be reported to Stackdriver Error Reporting.
106
+
```javascript
107
+
var StackdriverErrorReporter =require('stackdriver-errors-js/dist/stackdriver-errors-concat.min.js');
92
108
93
-
You can also change your application code to report errors: `try { ... } catch(e) { errorHandler.report(e); }` or simply `errorHandler.report('Something broke!');`.
109
+
var errorHandler =newStackdriverErrorReporter();
110
+
errorHandler.start({
111
+
key: myApiKey,
112
+
projectId: myProjectId,
113
+
// Other optional arguments can be supplied, see above.
114
+
});
115
+
```
94
116
95
-
You can set a user identifier at any time using `errorHandler.setUser('userId')`.
117
+
### Usage
96
118
97
-
### Source maps
119
+
Unhandled exception will now automatically be sent to Stackdriver Error Reporting.
98
120
99
-
Only publicly available JavaScript source maps are supported.
121
+
You can also change your application code to report errors:
100
122
101
-
Your minified file need to be appended with a comment directive to your source map file:
Uncaught exception in angular expressions will now be reported to Stackdriver Error Reporting.
137
172
138
-
If you wish, you can manually delegate exceptions, e.g. `try { ... } catch(e) { $exceptionHandler(e); }` or simply `$exceptionHandler('Something broke!');`.
173
+
If you wish, you can manually delegate exceptions, for instance:
174
+
```javascript
175
+
try { ... } catch(e) { $exceptionHandler(e); }
176
+
```
177
+
Or simply:
178
+
```javascript
179
+
$exceptionHandler('Something broke!');
180
+
```
139
181
140
182
## Setup for ReactJS
141
183
142
184
Follow the general instructions denoted in _Setup for JavaScript_ to load and initialize the library.
143
185
144
-
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`).
186
+
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`).
187
+
188
+
## Source maps
189
+
190
+
Only publicly available JavaScript source maps are supported.
191
+
192
+
Your minified file need to be appended with a comment directive to your source map file:
### Only reporting in the production environment with Webpack
181
233
182
-
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:
234
+
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:
264
+
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:
// Other optional arguments can be supplied, see above.
232
278
});
233
-
234
279
} else {
235
280
errorHandler = {report:console.error};
236
281
}
237
282
238
283
exportdefaulterrorHandler;
239
-
240
284
```
241
285
242
-
Consumption of the errorHandlerUtility would essentially follow the following pattern:
286
+
Consumption of `errorHandlerUtility` would essentially follow the following pattern:
243
287
244
288
```javascript
245
-
// MyComponent.jsx
246
289
importerrorHandlerfrom'./errorHandlerUtility';
247
290
248
291
try {
249
292
someFunctionThatThrows();
250
293
} catch (error) {
251
294
errorHandler.report(error);
252
295
}
253
-
254
296
```
255
297
256
-
If the call to report has additional levels of wrapping code, extra
257
-
frames can be trimmed from the top of generated stacks by using a
258
-
number greater than one for the `skipLocalFrames` option:
298
+
If the call to report has additional levels of wrapping code, extra frames can be trimmed from the top of generated stacks by using a number greater than one for the `skipLocalFrames` option:
259
299
260
300
```javascript
261
301
importerrorHandlerfrom'./errorHandlerUtility';
@@ -264,13 +304,23 @@ function backendReport (string) {
264
304
// Skipping the two frames, for report() and for backendReport()
265
305
errorHandler.report(error, {skipLocalFrames:2});
266
306
}
267
-
268
307
```
269
308
270
309
## FAQ
271
310
272
-
**Q: Should I use this code in my production application?** A: This is an experimental library provided without any guarantee or official support. We do not recommend using it on production without performing a review of its code.
311
+
**Q: Should I use this code in my production application?**
312
+
A: This is an experimental library provided without any guarantee or official support. We do not recommend using it on production without performing a review of its code.
313
+
314
+
**Q: Are private source maps supported?**
315
+
A: No, see [issue #4](https://github.com/GoogleCloudPlatform/stackdriver-errors-js/issues/4).
316
+
317
+
**Q: Can I propose changes to the library?**
318
+
A: Yes, see [the Contributing documentation](CONTRIBUTING.md) for more details.
273
319
274
-
**Q: Are private source maps supported?** A: No, see [#4](https://github.com/GoogleCloudPlatform/stackdriver-errors-js/issues/4)
275
320
276
-
**Q: Can I propose changes to the library?** A: Yes, see [the Contributing documentation](CONTRIBUTING.md) for more details.
321
+
[travis-ci status image]: https://travis-ci.org/GoogleCloudPlatform/stackdriver-errors-js.svg?branch=master
322
+
[travis-ci status link]: https://travis-ci.org/GoogleCloudPlatform/stackdriver-errors-js
323
+
[david-dm status image]: https://david-dm.org/GoogleCloudPlatform/stackdriver-errors-js.svg
324
+
[david-dm status link]: https://david-dm.org/GoogleCloudPlatform/stackdriver-errors-js
0 commit comments