Skip to content

Commit 9508c56

Browse files
docs(samples): modernize samples, sample tests (#346)
1 parent 831ed9a commit 9508c56

File tree

11 files changed

+287
-262
lines changed

11 files changed

+287
-262
lines changed

error-reporting/explicitSetup.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
// sample-metadata:
17+
// title: Explicit setup
18+
// description: Reports a simple error using explicit credentials.
19+
// usage: node explicitSetup.js
20+
21+
'use strict';
22+
23+
function explicitSetup() {
24+
// [START error_reporting_setup_explicit]
25+
// [START error_reporting_setup_nodejs_explicit]
26+
// Imports the Google Cloud client library
27+
const {ErrorReporting} = require('@google-cloud/error-reporting');
28+
29+
// Instantiates a client
30+
const errors = new ErrorReporting({
31+
projectId: 'your-project-id',
32+
keyFilename: '/path/to/key.json',
33+
});
34+
35+
// Reports a simple error
36+
errors.report('Something broke!');
37+
// [END error_reporting_setup_nodejs_explicit]
38+
// [END error_reporting_setup_explicit]
39+
}
40+
41+
explicitSetup();

error-reporting/express.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
// sample-metadata:
17+
// title: Express integration
18+
// description: Starts and Express service with integrated error reporting.
19+
// usage: node express.js
20+
21+
'use strict';
22+
23+
function express() {
24+
// [START error_reporting_express]
25+
// [START error_reporting_setup_nodejs_express]
26+
const express = require('express');
27+
28+
// Imports the Google Cloud client library
29+
const {ErrorReporting} = require('@google-cloud/error-reporting');
30+
31+
// Instantiates a client
32+
const errors = new ErrorReporting();
33+
34+
const app = express();
35+
36+
app.get('/error', (req, res, next) => {
37+
res.send('Something broke!');
38+
next(new Error('Custom error message'));
39+
});
40+
41+
app.get('/exception', () => {
42+
JSON.parse('{"malformedJson": true');
43+
});
44+
45+
// Note that express error handling middleware should be attached after all
46+
// the other routes and use() calls. See the Express.js docs.
47+
app.use(errors.express);
48+
49+
const PORT = process.env.PORT || 8080;
50+
app.listen(PORT, () => {
51+
console.log(`App listening on port ${PORT}`);
52+
console.log('Press Ctrl+C to quit.');
53+
});
54+
// [END error_reporting_setup_nodejs_express]
55+
// [END error_reporting_express]
56+
}
57+
express();

error-reporting/implicitSetup.js

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+
// sample-metadata:
17+
// title: Implicit setup
18+
// description: Reports a simple error using implicit credentials.
19+
// usage: node implicitSetup.js
20+
21+
'use strict';
22+
23+
function setupImplicit() {
24+
// [START error_reporting_setup_implicit]
25+
// [START error_reporting_setup_nodejs_implicit]
26+
// Imports the Google Cloud client library
27+
const {ErrorReporting} = require('@google-cloud/error-reporting');
28+
29+
// Instantiates a client
30+
const errors = new ErrorReporting();
31+
32+
// Reports a simple error
33+
errors.report('Something broke!');
34+
// [END error_reporting_setup_nodejs_implicit]
35+
// [END error_reporting_setup_implicit]
36+
}
37+
38+
setupImplicit();

error-reporting/manual.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
// sample-metadata:
17+
// title: Manual reporting
18+
// description: Manually reports errors.
19+
// usage: node manual.js
20+
21+
'use strict';
22+
23+
function manual() {
24+
// [START error_reporting_manual]
25+
// [START error_reporting_setup_nodejs_manual]
26+
// Imports the Google Cloud client library
27+
const {ErrorReporting} = require('@google-cloud/error-reporting');
28+
29+
// Instantiates a client
30+
const errors = new ErrorReporting();
31+
32+
// Use the error message builder to customize all fields ...
33+
const errorEvent = errors.event();
34+
35+
// Add error information
36+
errorEvent.setMessage('My error message');
37+
errorEvent.setUser('root@nexus');
38+
39+
// Report the error event
40+
errors.report(errorEvent, () => {
41+
console.log('Done reporting error event!');
42+
});
43+
44+
// Report an Error object
45+
errors.report(new Error('My error message'), () => {
46+
console.log('Done reporting Error object!');
47+
});
48+
49+
// Report an error by provided just a string
50+
errors.report('My error message', () => {
51+
console.log('Done reporting error string!');
52+
});
53+
// [END error_reporting_setup_nodejs_manual]
54+
// [END error_reporting_manual]
55+
}
56+
manual();

error-reporting/package.json

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
{
2-
"name": "nodejs-docs-samples-logging",
3-
"version": "0.0.1",
2+
"name": "nodejs-docs-samples-error-reporting",
43
"private": true,
54
"license": "Apache-2.0",
65
"author": "Google Inc.",
76
"repository": "googleapis/nodejs-error-reporting",
7+
"files": [
8+
"*.js"
9+
],
810
"engines": {
9-
"node": ">=8"
11+
"node": ">=10"
1012
},
1113
"scripts": {
12-
"error-test": "samples test app --msg \"Something broke!\" --url \"http://localhost:33332/error\" --port 33332 -- snippets.js express",
13-
"exception-test": "samples test app --code 500 --msg SyntaxError --url \"http://localhost:33333/exception\" --port 33333 -- snippets.js express",
14-
"all-test": "npm run test && npm run error-test && npm run exception-test",
15-
"test": "mocha system-test/*.test.js --timeout=600000"
14+
"test": "mocha --timeout=600000"
1615
},
1716
"dependencies": {
1817
"@google-cloud/error-reporting": "^0.6.3",
19-
"express": "^4.16.3",
20-
"yargs": "^13.0.0"
18+
"express": "^4.16.3"
2119
},
2220
"devDependencies": {
23-
"@google-cloud/nodejs-repo-tools": "^3.0.0",
24-
"mocha": "^6.0.0",
25-
"proxyquire": "^2.0.1",
26-
"sinon": "^7.0.0"
21+
"chai": "^4.2.0",
22+
"gaxios": "^2.0.1",
23+
"mocha": "^6.0.0"
2724
}
2825
}

error-reporting/quickstart.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,10 @@
1515

1616
'use strict';
1717

18-
// eslint-disable-next-line no-unused-vars
1918
function quickstart() {
2019
// [START error_reporting_quickstart]
2120
// Imports the Google Cloud client library
22-
const ErrorReporting = require('@google-cloud/error-reporting')
23-
.ErrorReporting;
24-
25-
// On Node 6+ the following syntax can be used instead:
26-
// const {ErrorReporting} = require('@google-cloud/error-reporting');
27-
28-
// With ES6 style imports via TypeScript or Babel, the following
29-
// syntax can be used instead:
30-
// import {ErrorReporting} from '@google-cloud/error-reporting';
21+
const {ErrorReporting} = require('@google-cloud/error-reporting');
3122

3223
// Instantiates a client
3324
const errors = new ErrorReporting();
@@ -36,3 +27,4 @@ function quickstart() {
3627
errors.report('Something broke!');
3728
// [END error_reporting_quickstart]
3829
}
30+
quickstart();

0 commit comments

Comments
 (0)