Skip to content

Commit 1865160

Browse files
docs: use repo metadata to generate docs (#372)
1 parent 964fa92 commit 1865160

File tree

9 files changed

+458
-185
lines changed

9 files changed

+458
-185
lines changed

.cloud-repo-tools.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

.readme-partials.yaml

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
introduction: |-
2+
> Node.js idiomatic client for [Error Reporting][product-docs].
3+
4+
[Stackdriver Error Reporting](https://cloud.google.com/error-reporting/docs/) aggregates and displays errors produced in your running cloud services.
5+
6+
body: |-
7+
This module provides custom Stackdriver Error Reporting support for Node.js applications.
8+
[Stackdriver Error Reporting](https://cloud.google.com/error-reporting/) is a feature of
9+
Google Cloud Platform that allows in-depth monitoring and viewing of errors reported by
10+
applications running in almost any environment.
11+
12+
However, note that [@google-cloud/logging-winston](https://github.com/googleapis/nodejs-logging-winston) and [@google-cloud/logging-bunyan](https://github.com/googleapis/nodejs-logging-bunyan) automatically integrate with the Error Reporting service for Error objects logged at severity `error` or higher, for applications running on Google Cloud Platform.
13+
14+
Thus, if you are already using Winston or Bunyan in your application, and don't need custom error reporting capabilities, you do not need to use the `@google-cloud/error-reporting` library directly to report errors to the Error Reporting Console.
15+
16+
![Stackdriver Error Reporting overview](https://raw.githubusercontent.com/googleapis/nodejs-error-reporting/master/doc/images/errors-overview.png)
17+
18+
Here's an introductory video that provides some more details:
19+
20+
[![Learn about Error Reporting in Stackdriver](https://img.youtube.com/vi/cVpWVD75Hs8/0.jpg)](https://www.youtube.com/watch?v=cVpWVD75Hs8)
21+
22+
# When Errors Are Reported
23+
24+
The `reportMode` configuration option is used to specify when errors are reported to the Error Reporting Console. It can have one of three values:
25+
* `'production'` (default): Only report errors if the NODE_ENV environment variable is set to "production".
26+
* `'always'`: Always report errors regardless of the value of NODE_ENV.
27+
* `'never'`: Never report errors regardless of the value of NODE_ENV.
28+
29+
The `reportMode` configuration option replaces the deprecated `ignoreEnvironmentCheck` configuration option. If both the `reportMode` and `ignoreEnvironmentCheck` options are specified, the `reportMode` configuration option takes precedence.
30+
31+
The `ignoreEnvironmentCheck` option should not be used. However, if it is used, and the `reportMode` option is not specified, it can have the values:
32+
* `false` (default): Only report errors if the NODE_ENV environment variable is set to "production".
33+
* `true`: Always report errors regardless of the value of NODE_ENV.
34+
35+
See the [Configuration](#configuration) section to learn how to specify configuration options.
36+
37+
## Configuration
38+
The following code snippet lists available configuration options. All configuration options are optional.
39+
40+
```js
41+
const {ErrorReporting} = require('@google-cloud/error-reporting');
42+
43+
// Using ES6 style imports via TypeScript or Babel
44+
// import {ErrorReporting} from '@google-cloud/error-reporting';
45+
46+
// Instantiates a client
47+
const errors = new ErrorReporting({
48+
projectId: 'my-project-id',
49+
keyFilename: '/path/to/keyfile.json',
50+
credentials: require('./path/to/keyfile.json'),
51+
// Specifies when errors are reported to the Error Reporting Console.
52+
// See the "When Errors Are Reported" section for more information.
53+
// Defaults to 'production'
54+
reportMode: 'production',
55+
// Determines the logging level internal to the library; levels range 0-5
56+
// where 0 indicates no logs should be reported and 5 indicates all logs
57+
// should be reported.
58+
// Defaults to 2 (warnings)
59+
logLevel: 2,
60+
serviceContext: {
61+
service: 'my-service',
62+
version: 'my-service-version'
63+
}
64+
});
65+
```
66+
67+
## Examples
68+
69+
### Reporting Manually
70+
71+
```js
72+
const {ErrorReporting} = require('@google-cloud/error-reporting');
73+
74+
// Using ES6 style imports via TypeScript or Babel
75+
// import {ErrorReporting} from '@google-cloud/error-reporting';
76+
77+
// Instantiates a client
78+
const errors = new ErrorReporting();
79+
80+
// Use the error message builder to customize all fields ...
81+
const errorEvt = errors.event()
82+
.setMessage('My error message')
83+
.setUser('root@nexus');
84+
errors.report(errorEvt, () => console.log('done!'));
85+
86+
// or just use a regular error ...
87+
errors.report(new Error('My error message'), () => console.log('done!'));
88+
89+
// or one can even just use a string.
90+
errors.report('My error message');
91+
```
92+
93+
The stack trace associated with an error can be viewed in the error reporting console.
94+
* If the `errors.report` method is given an `ErrorMessage` object built using the `errors.event` method, the stack trace at the point where the error event was constructed will be used.
95+
* If the `errors.report` method is given an `Error` object, the stack trace where the error was instantiated will be used.
96+
* If the `errors.report` method is given a string, the stack trace at the point where `errors.report` is invoked will be used.
97+
98+
### Using Express
99+
100+
```js
101+
const express = require('express');
102+
103+
const {ErrorReporting} = require('@google-cloud/error-reporting');
104+
105+
// Using ES6 style imports via TypeScript or Babel
106+
// import {ErrorReporting} from '@google-cloud/error-reporting';
107+
108+
// Instantiates a client
109+
const errors = new ErrorReporting();
110+
111+
const app = express();
112+
113+
app.get('/error', (req, res, next) => {
114+
res.send('Something broke!');
115+
next(new Error('Custom error message'));
116+
});
117+
118+
app.get('/exception', () => {
119+
JSON.parse('{\"malformedJson\": true');
120+
});
121+
122+
// Note that express error handling middleware should be attached after all
123+
// the other routes and use() calls. See [express docs][express-error-docs].
124+
app.use(errors.express);
125+
126+
app.listen(3000);
127+
```
128+
129+
### Using Hapi
130+
131+
```js
132+
const hapi = require('hapi');
133+
const {ErrorReporting} = require('@google-cloud/error-reporting');
134+
135+
// Using ES6 style imports via TypeScript or Babel
136+
// import {ErrorReporting} from '@google-cloud/error-reporting';
137+
138+
// Instantiates a client
139+
const errors = new ErrorReporting();
140+
141+
const server = new hapi.Server();
142+
server.connection({ port: 3000 });
143+
server.start();
144+
145+
server.route({
146+
method: 'GET',
147+
path: '/error',
148+
handler: (request, reply) => {
149+
reply('Something broke!');
150+
throw new Error('Custom error message');
151+
}
152+
});
153+
154+
server.register(errors.hapi);
155+
```
156+
157+
### Using Koa
158+
159+
```js
160+
const Koa = require('koa');
161+
const {ErrorReporting} = require('@google-cloud/error-reporting');
162+
163+
// Using ES6 style imports via TypeScript or Babel
164+
// import {ErrorReporting} from '@google-cloud/error-reporting';
165+
166+
// Instantiates a client
167+
const errors = new ErrorReporting();
168+
169+
const app = new Koa();
170+
171+
app.use(errors.koa);
172+
173+
app.use(function *(next) {
174+
//This will set status and message
175+
this.throw('Error Message', 500);
176+
});
177+
178+
// response
179+
app.use(function *(){
180+
this.body = 'Hello World';
181+
});
182+
183+
app.listen(3000);
184+
```
185+
186+
### Using Restify
187+
188+
```js
189+
const restify = require('restify');
190+
const {ErrorReporting} = require('@google-cloud/error-reporting');
191+
192+
// Using ES6 style imports via TypeScript or Babel
193+
// import {ErrorReporting} from '@google-cloud/error-reporting';
194+
195+
// Instantiates a client
196+
const errors = new ErrorReporting();
197+
198+
function respond(req, res, next) {
199+
next(new Error('this is a restify error'));
200+
}
201+
202+
const server = restify.createServer();
203+
204+
server.use(errors.restify(server));
205+
server.get('/hello/:name', respond);
206+
server.head('/hello/:name', respond);
207+
208+
server.listen(3000);
209+
```
210+
211+
## Unhandled Rejections
212+
213+
Unhandled Rejections are not reported by default. The reporting of unhandled rejections can be enabled using the `reportUnhandledRejections` configuration option. See the [Configuration](#configuration) section for more details.
214+
215+
If unhandled rejections are set to be reported, then, when an unhandled rejection occurs, a message is printed to standard out indicated that an unhandled rejection had occurred and is being reported, and the value causing the rejection is reported to the error-reporting console.
216+
217+
## Catching and Reporting Application-wide Uncaught Errors
218+
219+
Uncaught exceptions are not reported by default. *It is recommended to process `uncaughtException`s for production-deployed applications.*
220+
221+
Note that uncaught exceptions are not reported by default because to do so would require adding a listener to the `uncaughtException` event. Adding such a listener without knowledge of other `uncaughtException` listeners can cause interference between the event handlers or prevent the process from terminating cleanly. As such, it is necessary for `uncaughtException`s to be reported manually.
222+
223+
```js
224+
const {ErrorReporting} = require('@google-cloud/error-reporting');
225+
226+
// Using ES6 style imports via TypeScript or Babel
227+
// import {ErrorReporting} from '@google-cloud/error-reporting';
228+
229+
// Instantiates a client
230+
const errors = new ErrorReporting();
231+
232+
process.on('uncaughtException', (e) => {
233+
// Write the error to stderr.
234+
console.error(e);
235+
// Report that same error the Stackdriver Error Service
236+
errors.report(e);
237+
});
238+
```
239+
240+
More information about uncaught exception handling in Node.js and what it means for your application can be found [here](https://nodejs.org/api/process.html#process_event_uncaughtexception).
241+
242+
### Using an API Key
243+
244+
You may use an API key in lieu of locally-stored credentials. Please see [this document](https://support.google.com/cloud/answer/6158862) on how to set up an API key if you do not already have one.
245+
246+
Once you have obtained an API key, you may provide it as part of the Error Reporting instance configuration:
247+
248+
```js
249+
const {ErrorReporting} = require('@google-cloud/error-reporting');
250+
251+
// Using ES6 style imports via TypeScript or Babel
252+
// import {ErrorReporting} from '@google-cloud/error-reporting';
253+
254+
// Instantiates a client
255+
const errors = new ErrorReporting({
256+
projectId: '{your project ID}',
257+
key: '{your api key}'
258+
});
259+
```
260+
261+
If a key is provided, the module will not attempt to authenticate using the methods associated with locally-stored credentials. We recommend using a file, environment variable, or another mechanism to store the API key rather than hard-coding it into your application's source.
262+
263+
**Note:** The Error Reporting instance will check if the provided API key is invalid shortly after it is instantiated. If the key is invalid, an error-level message will be logged to stdout.
264+
265+
### Long Stack Traces
266+
267+
The [longjohn](https://www.npmjs.com/package/longjohn) module can be used with this library to enable [long-stack-traces](https://github.com/tlrobinson/long-stack-traces) and updates an `Error` object's stack trace, by adding special line, to indicate an async jump. In `longjohn` version `0.2.12`, for example, a single line of dashes is included in a stack trace, by default, to indicate an async jump.
268+
269+
Before reporting an `Error` object using the `report` method of the `@google-cloud/error-reporting` module, the stack trace needs to modified to remove this special line added by `longjohn`. Since the `longjohn` module can be configured to have a custom line indicating an async jump, the process of removing the custom line should be handled by the user of the `longjohn` module.
270+
271+
The following code illustrates how to update an `Error`'s stack trace, to remove the default line of dashes added by `longjohn` to indicate an async jump, before reporting the error.
272+
```js
273+
const {ErrorReporting} = require('@google-cloud/error-reporting');
274+
275+
// Instantiates a client
276+
const errors = new ErrorReporting();
277+
278+
const err = new Error('Some Error');
279+
err.stack = (err.stack || '').split('\n')
280+
.filter(line => !!line.replace(/-/g, '').trim())
281+
.join('\n');
282+
errors.report(err);
283+
```

.repo-metadata.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"name": "error_reporting",
2+
"name": "error-reporting",
33
"name_pretty": "Stackdriver Error Reporting",
44
"product_documentation": "https://cloud.google.com/error-reporting",
5-
"client_documentation": "https://cloud.google.com/nodejs/docs/reference/error-reporting/latest/",
5+
"client_documentation": "https://googleapis.dev/nodejs/error-reporting/latest/",
66
"issue_tracker": "https://issuetracker.google.com/savedsearches/559780",
77
"release_level": "beta",
88
"language": "nodejs",

0 commit comments

Comments
 (0)