Skip to content

Commit

Permalink
Merge pull request #108 from GoogleCloudPlatform/errorreporting
Browse files Browse the repository at this point in the history
Add App Engine error reporting example.
  • Loading branch information
jmdobry committed May 11, 2016
2 parents d4ebaa9 + 4131b07 commit 4fbaf10
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 1 deletion.
9 changes: 9 additions & 0 deletions appengine/errorreporting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Node.js error reporting sample for Google App Engine

This sample demonstrates error reporting in a Node.js app for
[Google App Engine Managed VMs](https://cloud.google.com/appengine).

## Running locally

Refer to the [appengine/README.md](../README.md) file for instructions on
running and deploying.
62 changes: 62 additions & 0 deletions appengine/errorreporting/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*jshint unused:false*/
// Copyright 2015-2016, 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.

// [START app]
'use strict';

var express = require('express');
var winston = require('winston');

var app = express();
var logFile = '/var/log/app_engine/custom_logs/myapp.errors.log.json';

winston.add(winston.transports.File, {
filename: logFile
});

function report(err, req) {
var payload = {
serviceContext: {
service: 'myapp',
},
message: err.stack,
context: {
httpRequest: {
url: req.originalUrl,
method: req.method,
referrer: req.header('Referer'),
userAgent: req.header('User-Agent'),
remoteIp: req.ip,
responseStatusCode: 500,
}
}
};
winston.error(payload);
}

app.get('/', function (req, res, next) {
next(new Error('something is wrong!'));
});

app.use(function (err, req, res, next) {
report(err, req);
res.status(500).send(err.message || 'Something broke!');
});

var server = app.listen(process.env.PORT || '8080', function () {
console.log('App listening on port %s', server.address().port);
console.log('Press Ctrl+C to quit.');
});
// [END app]
20 changes: 20 additions & 0 deletions appengine/errorreporting/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2015-2016, 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.

# [START app_yaml]
runtime: nodejs
vm: true

skip_files:
- ^(.*/)?.*/node_modules/.*$
# [END app_yaml]
20 changes: 20 additions & 0 deletions appengine/errorreporting/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "appengine-error-reporting",
"description": "Node.js error reporting sample for Google App Engine",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "~4.2"
},
"scripts": {
"start": "node app.js",
"monitor": "nodemon app.js",
"deploy": "gcloud preview app deploy"
},
"dependencies": {
"express": "^4.13.4",
"winston": "^2.2.0"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"ava": {
"files": [
"test/**/*.test.js"
"test/appengine/**/*.test.js"
]
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions test/appengine/all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ var sampleTests = [
args: ['app.js'],
msg: 'Instance:'
},
{
dir: 'appengine/errorreporting',
cmd: 'node',
args: ['app.js'],
msg: 'something is wrong',
code: 500
},
{
dir: 'appengine/express',
deploy: true,
Expand Down Expand Up @@ -291,6 +298,7 @@ function testRequest(url, sample, cb) {
// Success
return cb(null, true);
}

// Short-circuit app test
var message = sample.dir + ': failed verification!\n' +
'Expected: ' + sample.msg + '\n' +
Expand Down

0 comments on commit 4fbaf10

Please sign in to comment.