diff --git a/lib/IAMClient.js b/lib/IAMClient.js index 1fecca43..563e5e32 100644 --- a/lib/IAMClient.js +++ b/lib/IAMClient.js @@ -413,7 +413,7 @@ class IAMClient { }) .on('end', () => { - this.handleResponse(res, ret, callback); + this.handleResponse(res, ret, logger, callback); }); }); req.on('error', err => { @@ -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')); }); } } diff --git a/tests/unit/handleResponse.js b/tests/unit/handleResponse.js new file mode 100644 index 00000000..ca12d9d6 --- /dev/null +++ b/tests/unit/handleResponse.js @@ -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 = 'foo'; +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(); + }); + }); +});