Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 2 additions & 24 deletions appengine/errorreporting/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,17 @@

// [START setup]
const express = require('express');
const winston = require('winston');
const errors = require('@google-cloud/error-reporting')();

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

winston.add(winston.transports.File, {
filename: logFile
});
// [END setup]

function report (err, req) {
const payload = {
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('/', (req, res, next) => {
next(new Error('something is wrong!'));
});

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

Expand Down
8 changes: 4 additions & 4 deletions appengine/errorreporting/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
"e2e-test": "samples test deploy"
},
"dependencies": {
"express": "4.15.2",
"winston": "2.3.1"
"@google-cloud/error-reporting": "0.1.1",
"express": "4.15.2"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "1.4.14",
"ava": "0.19.1",
"proxyquire": "1.7.11",
"sinon": "2.1.0"
"proxyquire": "1.8.0",
"sinon": "2.2.0"
},
"cloud-repo-tools": {
"test": {
Expand Down
26 changes: 14 additions & 12 deletions appengine/errorreporting/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
'use strict';

const express = require(`express`);
const winston = require(`winston`);
const path = require(`path`);
const proxyquire = require(`proxyquire`).noPreserveCache();
const request = require(`supertest`);
Expand All @@ -34,21 +33,24 @@ function getSample () {
timestamp: `1234`,
userIp: `abcd`
}) + `\n`;
const winstonMock = {
add: sinon.stub(),
error: sinon.stub()
};
const reportMock = sinon.stub();
const errorsMock = sinon.stub().callsFake(function ErrorReporting () {
return {
report: reportMock
};
});

const app = proxyquire(SAMPLE_PATH, {
winston: winstonMock,
express: expressMock
express: expressMock,
'@google-cloud/error-reporting': errorsMock
});
return {
app: app,
mocks: {
errors: errorsMock,
express: expressMock,
results: resultsMock,
winston: winstonMock
report: reportMock,
results: resultsMock
}
};
}
Expand All @@ -60,8 +62,8 @@ test(`sets up the sample`, (t) => {
const sample = getSample();

t.true(sample.mocks.express.calledOnce);
t.true(sample.mocks.winston.add.calledOnce);
t.true(sample.mocks.winston.add.firstCall.args[0] === winston.transports.File);
t.true(sample.mocks.errors.calledOnce);
t.is(sample.mocks.report.callCount, 0);
t.true(sample.app.listen.calledOnce);
t.is(sample.app.listen.firstCall.args[0], process.env.PORT || 8080);
});
Expand All @@ -74,7 +76,7 @@ test.cb(`should throw an error`, (t) => {
.get(`/`)
.expect(500)
.expect((response) => {
t.true(sample.mocks.winston.error.calledOnce);
t.true(sample.mocks.report.calledOnce);
t.is(response.text, expectedResult);
})
.end(t.end);
Expand Down
Loading