mirror of
https://github.com/HackPlan/google-auth-library-nodejs.git
synced 2026-01-12 22:44:19 +08:00
Merge pull request #26 from gfxmonk/master
Create a meaningful error from response.error.errors array
This commit is contained in:
@@ -77,6 +77,14 @@ DefaultTransporter.prototype.wrapCallback_ = function(opt_callback) {
|
||||
if (typeof body.error === 'string') {
|
||||
err = new Error(body.error);
|
||||
err.code = res.statusCode;
|
||||
|
||||
} else if (Array.isArray(body.error.errors)) {
|
||||
err = new Error(body.error.errors.map(
|
||||
function(err) { return err.message; }
|
||||
).join('\n'));
|
||||
err.code = body.error.errors[0].code;
|
||||
err.errors = body.error.errors;
|
||||
|
||||
} else {
|
||||
err = new Error(body.error.message);
|
||||
err.code = body.error.code || res.statusCode;
|
||||
|
||||
@@ -41,4 +41,32 @@ describe('Transporters', function() {
|
||||
var re = new RegExp(applicationName + ' ' + defaultUserAgentRE);
|
||||
assert(re.test(opts.headers['User-Agent']));
|
||||
});
|
||||
|
||||
it('should create a single error from multiple response errors', function(done) {
|
||||
var firstError = {
|
||||
message: 'Error 1',
|
||||
code: 'ERR1',
|
||||
};
|
||||
var secondError = {
|
||||
message: 'Error 2',
|
||||
code: 'ERR2',
|
||||
};
|
||||
nock('http://example.com')
|
||||
.get('/api')
|
||||
.reply(200, {
|
||||
error: {
|
||||
errors: [ firstError, secondError ]
|
||||
}
|
||||
});
|
||||
|
||||
transporter.request({
|
||||
uri: 'http://example.com/api',
|
||||
}, function(error) {
|
||||
assert(error.message === 'Error 1\nError 2');
|
||||
assert(error.code, 'ERR1');
|
||||
assert(error.errors.length, 2);
|
||||
assert(error.errors[1].code, 'ERR2');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user