Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

GoogleCloudPlatform/cloud-errors-nodejs

 
 

Repository files navigation

Node.js module for Stackdriver Error Reporting

Build Status Coverage Status Dependencies

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:

Learn about Error Reporting in Stackdriver

Prerequisites

  1. Your application needs to use Node.js version 0.12 or greater.
  2. You need a Google Cloud project. Your application can run anywhere, but errors are reported to a particular project.
  3. Enable the Stackdriver Error Reporting API for your project.
  4. The module will only send errors when the NODE_ENV environment variable is set to production.

Quickstart on Google Cloud Platform

  1. 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
```
  1. 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!');
  2. View reported errors:

Open Stackdriver Error Reporting at https://console.cloud.google.com/errors to view the reported errors.

Setup

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_CREDENTIALS environment variable,
    • a path to your keyfile in the keyFilename argument,
    • an API key string in the key argument.
  • projectId: either using the GLCOUD_PROJECT environment variable or the projectId argument.
  • service: either using the GAE_MODULE_NAME environment variable or the serviceContext.service argument.

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'
	}
});

Using Express

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);

Using Hapi

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 });

Using Koa

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);

Using Restify

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);

Developing the library

Install the dependencies:

npm install

Add your unit tests to:

tests/unit/

Run the test suite:

npm test

Run the coverage suite (will also run the test suite):

npm run-script coverage

Run the style checking suite:

npm run-script style

Pre-commit, run the Pre-commit hook to run Clang Formatter (Must have Clang Formatter installed prior to use)

git commit

Then commit your changes and make a pull-request

About

Node.js module for Google Stackdriver Error Reporting

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors