Skip to content

Commit 48733aa

Browse files
authored
Error reporting (GoogleCloudPlatform#408)
1 parent 5c6353e commit 48733aa

File tree

5 files changed

+4016
-0
lines changed

5 files changed

+4016
-0
lines changed

error-reporting/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Stackdriver Error Reporting Node.js Samples
4+
5+
[![Build](https://storage.googleapis.com/cloud-docs-samples-badges/GoogleCloudPlatform/nodejs-docs-samples/nodejs-docs-samples-logging.svg)]()
6+
7+
[Stackdriver Error Reporting](https://cloud.google.com/error-reporting/docs/) aggregates and displays errors produced in your running cloud services.
8+
9+
## Table of Contents
10+
11+
* [Setup](#setup)
12+
* [Samples](#samples)
13+
* [Examples](#examples)
14+
* [Running the tests](#running-the-tests)
15+
16+
## Setup
17+
18+
1. Read [Prerequisites][prereq] and [How to run a sample][run] first.
19+
1. Install dependencies:
20+
21+
With **npm**:
22+
23+
npm install
24+
25+
With **yarn**:
26+
27+
yarn install
28+
29+
[prereq]: ../README.md#prerequisites
30+
[run]: ../README.md#how-to-run-a-sample
31+
32+
## Samples
33+
34+
### Examples
35+
36+
View the [documentation][snippets_0_docs] or the [source code][snippets_0_code].
37+
38+
__Usage:__ `node snippets.js --help`
39+
40+
```
41+
Commands:
42+
manual Manually reports errors.
43+
44+
Options:
45+
--help Show help [boolean]
46+
47+
Examples:
48+
node snippets.js manual Manually report some errors.
49+
50+
For more information, see https://cloud.google.com/error-reporting/docs
51+
```
52+
53+
[snippets_0_docs]: https://cloud.google.com/error-reporting/docs
54+
[snippets_0_code]: snippets.js
55+
56+
## Running the tests
57+
58+
1. Set the **GCLOUD_PROJECT** and **GOOGLE_APPLICATION_CREDENTIALS** environment variables.
59+
60+
1. Run the tests:
61+
62+
With **npm**:
63+
64+
npm test
65+
66+
With **yarn**:
67+
68+
yarn test

error-reporting/package.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "nodejs-docs-samples-logging",
3+
"version": "0.0.1",
4+
"private": true,
5+
"license": "Apache-2.0",
6+
"author": "Google Inc.",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
10+
},
11+
"engines": {
12+
"node": ">=4.3.2"
13+
},
14+
"scripts": {
15+
"lint": "samples lint",
16+
"pretest": "npm run lint",
17+
"error-test": "samples test app --msg \"Something broke!\" --url \"http://localhost:33332/error\" --port 33332 -- snippets.js express",
18+
"exception-test": "samples test app --code 500 --msg SyntaxError --url \"http://localhost:33333/exception\" --port 33333 -- snippets.js express",
19+
"system-test": "ava -T 1m --verbose system-test/*.test.js && npm run error-test && npm run exception-test",
20+
"test": "npm run system-test"
21+
},
22+
"dependencies": {
23+
"@google-cloud/error-reporting": "0.1.3",
24+
"express": "4.15.3",
25+
"yargs": "8.0.2"
26+
},
27+
"devDependencies": {
28+
"@google-cloud/nodejs-repo-tools": "1.4.15",
29+
"ava": "0.19.1",
30+
"proxyquire": "1.8.0",
31+
"sinon": "2.3.5"
32+
},
33+
"cloud-repo-tools": {
34+
"requiresKeyFile": true,
35+
"requiresProjectId": true,
36+
"product": "error_reporting",
37+
"samples": [
38+
{
39+
"id": "snippets",
40+
"name": "Examples",
41+
"file": "snippets.js",
42+
"docs_link": "https://cloud.google.com/error-reporting/docs",
43+
"usage": "node snippets.js --help"
44+
}
45+
]
46+
}
47+
}

error-reporting/snippets.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
function setupImplicit () {
19+
// [START error_reporting_setup_implicit]
20+
// Imports the Google Cloud client library
21+
const ErrorReporting = require('@google-cloud/error-reporting');
22+
23+
// Instantiates a client
24+
const errors = ErrorReporting();
25+
26+
// Reports a simple error
27+
errors.report('Something broke!');
28+
// [END error_reporting_setup_implicit]
29+
}
30+
31+
function setupExplicit () {
32+
// [START error_reporting_setup_explicit]
33+
// Imports the Google Cloud client library
34+
const ErrorReporting = require('@google-cloud/error-reporting');
35+
36+
// Instantiates a client
37+
const errors = ErrorReporting({
38+
projectId: 'your-project-id',
39+
keyFilename: '/path/to/key.json'
40+
});
41+
42+
// Reports a simple error
43+
errors.report('Something broke!');
44+
// [END error_reporting_setup_explicit]
45+
}
46+
47+
function manual () {
48+
// [START error_reporting_manual]
49+
// Imports the Google Cloud client library
50+
const ErrorReporting = require('@google-cloud/error-reporting');
51+
52+
// Instantiates a client
53+
const errors = ErrorReporting();
54+
55+
// Use the error message builder to customize all fields ...
56+
const errorEvent = errors.event();
57+
58+
// Add error information
59+
errorEvent.setMessage('My error message');
60+
errorEvent.setUser('root@nexus');
61+
62+
// Report the error event
63+
errors.report(errorEvent, () => {
64+
console.log('Done reporting error event!');
65+
});
66+
67+
// Report an Error object
68+
errors.report(new Error('My error message'), () => {
69+
console.log('Done reporting Error object!');
70+
});
71+
72+
// Report an error by provided just a string
73+
errors.report('My error message', () => {
74+
console.log('Done reporting error string!');
75+
});
76+
// [END error_reporting_manual]
77+
}
78+
79+
function express () {
80+
// [START error_reporting_express]
81+
const express = require('express');
82+
83+
// Imports the Google Cloud client library
84+
const ErrorReporting = require('@google-cloud/error-reporting');
85+
86+
// Instantiates a client
87+
const errors = ErrorReporting();
88+
89+
const app = express();
90+
91+
app.get('/error', (req, res, next) => {
92+
res.send('Something broke!');
93+
next(new Error('Custom error message'));
94+
});
95+
96+
app.get('/exception', () => {
97+
JSON.parse('{"malformedJson": true');
98+
});
99+
100+
// Note that express error handling middleware should be attached after all
101+
// the other routes and use() calls. See the Express.js docs.
102+
app.use(errors.express);
103+
104+
const PORT = process.env.PORT || 8080;
105+
app.listen(PORT, () => {
106+
console.log(`App listening on port ${PORT}`);
107+
console.log('Press Ctrl+C to quit.');
108+
});
109+
// [END error_reporting_express]
110+
}
111+
112+
// The command-line program
113+
const cli = require(`yargs`)
114+
.demand(1)
115+
.command('setup-implicit', 'Reports a simple error using implicit credentials.', {}, setupImplicit)
116+
.command('setup-explicit', 'Reports a simple error using explicit credentials.', {}, setupExplicit)
117+
.command('manual', 'Manually reports errors.', {}, manual)
118+
.command('express', 'Starts and Express service with integrated error reporting.', {}, express)
119+
.example('node $0 setup-implicit', 'Reports a simple error using implicit credentials.')
120+
.example('node $0 setup-explicit', 'Reports a simple error using explicit credentials.')
121+
.example('node $0 manual', 'Manually report some errors.')
122+
.example('node $0 express', 'Starts and Express service with integrated error reporting.')
123+
.wrap(120)
124+
.recommendCommands()
125+
.epilogue(`For more information, see https://cloud.google.com/error-reporting/docs`)
126+
.help()
127+
.strict();
128+
129+
if (module === require.main) {
130+
cli.parse(process.argv.slice(2));
131+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
const path = require(`path`);
19+
const test = require(`ava`);
20+
const tools = require(`@google-cloud/nodejs-repo-tools`);
21+
22+
const cwd = path.join(__dirname, `..`);
23+
const cmd = `node snippets.js`;
24+
25+
test.before(tools.checkCredentials);
26+
27+
test.serial(`should setup using implicit credentials`, async (t) => {
28+
t.plan(0);
29+
// There's no output, the command should just succeed
30+
await tools.runAsync(`${cmd} setup-implicit`, cwd);
31+
});
32+
33+
test.serial(`should report errors manually`, async (t) => {
34+
const output = await tools.runAsync(`${cmd} manual`, cwd);
35+
t.is(output.includes('Done reporting error event!'), true);
36+
t.is(output.includes('Done reporting Error object!'), true);
37+
t.is(output.includes('Done reporting error string!'), true);
38+
});

0 commit comments

Comments
 (0)