diff --git a/error-reporting/explicitSetup.js b/error-reporting/explicitSetup.js new file mode 100644 index 0000000000..cb81308272 --- /dev/null +++ b/error-reporting/explicitSetup.js @@ -0,0 +1,41 @@ +/** + * Copyright 2017, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// sample-metadata: +// title: Explicit setup +// description: Reports a simple error using explicit credentials. +// usage: node explicitSetup.js + +'use strict'; + +function explicitSetup() { + // [START error_reporting_setup_explicit] + // [START error_reporting_setup_nodejs_explicit] + // Imports the Google Cloud client library + const {ErrorReporting} = require('@google-cloud/error-reporting'); + + // Instantiates a client + const errors = new ErrorReporting({ + projectId: 'your-project-id', + keyFilename: '/path/to/key.json', + }); + + // Reports a simple error + errors.report('Something broke!'); + // [END error_reporting_setup_nodejs_explicit] + // [END error_reporting_setup_explicit] +} + +explicitSetup(); diff --git a/error-reporting/express.js b/error-reporting/express.js new file mode 100644 index 0000000000..42cf66d09e --- /dev/null +++ b/error-reporting/express.js @@ -0,0 +1,57 @@ +/** + * Copyright 2017, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// sample-metadata: +// title: Express integration +// description: Starts and Express service with integrated error reporting. +// usage: node express.js + +'use strict'; + +function express() { + // [START error_reporting_express] + // [START error_reporting_setup_nodejs_express] + const express = require('express'); + + // Imports the Google Cloud client library + const {ErrorReporting} = require('@google-cloud/error-reporting'); + + // Instantiates a client + const errors = new ErrorReporting(); + + const app = express(); + + app.get('/error', (req, res, next) => { + res.send('Something broke!'); + next(new Error('Custom error message')); + }); + + app.get('/exception', () => { + JSON.parse('{"malformedJson": true'); + }); + + // Note that express error handling middleware should be attached after all + // the other routes and use() calls. See the Express.js docs. + app.use(errors.express); + + const PORT = process.env.PORT || 8080; + app.listen(PORT, () => { + console.log(`App listening on port ${PORT}`); + console.log('Press Ctrl+C to quit.'); + }); + // [END error_reporting_setup_nodejs_express] + // [END error_reporting_express] +} +express(); diff --git a/error-reporting/implicitSetup.js b/error-reporting/implicitSetup.js new file mode 100644 index 0000000000..75573722a1 --- /dev/null +++ b/error-reporting/implicitSetup.js @@ -0,0 +1,38 @@ +/** + * Copyright 2017, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// sample-metadata: +// title: Implicit setup +// description: Reports a simple error using implicit credentials. +// usage: node implicitSetup.js + +'use strict'; + +function setupImplicit() { + // [START error_reporting_setup_implicit] + // [START error_reporting_setup_nodejs_implicit] + // Imports the Google Cloud client library + const {ErrorReporting} = require('@google-cloud/error-reporting'); + + // Instantiates a client + const errors = new ErrorReporting(); + + // Reports a simple error + errors.report('Something broke!'); + // [END error_reporting_setup_nodejs_implicit] + // [END error_reporting_setup_implicit] +} + +setupImplicit(); diff --git a/error-reporting/manual.js b/error-reporting/manual.js new file mode 100644 index 0000000000..12008365d2 --- /dev/null +++ b/error-reporting/manual.js @@ -0,0 +1,56 @@ +/** + * Copyright 2017, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// sample-metadata: +// title: Manual reporting +// description: Manually reports errors. +// usage: node manual.js + +'use strict'; + +function manual() { + // [START error_reporting_manual] + // [START error_reporting_setup_nodejs_manual] + // Imports the Google Cloud client library + const {ErrorReporting} = require('@google-cloud/error-reporting'); + + // Instantiates a client + const errors = new ErrorReporting(); + + // Use the error message builder to customize all fields ... + const errorEvent = errors.event(); + + // Add error information + errorEvent.setMessage('My error message'); + errorEvent.setUser('root@nexus'); + + // Report the error event + errors.report(errorEvent, () => { + console.log('Done reporting error event!'); + }); + + // Report an Error object + errors.report(new Error('My error message'), () => { + console.log('Done reporting Error object!'); + }); + + // Report an error by provided just a string + errors.report('My error message', () => { + console.log('Done reporting error string!'); + }); + // [END error_reporting_setup_nodejs_manual] + // [END error_reporting_manual] +} +manual(); diff --git a/error-reporting/package.json b/error-reporting/package.json index a4e0a50a84..1e1960e5cf 100644 --- a/error-reporting/package.json +++ b/error-reporting/package.json @@ -1,28 +1,25 @@ { - "name": "nodejs-docs-samples-logging", - "version": "0.0.1", + "name": "nodejs-docs-samples-error-reporting", "private": true, "license": "Apache-2.0", "author": "Google Inc.", "repository": "googleapis/nodejs-error-reporting", + "files": [ + "*.js" + ], "engines": { - "node": ">=8" + "node": ">=10" }, "scripts": { - "error-test": "samples test app --msg \"Something broke!\" --url \"http://localhost:33332/error\" --port 33332 -- snippets.js express", - "exception-test": "samples test app --code 500 --msg SyntaxError --url \"http://localhost:33333/exception\" --port 33333 -- snippets.js express", - "all-test": "npm run test && npm run error-test && npm run exception-test", - "test": "mocha system-test/*.test.js --timeout=600000" + "test": "mocha --timeout=600000" }, "dependencies": { "@google-cloud/error-reporting": "^0.6.3", - "express": "^4.16.3", - "yargs": "^13.0.0" + "express": "^4.16.3" }, "devDependencies": { - "@google-cloud/nodejs-repo-tools": "^3.0.0", - "mocha": "^6.0.0", - "proxyquire": "^2.0.1", - "sinon": "^7.0.0" + "chai": "^4.2.0", + "gaxios": "^2.0.1", + "mocha": "^6.0.0" } } diff --git a/error-reporting/quickstart.js b/error-reporting/quickstart.js index c1cc209904..0e13983bba 100644 --- a/error-reporting/quickstart.js +++ b/error-reporting/quickstart.js @@ -15,19 +15,10 @@ 'use strict'; -// eslint-disable-next-line no-unused-vars function quickstart() { // [START error_reporting_quickstart] // Imports the Google Cloud client library - const ErrorReporting = require('@google-cloud/error-reporting') - .ErrorReporting; - - // On Node 6+ the following syntax can be used instead: - // const {ErrorReporting} = require('@google-cloud/error-reporting'); - - // With ES6 style imports via TypeScript or Babel, the following - // syntax can be used instead: - // import {ErrorReporting} from '@google-cloud/error-reporting'; + const {ErrorReporting} = require('@google-cloud/error-reporting'); // Instantiates a client const errors = new ErrorReporting(); @@ -36,3 +27,4 @@ function quickstart() { errors.report('Something broke!'); // [END error_reporting_quickstart] } +quickstart(); diff --git a/error-reporting/snippets.js b/error-reporting/snippets.js deleted file mode 100644 index ac72c7e756..0000000000 --- a/error-reporting/snippets.js +++ /dev/null @@ -1,197 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -function setupImplicit() { - // [START error_reporting_setup_implicit] - // [START error_reporting_setup_nodejs_implicit] - // Imports the Google Cloud client library - const ErrorReporting = require('@google-cloud/error-reporting') - .ErrorReporting; - - // On Node 6+ the following syntax can be used instead: - // const {ErrorReporting} = require('@google-cloud/error-reporting'); - - // With ES6 style imports via TypeScript or Babel, the following - // syntax can be used instead: - // import {ErrorReporting} from '@google-cloud/error-reporting'; - - // Instantiates a client - const errors = new ErrorReporting(); - - // Reports a simple error - errors.report('Something broke!'); - // [END error_reporting_setup_nodejs_implicit] - // [END error_reporting_setup_implicit] -} - -function setupExplicit() { - // [START error_reporting_setup_explicit] - // [START error_reporting_setup_nodejs_explicit] - // Imports the Google Cloud client library - const ErrorReporting = require('@google-cloud/error-reporting') - .ErrorReporting; - - // On Node 6+ the following syntax can be used instead: - // const {ErrorReporting} = require('@google-cloud/error-reporting'); - - // With ES6 style imports via TypeScript or Babel, the following - // syntax can be used instead: - // import {ErrorReporting} from '@google-cloud/error-reporting'; - - // Instantiates a client - const errors = new ErrorReporting({ - projectId: 'your-project-id', - keyFilename: '/path/to/key.json', - }); - - // Reports a simple error - errors.report('Something broke!'); - // [END error_reporting_setup_nodejs_explicit] - // [END error_reporting_setup_explicit] -} - -function manual() { - // [START error_reporting_manual] - // [START error_reporting_setup_nodejs_manual] - // Imports the Google Cloud client library - const ErrorReporting = require('@google-cloud/error-reporting') - .ErrorReporting; - - // On Node 6+ the following syntax can be used instead: - // const {ErrorReporting} = require('@google-cloud/error-reporting'); - - // With ES6 style imports via TypeScript or Babel, the following - // syntax can be used instead: - // import {ErrorReporting} from '@google-cloud/error-reporting'; - - // Instantiates a client - const errors = new ErrorReporting(); - - // Use the error message builder to customize all fields ... - const errorEvent = errors.event(); - - // Add error information - errorEvent.setMessage('My error message'); - errorEvent.setUser('root@nexus'); - - // Report the error event - errors.report(errorEvent, () => { - console.log('Done reporting error event!'); - }); - - // Report an Error object - errors.report(new Error('My error message'), () => { - console.log('Done reporting Error object!'); - }); - - // Report an error by provided just a string - errors.report('My error message', () => { - console.log('Done reporting error string!'); - }); - // [END error_reporting_setup_nodejs_manual] - // [END error_reporting_manual] -} - -function express() { - // [START error_reporting_express] - // [START error_reporting_setup_nodejs_express] - const express = require('express'); - - // Imports the Google Cloud client library - const ErrorReporting = require('@google-cloud/error-reporting') - .ErrorReporting; - - // On Node 6+ the following syntax can be used instead: - // const {ErrorReporting} = require('@google-cloud/error-reporting'); - - // With ES6 style imports via TypeScript or Babel, the following - // syntax can be used instead: - // import {ErrorReporting} from '@google-cloud/error-reporting'; - - // Instantiates a client - const errors = new ErrorReporting(); - - const app = express(); - - app.get('/error', (req, res, next) => { - res.send('Something broke!'); - next(new Error('Custom error message')); - }); - - app.get('/exception', () => { - JSON.parse('{"malformedJson": true'); - }); - - // Note that express error handling middleware should be attached after all - // the other routes and use() calls. See the Express.js docs. - app.use(errors.express); - - const PORT = process.env.PORT || 8080; - app.listen(PORT, () => { - console.log(`App listening on port ${PORT}`); - console.log('Press Ctrl+C to quit.'); - }); - // [END error_reporting_setup_nodejs_express] - // [END error_reporting_express] -} - -// The command-line program -const cli = require(`yargs`) - .demand(1) - .command( - 'setup-implicit', - 'Reports a simple error using implicit credentials.', - {}, - setupImplicit - ) - .command( - 'setup-explicit', - 'Reports a simple error using explicit credentials.', - {}, - setupExplicit - ) - .command('manual', 'Manually reports errors.', {}, manual) - .command( - 'express', - 'Starts and Express service with integrated error reporting.', - {}, - express - ) - .example( - 'node $0 setup-implicit', - 'Reports a simple error using implicit credentials.' - ) - .example( - 'node $0 setup-explicit', - 'Reports a simple error using explicit credentials.' - ) - .example('node $0 manual', 'Manually report some errors.') - .example( - 'node $0 express', - 'Starts and Express service with integrated error reporting.' - ) - .wrap(120) - .recommendCommands() - .epilogue( - `For more information, see https://cloud.google.com/error-reporting/docs` - ) - .help() - .strict(); - -if (module === require.main) { - cli.parse(process.argv.slice(2)); -} diff --git a/error-reporting/system-test/.eslintrc.yml b/error-reporting/system-test/.eslintrc.yml deleted file mode 100644 index 0ab526f52b..0000000000 --- a/error-reporting/system-test/.eslintrc.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- -env: - mocha: true -rules: - node/no-unpublished-require: off - no-empty: off diff --git a/error-reporting/system-test/snippets.test.js b/error-reporting/system-test/snippets.test.js deleted file mode 100644 index f5f155e350..0000000000 --- a/error-reporting/system-test/snippets.test.js +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright 2017, Google, Inc. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; - -const path = require('path'); -const assert = require('assert'); -const tools = require('@google-cloud/nodejs-repo-tools'); - -const cwd = path.join(__dirname, '..'); -const cmd = 'node snippets.js'; - -before(tools.checkCredentials); - -it('should setup using implicit credentials', async () => - // There's no output, the command should just succeed - await tools.runAsync(`${cmd} setup-implicit`, cwd)); - -it('should report errors manually', async () => { - const output = await tools.runAsync(`${cmd} manual`, cwd); - assert.strictEqual(output.includes('Done reporting error event!'), true); - assert.strictEqual(output.includes('Done reporting Error object!'), true); - assert.strictEqual(output.includes('Done reporting error string!'), true); -}); diff --git a/error-reporting/test/.eslintrc.yml b/error-reporting/test/.eslintrc.yml new file mode 100644 index 0000000000..6db2a46c53 --- /dev/null +++ b/error-reporting/test/.eslintrc.yml @@ -0,0 +1,3 @@ +--- +env: + mocha: true diff --git a/error-reporting/test/snippets.test.js b/error-reporting/test/snippets.test.js new file mode 100644 index 0000000000..6f8db37bf3 --- /dev/null +++ b/error-reporting/test/snippets.test.js @@ -0,0 +1,80 @@ +/** + * Copyright 2017, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const {assert} = require('chai'); +const {execSync, spawn} = require('child_process'); +const {request} = require('gaxios'); + +const exec = cmd => execSync(cmd, {encoding: 'utf8'}); + +/** + * Start the express server, and wait for it to start serving content before + * resolving the promise. + */ +async function startExpress() { + return new Promise((resolve, reject) => { + const p = spawn('node', ['express']); + p.on('error', reject); + p.stdout.on('data', data => { + if (data.includes('Press Ctrl+C to quit.')) { + resolve(p); + } + }); + }); +} + +describe(__filename, () => { + it('should setup using implicit credentials', () => { + // There's no output, the command should just succeed + exec('node implicitSetup'); + }); + + it('should report errors manually', () => { + const output = exec('node manual'); + assert.include(output, 'Done reporting error event!'); + assert.include(output, 'Done reporting Error object!'); + assert.include(output, 'Done reporting error string!'); + }); + + it('should report errors with the express test', async () => { + let pid; + try { + pid = await startExpress(); + const res = await request({ + url: 'http://localhost:8080/error', + }); + assert.include(res.data, 'Something broke!'); + } finally { + if (pid) pid.kill(); + } + }); + + it('should report exceptions from the express test', async () => { + let pid; + try { + pid = await startExpress(); + const res = await request({ + url: 'http://localhost:8080/exception', + validateStatus: () => true, + }); + assert.strictEqual(res.status, 500); + assert.include(res.data, 'SyntaxError'); + } finally { + if (pid) pid.kill(); + } + }); +});