Skip to content

Commit

Permalink
docs(samples): modernize samples, sample tests (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored and Ace Nassri committed Nov 11, 2022
1 parent eabcf26 commit fe0aaf1
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 262 deletions.
41 changes: 41 additions & 0 deletions error-reporting/explicitSetup.js
Original file line number Diff line number Diff line change
@@ -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();
57 changes: 57 additions & 0 deletions error-reporting/express.js
Original file line number Diff line number Diff line change
@@ -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();
38 changes: 38 additions & 0 deletions error-reporting/implicitSetup.js
Original file line number Diff line number Diff line change
@@ -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();
56 changes: 56 additions & 0 deletions error-reporting/manual.js
Original file line number Diff line number Diff line change
@@ -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();
23 changes: 10 additions & 13 deletions error-reporting/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
12 changes: 2 additions & 10 deletions error-reporting/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -36,3 +27,4 @@ function quickstart() {
errors.report('Something broke!');
// [END error_reporting_quickstart]
}
quickstart();
Loading

0 comments on commit fe0aaf1

Please sign in to comment.