Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 0175894

Browse files
authored
Merge pull request #153 from glynnbird/promisesissues
Promisesissues
2 parents ec9acec + 2a1e82d commit 0175894

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

cloudant.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ function Cloudant(options, callback) {
4343
if (typeof options == "object") {
4444
if (options.requestDefaults) {
4545
requestDefaults = options.requestDefaults;
46-
delete options.requestDefaults;
4746
}
4847
theurl = reconfigure(options);
4948
} else {
@@ -60,6 +59,7 @@ function Cloudant(options, callback) {
6059
// plugin a request library
6160
var plugin = null;
6261
if (options.plugin) {
62+
options.requestDefaults = requestDefaults;
6363
if(typeof options.plugin === 'string') {
6464
var plugintype = options.plugin || 'default';
6565
debug('Using the "' + plugintype + '" plugin');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"license": "Apache-2.0",
55
"homepage": "http://github.com/cloudant/nodejs-cloudant",
66
"repository": "git://github.com/cloudant/nodejs-cloudant",
7-
"version": "1.5.0",
7+
"version": "1.5.1",
88
"author": "Jason Smith <[email protected]>",
99
"contributors": [
1010
"Glynn Bird <[email protected]>"

plugins/promises.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ module.exports = function(options) {
2929
return new Promise(function(resolve, reject) {
3030
request(req, function(err, h, b) {
3131
var statusCode = h && h.statusCode || 500;
32+
if (b) {
33+
try { b = JSON.parse(b); } catch (err) { }
34+
}
3235
if (statusCode >= 200 && statusCode < 400) {
3336
callback(null, h, b);
3437
return resolve(b);
3538
}
39+
if (b) {
40+
b.statusCode = statusCode;
41+
}
3642
reject(err || b);
3743
callback(err, h, b);
3844
})

tests/plugin.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,32 @@ describe('promise plugin', function() {
5353

5454
it('should return a promise', function(done) {
5555
var mocks = nock(SERVER)
56-
.get('/' + MYDB).reply(200, {});
56+
.get('/' + MYDB).reply(200, { ok: true});
5757
var cloudant = Cloudant({plugin:'promises', account:ME, password: PASSWORD});
5858
var db = cloudant.db.use(MYDB);
5959
var p = db.info().then(function(data) {
60-
data.should.be.an.object;
60+
data.should.be.an.Object;
6161
done();
6262
});
6363
assert.equal(p instanceof Promise, true);
64-
})
64+
});
65+
66+
it('should return an error status code', function(done) {
67+
var mocks = nock(SERVER)
68+
.get('/' + MYDB).reply(404, { ok: false});
69+
var cloudant = Cloudant({plugin:'promises', account:ME, password: PASSWORD});
70+
var db = cloudant.db.use(MYDB);
71+
var p = db.info().then(function(data) {
72+
assert(false);
73+
}).catch(function(e) {
74+
e.should.be.an.Object;
75+
e.should.have.property.statusCode;
76+
e.statusCode.should.be.a.Number;
77+
e.statusCode.should.equal(404);
78+
done();
79+
});
80+
assert.equal(p instanceof Promise, true);
81+
});
6582

6683
});
6784

0 commit comments

Comments
 (0)