Skip to content
Open
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
15 changes: 14 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const defaults = {
includeEventTags: false,
requestPayload: false,
requestHeaders: false,
tags: ['detailed-response']
tags: ['detailed-response'],
logAborts: true
};

const contains = (arr1, arr2) => arr1.some(item => arr2.includes(item));
Expand Down Expand Up @@ -180,6 +181,18 @@ const register = (server, options) => {
}
});

if (options.logAborts) {
server.events.on({ name: 'request' }, (request, h) => {
request.events.once('disconnect', () => {
const tags = [].concat(options.tags);
tags.push('client-disconnect');
const data = getLogData(request, 499);
data.message = `${request.route.path}: ${data.message}`;
server.log(tags, data);
});
});
}

// options.request will register a handler that logs a response for every route:
if (options.requests) {
server.events.on({ name: 'response' }, (request) => {
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
"url": "https://github.com/firstandthird/hapi-log-response.git"
},
"devDependencies": {
"boom": "4.3.0",
"eslint": "^4.15.0",
"boom": "7.3.0",
"eslint": "^5.13.0",
"eslint-config-firstandthird": "^4.3.0",
"eslint-plugin-import": "^2.8.0",
"handlebars": "^4.0.11",
"hapi": "^17.0.1",
"hapi-logr": "2.2.0",
"logr-console-color": "1.2.1",
"hapi": "^18.1.0",
"hapi-logr": "6.1.1",
"logr-console-color": "1.4.0",
"tap": "^12.0.1",
"vision": "^5.2.0",
"wreck": "^14.0.2"
Expand Down
31 changes: 31 additions & 0 deletions test/test.response.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,37 @@ test('does not interfere with routes', async (t) => {
t.end();
});

test('Handles 499', async (t) => {
const server = new Hapi.Server({
debug: {
//request: ['error']
},
port: 8080
});
await server.register([
{ plugin: require('../'),
options: {
}
}
]);
server.route([
{
method: 'GET',
path: '/hello',
handler(request, h) {
throw new boom('client disconnect', { // eslint-disable-line new-cap
statusCode: 499
});
}
},
]);
await server.start();
const response = await server.inject({ url: '/hello' });
t.equal(response.statusCode, 499);
await server.stop();
t.end();
});

test('does not log when client closes the connection', async (t) => {
const server = new Hapi.Server({
debug: {
Expand Down