@altangent/http-client
Advanced tools
Comparing version 2.2.0 to 2.2.1
{ | ||
"name": "@altangent/http-client", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Simple promise based http/https client for Node", | ||
@@ -5,0 +5,0 @@ "main": "src", |
@@ -8,3 +8,6 @@ const http = require('http'); | ||
function request(opts, { idleTimeoutMs = 10000, connectionTimeoutMs = 10000, parseJson = true } = {}) { | ||
function request( | ||
opts, | ||
{ idleTimeoutMs = 10000, connectionTimeoutMs = 10000, parseJson = true } = {} | ||
) { | ||
return new Promise((resolve, reject) => { | ||
@@ -26,14 +29,21 @@ function callback(res) { | ||
let body = Buffer.concat(buffers); | ||
if(!parseJson) return resolve(body); | ||
let json = JSON.parse(body); | ||
if (json.error) { | ||
let error = new Error('HTTPS failed with error payload'); | ||
error.statusCode = res.statusCode; | ||
error.request = opts; | ||
error.body = json; | ||
return reject(error); | ||
if (!parseJson) return resolve(body); | ||
try { | ||
let json = JSON.parse(body); | ||
if (json.error) { | ||
let error = new Error('HTTPS failed with error payload'); | ||
error.statusCode = res.statusCode; | ||
error.request = opts; | ||
error.body = json; | ||
return reject(error); | ||
} | ||
else { | ||
return resolve(json); | ||
} | ||
} | ||
else { | ||
return resolve(json); | ||
catch (ex) { | ||
let err = new Error('Invalid JSON response'); | ||
err.subError = ex; | ||
err.body = body.toString(); | ||
return reject(err); | ||
} | ||
@@ -40,0 +50,0 @@ } |
@@ -56,6 +56,20 @@ const nock = require('nock'); | ||
}); | ||
describe('when error in json response', () => { | ||
describe('when invalid json response', () => { | ||
before(async () => { | ||
nock('https://test') | ||
.get('/somepath') | ||
.reply(200, '{"bad":'); | ||
await run(); | ||
}); | ||
it('should reject with error', () => { | ||
expect(result.message).to.equal('Invalid JSON response'); | ||
}); | ||
it('should include sub-body', () => { | ||
expect(result.body).to.equal('{"bad":'); | ||
}); | ||
}); | ||
describe('when error object in json response', () => { | ||
before(async () => { | ||
nock('https://test') | ||
.get('/somepath') | ||
.reply(200, { error: 'things went poorly' }); | ||
@@ -62,0 +76,0 @@ await run(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5971
177