Skip to content

Commit 4131b07

Browse files
committed
Add App Engine error reporting example.
1 parent d4ebaa9 commit 4131b07

File tree

6 files changed

+120
-1
lines changed

6 files changed

+120
-1
lines changed

appengine/errorreporting/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Node.js error reporting sample for Google App Engine
2+
3+
This sample demonstrates error reporting in a Node.js app for
4+
[Google App Engine Managed VMs](https://cloud.google.com/appengine).
5+
6+
## Running locally
7+
8+
Refer to the [appengine/README.md](../README.md) file for instructions on
9+
running and deploying.

appengine/errorreporting/app.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*jshint unused:false*/
2+
// Copyright 2015-2016, Google, Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
// [START app]
17+
'use strict';
18+
19+
var express = require('express');
20+
var winston = require('winston');
21+
22+
var app = express();
23+
var logFile = '/var/log/app_engine/custom_logs/myapp.errors.log.json';
24+
25+
winston.add(winston.transports.File, {
26+
filename: logFile
27+
});
28+
29+
function report(err, req) {
30+
var payload = {
31+
serviceContext: {
32+
service: 'myapp',
33+
},
34+
message: err.stack,
35+
context: {
36+
httpRequest: {
37+
url: req.originalUrl,
38+
method: req.method,
39+
referrer: req.header('Referer'),
40+
userAgent: req.header('User-Agent'),
41+
remoteIp: req.ip,
42+
responseStatusCode: 500,
43+
}
44+
}
45+
};
46+
winston.error(payload);
47+
}
48+
49+
app.get('/', function (req, res, next) {
50+
next(new Error('something is wrong!'));
51+
});
52+
53+
app.use(function (err, req, res, next) {
54+
report(err, req);
55+
res.status(500).send(err.message || 'Something broke!');
56+
});
57+
58+
var server = app.listen(process.env.PORT || '8080', function () {
59+
console.log('App listening on port %s', server.address().port);
60+
console.log('Press Ctrl+C to quit.');
61+
});
62+
// [END app]

appengine/errorreporting/app.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2015-2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
# [START app_yaml]
15+
runtime: nodejs
16+
vm: true
17+
18+
skip_files:
19+
- ^(.*/)?.*/node_modules/.*$
20+
# [END app_yaml]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "appengine-error-reporting",
3+
"description": "Node.js error reporting sample for Google App Engine",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"author": "Google Inc.",
8+
"engines": {
9+
"node": "~4.2"
10+
},
11+
"scripts": {
12+
"start": "node app.js",
13+
"monitor": "nodemon app.js",
14+
"deploy": "gcloud preview app deploy"
15+
},
16+
"dependencies": {
17+
"express": "^4.13.4",
18+
"winston": "^2.2.0"
19+
}
20+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
},
4747
"ava": {
4848
"files": [
49-
"test/**/*.test.js"
49+
"test/appengine/**/*.test.js"
5050
]
5151
},
5252
"devDependencies": {

test/appengine/all.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ var sampleTests = [
7272
args: ['app.js'],
7373
msg: 'Instance:'
7474
},
75+
{
76+
dir: 'appengine/errorreporting',
77+
cmd: 'node',
78+
args: ['app.js'],
79+
msg: 'something is wrong',
80+
code: 500
81+
},
7582
{
7683
dir: 'appengine/express',
7784
deploy: true,
@@ -291,6 +298,7 @@ function testRequest(url, sample, cb) {
291298
// Success
292299
return cb(null, true);
293300
}
301+
294302
// Short-circuit app test
295303
var message = sample.dir + ': failed verification!\n' +
296304
'Expected: ' + sample.msg + '\n' +

0 commit comments

Comments
 (0)