Skip to content

Commit

Permalink
Merge pull request #191 from strongloop/fix/invocation-error-details
Browse files Browse the repository at this point in the history
Forward invocation error details
  • Loading branch information
bajtos committed Apr 2, 2015
2 parents 97bedcb + 350830c commit b091c68
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
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

0 comments on commit b091c68

Please sign in to comment.