@bbc/http-transport
Advanced tools
Comparing version 0.0.13 to 0.0.14
@@ -9,2 +9,6 @@ 'use strict'; | ||
function isCriticalError(ctx) { | ||
return ctx.res.statusCode >= 500; | ||
} | ||
function hasElapsedTime(ctx) { | ||
@@ -14,9 +18,21 @@ return !_.isUndefined(_.get(ctx, 'res.elapsedTime')); | ||
function createMessage(ctx) { | ||
function getBaseMessage(ctx) { | ||
return `${ctx.req.getMethod()} ${ctx.req.getUrl()} ${ctx.res.statusCode}`; | ||
} | ||
function createRequestMessage(ctx) { | ||
const res = ctx.res; | ||
const message = getBaseMessage(ctx); | ||
if (hasElapsedTime(ctx)) return `${message} ${res.elapsedTime} ms`; | ||
return message; | ||
} | ||
function createRetryMessage(ctx) { | ||
const res = ctx.res; | ||
const attempts = res.retries; | ||
let message = `${ctx.req.getMethod()} ${ctx.req.getUrl()} ${res.statusCode}`; | ||
if (isRetry(ctx)) message = `Attempt ${attempts.length} ${message}`; | ||
if (hasElapsedTime(ctx)) message += ` ${res.elapsedTime} ms`; | ||
const message = `Attempt ${attempts.length} ${getBaseMessage(ctx)}`; | ||
if (hasElapsedTime(ctx)) return `${message} ${res.elapsedTime} ms`; | ||
@@ -30,9 +46,10 @@ return message; | ||
return (ctx, next) => { | ||
return next().then(() => { | ||
if (isRetry(ctx)) { | ||
return logger.warn(createMessage(ctx)); | ||
} | ||
logger.info(createMessage(ctx)); | ||
}); | ||
return next() | ||
.then(() => { | ||
if (isRetry(ctx) && isCriticalError(ctx)) { | ||
return logger.warn(createRetryMessage(ctx)); | ||
} | ||
logger.info(createRequestMessage(ctx)); | ||
}); | ||
}; | ||
}; |
{ | ||
"name": "@bbc/http-transport", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "A flexible, modular REST client built for ease-of-use and resilience.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -105,6 +105,6 @@ 'use strict'; | ||
nock(host, { | ||
reqheaders: { | ||
'User-Agent': HeaderValue | ||
} | ||
}) | ||
reqheaders: { | ||
'User-Agent': HeaderValue | ||
} | ||
}) | ||
.get(path) | ||
@@ -370,7 +370,7 @@ .times(2) | ||
nock(host, { | ||
reqheaders: { | ||
'User-Agent': HeaderValue, | ||
foo: 'bar' | ||
} | ||
}) | ||
reqheaders: { | ||
'User-Agent': HeaderValue, | ||
foo: 'bar' | ||
} | ||
}) | ||
.get(path) | ||
@@ -582,4 +582,4 @@ .reply(200, responseBody); | ||
api.defaultReplyHeaders({ | ||
'Content-Type': 'application/json' | ||
}) | ||
'Content-Type': 'application/json' | ||
}) | ||
.get(path) | ||
@@ -651,3 +651,3 @@ .reply(200, responseBody); | ||
it('logs retry attempts as warnings', () => { | ||
it('logs retry attempts as warnings when they return a critical error', () => { | ||
sandbox.stub(console, 'info'); | ||
@@ -668,8 +668,7 @@ sandbox.stub(console, 'warn'); | ||
/*eslint no-console: ["error", { allow: ["info", "warn"] }] */ | ||
sinon.assert.calledOnce(console.warn); | ||
const intial = console.info.getCall(0).args[0]; | ||
const attempt1 = console.warn.getCall(0).args[0]; | ||
const attempt2 = console.warn.getCall(1).args[0]; | ||
assert.match(intial, /GET http:\/\/www.example.com\/ 500 \d+ ms/); | ||
assert.match(attempt1, /Attempt 1 GET http:\/\/www.example.com\/ 500 \d+ ms/); | ||
assert.match(attempt2, /Attempt 2 GET http:\/\/www.example.com\/ 200 \d+ ms/); | ||
}); | ||
@@ -676,0 +675,0 @@ }); |
327445
6168