Skip to content

Commit

Permalink
server: refactor log_interceptor to be more DRY (#9617)
Browse files Browse the repository at this point in the history
This is purely an internal refactor that should have no effect on
consumers of the LogInterceptor class.
  • Loading branch information
epixa committed Dec 22, 2016
1 parent 116530a commit aaab285
Showing 1 changed file with 21 additions and 36 deletions.
57 changes: 21 additions & 36 deletions src/server/logging/log_interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ function doTagsMatch(event, tags) {
return isEqual(get(event, 'tags'), tags);
}

// converts the given event into a debug log if it's an error of the given type
function downgradeIfErrorMatches(errorType, event) {
const isClientError = doTagsMatch(event, ['connection', 'client', 'error']);
const matchesErrorType = isClientError && get(event, 'data.errno') === errorType;

if (!matchesErrorType) return null;

const errorTypeTag = errorType.toLowerCase();

return {
event: 'log',
pid: event.pid,
timestamp: event.timestamp,
tags: ['debug', 'connection', errorTypeTag],
data: `${errorType}: Socket was closed by the client (probably the browser) before it could be read completely`
};
}

export class LogInterceptor extends Stream.Transform {
constructor() {
super({
Expand All @@ -24,18 +42,7 @@ export class LogInterceptor extends Stream.Transform {
* @param {object} - log event
*/
downgradeIfEconnreset(event) {
const isClientError = doTagsMatch(event, ['connection', 'client', 'error']);
const isEconnreset = isClientError && get(event, 'data.errno') === 'ECONNRESET';

if (!isEconnreset) return null;

return {
event: 'log',
pid: event.pid,
timestamp: event.timestamp,
tags: ['debug', 'connection', 'econnreset'],
data: 'ECONNRESET: Socket was closed by the client (probably the browser) before it could be read completely'
};
return downgradeIfErrorMatches('ECONNRESET', event);
}

/**
Expand All @@ -49,18 +56,7 @@ export class LogInterceptor extends Stream.Transform {
* @param {object} - log event
*/
downgradeIfEpipe(event) {
const isClientError = doTagsMatch(event, ['connection', 'client', 'error']);
const isEpipe = isClientError && get(event, 'data.errno') === 'EPIPE';

if (!isEpipe) return null;

return {
event: 'log',
pid: event.pid,
timestamp: event.timestamp,
tags: ['debug', 'connection', 'epipe'],
data: 'EPIPE: Socket was closed by the client (probably the browser) before the response could be completed'
};
return downgradeIfErrorMatches('EPIPE', event);
}

/**
Expand All @@ -74,18 +70,7 @@ export class LogInterceptor extends Stream.Transform {
* @param {object} - log event
*/
downgradeIfEcanceled(event) {
const isClientError = doTagsMatch(event, ['connection', 'client', 'error']);
const isEcanceled = isClientError && get(event, 'data.errno') === 'ECANCELED';

if (!isEcanceled) return null;

return {
event: 'log',
pid: event.pid,
timestamp: event.timestamp,
tags: ['debug', 'connection', 'ecanceled'],
data: 'ECANCELED: Socket was closed by the client (probably the browser) before the response could be completed'
};
return downgradeIfErrorMatches('ECANCELED', event);
}

_transform(event, enc, next) {
Expand Down

0 comments on commit aaab285

Please sign in to comment.