This is not an official Google product. This module is experimental and may not be ready for use. This module uses APIs that may be undocumented and are subject to change without notice.
This modules provides Stackdriver Error Reporting support for Node.js applications. Stackdriver Error Reporting is a feature of Google Cloud Platform that allows in-depth monitoring and viewing of errors reported by applications running in almost any environment. Here's an introductory video:
- Your application needs to use Node.js version 0.12 or greater.
- You need a Google Cloud project. Your application can run anywhere, but errors are reported to a particular project.
- Enable the Stackdriver Error Reporting API for your project.
- The module will only send errors when the
NODE_ENVenvironment variable is set toproduction.
- Install the module:
In your project, on the command line:
```shell
# Install through npm while saving to the local 'package.json'
npm install --save @google/cloud-errors
```
-
Instrument your application:
// Require the library and initialize the error handler var errors = require('@google/cloud-errors')({ serviceContext: {service: 'my-service'} // not needed on Google App Engine }); // Report an error to the Stackdriver Error Reporting API errors.report('Something broke!');
-
View reported errors:
Open Stackdriver Error Reporting at https://console.cloud.google.com/errors to view the reported errors.
When initing the Stackdriver Error Reporting library you must specify the following:
- Authentication: Use one of the following:
- (recommended) a path to your keyfile in the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable, - a path to your keyfile in the
keyFilenameargument, - an API key string in the
keyargument.
- (recommended) a path to your keyfile in the
- projectId: either using the
GLCOUD_PROJECTenvironment variable or theprojectIdargument. - service: either using the
GAE_MODULE_NAMEenvironment variable or theserviceContext.serviceargument.
On Google App Engine, these environment variables are already set.
var errors = require('@google/cloud-errors')({
projectId: 'my-project-id',
key: 'my-api-key',
keyFilename: 'path-to-my-keyfile'
onUncaughtException: 'report', // or 'ignore' or 'reportAndExit'
serviceContext: {
service: 'my-service',
version: 'my-service-version'
}
});var express = require('express');
var app = express();
var errors = require('@google/cloud-errors')();
app.get('/error', function ( req, res, next ) {
res.send('Something broke!');
next(new Error('Custom error message'));
});
app.get('/exception', function () {
JSON.parse('{"malformedJson": true'); // Missing }. This will throw an Error.
});
app.use(errors.express);
app.listen(3000);var hapi = require('hapi');
var errors = require('@google/cloud-errors')();
var server = new hapi.Server();
server.connection({ port: 3000 });
server.start();
server.route({
method: 'GET',
path: '/error',
handler: function ( request, reply ) {
throw new Error('Custom error message');
reply('Something broke!');
}
});
server.register({ register: errors.hapi });var errors = require('@google/cloud-errors')();
var koa = require('koa');
var app = koa();
app.use(errors.koa);
app.use(function *(next) {
//This will set status and message
this.throw('Error Message', 500);
});
// response
app.use(function *(){
this.body = 'Hello World';
});
app.listen(3000);function respond(req, res, next) {
next(new Error('this is a restify error'));
}
var restify = require('restify');
var errors = require('@google/cloud-errors')();
var server = restify.createServer();
server.use(errors.restify(server));
server.get('/hello/:name', respond);
server.head('/hello/:name', respond);
server.listen(8080);Install the dependencies:
npm installAdd your unit tests to:
tests/unit/
Run the test suite:
npm testRun the coverage suite (will also run the test suite):
npm run-script coverageRun the style checking suite:
npm run-script stylePre-commit, run the Pre-commit hook to run Clang Formatter (Must have Clang Formatter installed prior to use)
git commitThen commit your changes and make a pull-request
