diff --git a/lib/errors.js b/lib/errors.js index 7a45b1c..7967754 100644 --- a/lib/errors.js +++ b/lib/errors.js @@ -1,11 +1,14 @@ '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); diff --git a/lib/rp.js b/lib/rp.js index 4dd1706..3ea968d 100644 --- a/lib/rp.js +++ b/lib/rp.js @@ -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)) {