|
| 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 | +} |
0 commit comments