Commit 4bf3c083 authored by analog-nico's avatar analog-nico
Browse files

Transform function may return a Promise

parent 723f7a85
Loading
Loading
Loading
Loading
+17 −13
Original line number Diff line number Diff line
@@ -8,34 +8,38 @@ var request = require('request'),
function ownCallback(err, httpResponse, body) {

    /* jshint validthis:true */
    var self = this;

    if (_.isFunction(this._rp_callbackOrig)) {
    if (_.isFunction(self._rp_callbackOrig)) {
        try {
            this._rp_callbackOrig.apply(this, arguments);
            self._rp_callbackOrig.apply(self, arguments);
        } catch (e) { }
    }

    // FIXME: Access to httpResponse.statusCode could crash.

    if (err) {
        this._rp_reject({
        self._rp_reject({
            error: err,
            options: this._rp_options,
            options: self._rp_options,
            response: httpResponse
        });
    } else if (this._rp_options.simple && !(/^2/.test('' + httpResponse.statusCode))) {
        this._rp_reject({
    } else if (self._rp_options.simple && !(/^2/.test('' + httpResponse.statusCode))) {
        self._rp_reject({
            error: body,
            options: this._rp_options,
            options: self._rp_options,
            response: httpResponse,
            statusCode: httpResponse.statusCode
        });
    } else {
        if (this._rp_options.transform && typeof this._rp_options.transform === 'function') {
            // FIXME: Produces unhandled exception if transform fails.
            this._rp_resolve(this._rp_options.transform(body));
        } else if (this._rp_options.resolveWithFullResponse) {
            this._rp_resolve(httpResponse);
        if (_.isFunction(self._rp_options.transform)) {
            self._rp_resolve(new Bluebird(function (resolve) {
                resolve(self._rp_options.transform(body));
            }));
        } else if (self._rp_options.resolveWithFullResponse) {
            self._rp_resolve(httpResponse);
        } else {
            this._rp_resolve(body);
            self._rp_resolve(body);
        }
    }
}