Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward invocation error details #191

Merged
merged 1 commit into from
Apr 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/http-invocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,13 @@ HttpInvocation.prototype.transformResponse = function(res, body, callback) {
if (hasError) {
if (isObject && body.error) {
err = new Error(body.error.message);
err.name = body.error.name;
err.stack = body.error.stack;
err.details = body.error.details;
for (var key in body.error) {
err[key] = body.error[key];
}
} else {
err = new Error('Error: ' + res.statusCode);
err.statusCode = res.statusCode;
err.details = body;
}

return callback(err);
Expand Down
49 changes: 49 additions & 0 deletions test/http-invocation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,55 @@ describe('HttpInvocation', function() {
this.foo = data.foo;
}
});

it('should forward all error properties', function(done) {
var method = givenSharedStaticMethod({});
var inv = new HttpInvocation(method);
var res = {
statusCode: 555,
body: {
error: {
name: 'CustomError',
message: 'Custom error message',
statusCode: 555,
details: {
key: 'value'
},
extra: 'extra value'
}
}
};

inv.transformResponse(res, res.body, function(err) {
if (!err)
return done(new Error('transformResponse should have failed.'));

expect(err).to.have.property('name', 'CustomError');
expect(err).to.have.property('message', 'Custom error message');
expect(err).to.have.property('statusCode', 555);
expect(err).to.have.property('details').eql({ key: 'value' });
expect(err).to.have.property('extra', 'extra value');
done();
});
});

it('should forward statusCode and non-object error response', function(done) {
var method = givenSharedStaticMethod({});
var inv = new HttpInvocation(method);
var res = {
statusCode: 555,
body: 'error body'
};

inv.transformResponse(res, res.body, function(err) {
if (!err)
return done(new Error('transformResponse should have failed.'));

expect(err).to.have.property('statusCode', 555);
expect(err).to.have.property('details', 'error body');
done();
});
});
});
});

Expand Down