Commit 0cfa8f27 authored by analog-nico's avatar analog-nico
Browse files

feat: introduced TransformError for failed transforms

parent d678f35e
Loading
Loading
Loading
Loading
+20 −1
Original line number Diff line number Diff line
@@ -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
};
+1 −1
Original line number Diff line number Diff line
@@ -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);
+20 −2
Original line number Diff line number Diff line
@@ -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);
                });

        });