Skip to content

Commit

Permalink
refactor: error attribute assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
analog-nico committed Apr 16, 2016
1 parent 2ad8792 commit d678f35
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
12 changes: 9 additions & 3 deletions lib/errors.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand Down
12 changes: 2 additions & 10 deletions lib/rp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit d678f35

Please sign in to comment.