Skip to content

Allow parsing of error object by given transform #86

@Limess

Description

@Limess

request-promise doesn't seem to apply the given options.transform function to the response object attached to a StatusCodeError.

I'd expect that transform to be applied, given the relation implied by "Rejected promises and the simple option" example. In this case, the standard request library would have parsed the request body before it was filtered by status code. The response at this point is not an error with the request so I see no gain in not parsing the response.

I realise this would be a breaking change, so was wondering if it would be of interest to pass an additional option to enable this behaviour?

For example, something along the lines of

} else if (self._rp_options.simple && !(/^2/.test('' + response.statusCode))) {
    var statusCodeError;
    if (self._rp_options.transformError && isFunction(self._rp_options.transform)) {
        try {
            body = self._rp_options.transform(body, response, self._rp_options.resolveWithFullResponse);
        } catch (e) {
            return self._rp_reject(e);
        }
     }
     self._rp_reject(assign(new errors.StatusCodeError(response.statusCode, body), {
        error: body,
        options: self._rp_options,
        response: response
     });

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions