Skip to content

Commit

Permalink
feat: introduced TransformError for failed transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
analog-nico committed Apr 16, 2016
1 parent d678f35 commit 0cfa8f2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
21 changes: 20 additions & 1 deletion lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,26 @@ StatusCodeError.prototype = Object.create(Error.prototype);
StatusCodeError.prototype.constructor = StatusCodeError;


function TransformError(cause, options, response) {

this.name = 'TransformError';
this.message = String(cause);
this.cause = cause;
this.error = cause; // legacy attribute
this.options = options;
this.response = response;

if (Error.captureStackTrace) { // if required for non-V8 envs - see PR #40
Error.captureStackTrace(this);
}

}
TransformError.prototype = Object.create(Error.prototype);
TransformError.prototype.constructor = TransformError;


module.exports = {
RequestError: RequestError,
StatusCodeError: StatusCodeError
StatusCodeError: StatusCodeError,
TransformError: TransformError
};
2 changes: 1 addition & 1 deletion lib/rp.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function RP$callback(err, response, body) {
try {
self._rp_resolve(self._rp_options.transform(body, response, self._rp_options.resolveWithFullResponse));
} catch (e) {
self._rp_reject(e);
self._rp_reject(new errors.TransformError(e, self._rp_options, response));
}
} else if (self._rp_options.resolveWithFullResponse) {
self._rp_resolve(response);
Expand Down
22 changes: 20 additions & 2 deletions test/spec/request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,19 +347,37 @@ describe('Request-Promise', function () {

it('that throws an exception', function () {

var cause = new Error('Transform failed!');

var options = {
url: 'http://localhost:4000/200',
transform: function (body) {
throw new Error('Transform failed!');
throw cause;
}
};

var expectedOptions = {
url: 'http://localhost:4000/200',
simple: true,
resolveWithFullResponse: false,
transform: options.transform
};

return rp(options)
.then(function (transformedResponse) {
throw new Error('Request should not have been fulfilled!');
})
.catch(function (err) {
expect(err.message).to.eql('Transform failed!');
expect(err instanceof errors.TransformError).to.eql(true);
expect(err.name).to.eql('TransformError');
expect(err.message).to.eql('Error: Transform failed!');
expect(err.cause).to.eql(cause);
expect(err.error).to.eql(cause);
delete err.options.callback; // Even out Request version differences.
expect(err.options).to.eql(expectedOptions);
expect(err.response).to.be.an('object');
expect(err.response.body).to.eql('GET /200');
expect(err.response.statusCode).to.eql(200);
});

});
Expand Down

0 comments on commit 0cfa8f2

Please sign in to comment.