Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/fix/referenceError' into rel/6…
Browse files Browse the repository at this point in the history
….2.5
  • Loading branch information
Cloud User committed Jan 23, 2017
2 parents 62ca059 + db898b1 commit ccb74f8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
28 changes: 24 additions & 4 deletions lib/IAMClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ class IAMClient {
})

.on('end', () => {
this.handleResponse(res, ret, callback);
this.handleResponse(res, ret, logger, callback);
});
});
req.on('error', err => {
Expand Down Expand Up @@ -476,21 +476,41 @@ class IAMClient {
* @param {object} res - response to the request
* @param {string} ret - content (data and/or metadata) returned after the
* request has been processed
* @param {object} log - werelogs request logger
* @param {IAMClient~requestCallback} callback - callback
* @returns {undefined}
*/
handleResponse(res, ret, callback) {
handleResponse(res, ret, log, callback) {
this.getObj(ret, (err, obj) => {
if (err) {
if (log) {
log.error('error from vault',
{ error: err, method: 'IAMClient.handleResponse' });
}
return callback(err, null, res.statusCode);
}
/* This is not an error */
if (res.statusCode < 400) {
return callback(null, obj, res.statusCode);
}

// Load the error from errors(arsenal)
return callback(
errors[obj.ErrorResponse.Error.Code], null, res.statusCode);
if (obj && obj.ErrorResponse && obj.ErrorResponse.Error) {
if (log) {
log.debug('error from vault',
{ error: obj, method: 'IAMClient.handleResponse' });
}
return callback(
errors[obj.ErrorResponse.Error.Code], null, res.statusCode);
}
if (log) {
log.error('unable to translate error from vault', {
error: obj,
method: 'IAMClient.handleResponse',
});
}
return callback(errors.InternalError
.customizeDescription('unable to translate error from vault'));
});
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/handleResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'; // eslint-disable-line
// const http = require('http');
const assert = require('assert');
const errors = require('arsenal').errors;
const IAMClient = require('../../lib/IAMClient.js');

const log = { error() {} };
const res = { statusCode: 400 };
const ret = '<Response><Code>foo</Code></Response>';
const expErr = errors.InternalError
.customizeDescription('unable to translate error from vault');

describe('handling unrecognized error syntax', () => {
let client;

beforeEach('create client', () => client = new IAMClient('127.0.0.1'));

afterEach('delete client', () => { client = undefined; });

it('should return Internal Error for unrecognized errors', done => {
client.handleResponse(res, ret, log, err => {
assert.deepStrictEqual(err, expErr);
return done();
});
});
});

0 comments on commit ccb74f8

Please sign in to comment.