| 20 | 'use strict'; |
| 21 | |
| 22 | function express() { |
| 23 | // [START error_reporting_setup_nodejs_express] |
| 24 | const express = require('express'); |
| 25 | |
| 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 | const app = express(); |
| 33 | |
| 34 | app.get('/error', (req, res, next) => { |
| 35 | res.send('Something broke!'); |
| 36 | next(new Error('Custom error message')); |
| 37 | }); |
| 38 | |
| 39 | app.get('/exception', () => { |
| 40 | JSON.parse('{"malformedJson": true'); |
| 41 | }); |
| 42 | |
| 43 | // Note that express error handling middleware should be attached after all |
| 44 | // the other routes and use() calls. See the Express.js docs. |
| 45 | app.use(errors.express); |
| 46 | |
| 47 | const PORT = process.env.PORT || 8080; |
| 48 | app.listen(PORT, () => { |
| 49 | console.log(`App listening on port ${PORT}`); |
| 50 | console.log('Press Ctrl+C to quit.'); |
| 51 | }); |
| 52 | // [END error_reporting_setup_nodejs_express] |
| 53 | } |
| 54 | express(); |