Loading errors.js 0 → 100644 +1 −0 Original line number Diff line number Diff line module.exports = require('./lib/errors.js'); lib/errors.js 0 → 100644 +33 −0 Original line number Diff line number Diff line 'use strict'; function RequestError(cause) { this.name = 'RequestError'; this.message = String(cause); this.cause = cause; Error.captureStackTrace(this); } RequestError.prototype = Object.create(Error.prototype); RequestError.prototype.constructor = RequestError; function StatusCodeError(statusCode, message) { this.name = 'StatusCodeError'; this.statusCode = statusCode; this.message = statusCode + ' - ' + message; Error.captureStackTrace(this); } StatusCodeError.prototype = Object.create(Error.prototype); StatusCodeError.prototype.constructor = StatusCodeError; module.exports = { RequestError: RequestError, StatusCodeError: StatusCodeError }; lib/rp.js +11 −7 Original line number Diff line number Diff line Loading @@ -3,7 +3,8 @@ var Bluebird = require('./bluebird-fresh.js'), CapturedTrace = require('./bluebird-captured-trace-fresh.js'), _ = require('lodash'), chalk = require('chalk'); chalk = require('chalk'), errors = require('./errors.js'); // Load Request freshly - so that users can require an unaltered request instance! Loading Loading @@ -45,18 +46,21 @@ function RP$callback(err, response, body) { } if (err) { self._rp_reject({ self._rp_reject(_.assign(new errors.RequestError(err), { error: err, options: self._rp_options, response: response }); })); } else if (self._rp_options.simple && !(/^2/.test('' + response.statusCode))) { self._rp_reject({ self._rp_reject(_.assign(new errors.StatusCodeError(response.statusCode, body), { error: body, options: self._rp_options, response: response, statusCode: response.statusCode }); response: response })); } else { if (_.isFunction(self._rp_options.transform)) { try { Loading test/spec/request-test.js +23 −0 Original line number Diff line number Diff line 'use strict'; var rp = require('../../lib/rp.js'); var errors = require('../../errors.js'); var http = require('http'); var url = require('url'); var Bluebird = require('bluebird'); Loading Loading @@ -114,11 +115,22 @@ describe('Request-Promise', function () { }) .catch(function (reason) { expect(reason).to.be.an('object'); expect(typeof reason).to.eql('object'); // Just double checking expect(reason.error.message).to.contain('connect ECONNREFUSED'); delete reason.options.callback; // Even out Request version differences. expect(reason.options).to.eql(expectedOptions); expect(reason.response).to.eql(undefined); expect(reason.statusCode).to.eql(undefined); // Test for Error type introduced in 0.4 expect(reason instanceof errors.RequestError).to.eql(true); expect(reason.name).to.eql('RequestError'); expect(reason.message).to.contain('connect ECONNREFUSED'); expect(reason.cause).to.eql(reason.error); throw reason; // Testing Bluebird's catch by type }) .catch(errors.RequestError, function (reason) { done(); }) .catch(done); Loading @@ -139,12 +151,23 @@ describe('Request-Promise', function () { }) .catch(function (reason) { expect(reason).to.be.an('object'); expect(typeof reason).to.eql('object'); // Just double checking expect(reason.error).to.eql('GET /404'); delete reason.options.callback; // Even out Request version differences. expect(reason.options).to.eql(expectedOptions); expect(reason.response).to.be.an('object'); expect(reason.response.body).to.eql('GET /404'); expect(reason.statusCode).to.eql(404); // Test for Error type introduced in 0.4 expect(reason instanceof errors.StatusCodeError).to.eql(true); expect(reason.name).to.eql('StatusCodeError'); expect(reason.statusCode).to.eql(404); expect(reason.message).to.eql('404 - GET /404'); throw reason; // Testing Bluebird's catch by type }) .catch(errors.StatusCodeError, function (reason) { done(); }) .catch(done); Loading Loading
errors.js 0 → 100644 +1 −0 Original line number Diff line number Diff line module.exports = require('./lib/errors.js');
lib/errors.js 0 → 100644 +33 −0 Original line number Diff line number Diff line 'use strict'; function RequestError(cause) { this.name = 'RequestError'; this.message = String(cause); this.cause = cause; Error.captureStackTrace(this); } RequestError.prototype = Object.create(Error.prototype); RequestError.prototype.constructor = RequestError; function StatusCodeError(statusCode, message) { this.name = 'StatusCodeError'; this.statusCode = statusCode; this.message = statusCode + ' - ' + message; Error.captureStackTrace(this); } StatusCodeError.prototype = Object.create(Error.prototype); StatusCodeError.prototype.constructor = StatusCodeError; module.exports = { RequestError: RequestError, StatusCodeError: StatusCodeError };
lib/rp.js +11 −7 Original line number Diff line number Diff line Loading @@ -3,7 +3,8 @@ var Bluebird = require('./bluebird-fresh.js'), CapturedTrace = require('./bluebird-captured-trace-fresh.js'), _ = require('lodash'), chalk = require('chalk'); chalk = require('chalk'), errors = require('./errors.js'); // Load Request freshly - so that users can require an unaltered request instance! Loading Loading @@ -45,18 +46,21 @@ function RP$callback(err, response, body) { } if (err) { self._rp_reject({ self._rp_reject(_.assign(new errors.RequestError(err), { error: err, options: self._rp_options, response: response }); })); } else if (self._rp_options.simple && !(/^2/.test('' + response.statusCode))) { self._rp_reject({ self._rp_reject(_.assign(new errors.StatusCodeError(response.statusCode, body), { error: body, options: self._rp_options, response: response, statusCode: response.statusCode }); response: response })); } else { if (_.isFunction(self._rp_options.transform)) { try { Loading
test/spec/request-test.js +23 −0 Original line number Diff line number Diff line 'use strict'; var rp = require('../../lib/rp.js'); var errors = require('../../errors.js'); var http = require('http'); var url = require('url'); var Bluebird = require('bluebird'); Loading Loading @@ -114,11 +115,22 @@ describe('Request-Promise', function () { }) .catch(function (reason) { expect(reason).to.be.an('object'); expect(typeof reason).to.eql('object'); // Just double checking expect(reason.error.message).to.contain('connect ECONNREFUSED'); delete reason.options.callback; // Even out Request version differences. expect(reason.options).to.eql(expectedOptions); expect(reason.response).to.eql(undefined); expect(reason.statusCode).to.eql(undefined); // Test for Error type introduced in 0.4 expect(reason instanceof errors.RequestError).to.eql(true); expect(reason.name).to.eql('RequestError'); expect(reason.message).to.contain('connect ECONNREFUSED'); expect(reason.cause).to.eql(reason.error); throw reason; // Testing Bluebird's catch by type }) .catch(errors.RequestError, function (reason) { done(); }) .catch(done); Loading @@ -139,12 +151,23 @@ describe('Request-Promise', function () { }) .catch(function (reason) { expect(reason).to.be.an('object'); expect(typeof reason).to.eql('object'); // Just double checking expect(reason.error).to.eql('GET /404'); delete reason.options.callback; // Even out Request version differences. expect(reason.options).to.eql(expectedOptions); expect(reason.response).to.be.an('object'); expect(reason.response.body).to.eql('GET /404'); expect(reason.statusCode).to.eql(404); // Test for Error type introduced in 0.4 expect(reason instanceof errors.StatusCodeError).to.eql(true); expect(reason.name).to.eql('StatusCodeError'); expect(reason.statusCode).to.eql(404); expect(reason.message).to.eql('404 - GET /404'); throw reason; // Testing Bluebird's catch by type }) .catch(errors.StatusCodeError, function (reason) { done(); }) .catch(done); Loading