-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): modernize samples, sample tests (#346)
- Loading branch information
1 parent
eabcf26
commit fe0aaf1
Showing
11 changed files
with
287 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.