Commit d678f35e authored by analog-nico's avatar analog-nico
Browse files

refactor: error attribute assignment

parent 2ad87923
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
'use strict';


function RequestError(cause) {
function RequestError(cause, options, response) {

    this.name = 'RequestError';
    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);
@@ -16,11 +19,14 @@ RequestError.prototype = Object.create(Error.prototype);
RequestError.prototype.constructor = RequestError;


function StatusCodeError(statusCode, message) {
function StatusCodeError(statusCode, body, options, response) {

    this.name = 'StatusCodeError';
    this.statusCode = statusCode;
    this.message = statusCode + ' - ' + (JSON && JSON.stringify ? JSON.stringify(message) : message);
    this.message = statusCode + ' - ' + (JSON && JSON.stringify ? JSON.stringify(body) : body);
    this.error = body; // legacy attribute
    this.options = options;
    this.response = response;

    if (Error.captureStackTrace) { // if required for non-V8 envs - see PR #40
        Error.captureStackTrace(this);
+2 −10
Original line number Diff line number Diff line
@@ -57,19 +57,11 @@ function RP$callback(err, response, body) {

    if (err) {

        self._rp_reject(assign(new errors.RequestError(err), {
            error: err,
            options: self._rp_options,
            response: response
        }));
        self._rp_reject(new errors.RequestError(err, self._rp_options, response));

    } else if (self._rp_options.simple && !(/^2/.test('' + response.statusCode))) {

        self._rp_reject(assign(new errors.StatusCodeError(response.statusCode, body), {
            error: body,
            options: self._rp_options,
            response: response
        }));
        self._rp_reject(new errors.StatusCodeError(response.statusCode, body, self._rp_options, response));

    } else {
        if (isFunction(self._rp_options.transform)) {