Skip to content

Commit d0c1707

Browse files
authored
Merge pull request #79 from tidepool-org/krystophv/electron
WIP: Update superagent in order to be able to bundle for Electron
2 parents a717413 + 17251e1 commit d0c1707

6 files changed

Lines changed: 70 additions & 8 deletions

File tree

confirm.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ module.exports = function (common, deps) {
5353
.put(common.makeAPIUrl('/confirm/accept/signup/'+signupId))
5454
.end(function (err, res) {
5555
if (err != null) {
56+
// TODO: update version of lodash so we can use _.get
57+
err.message = (err.response && err.response.body && err.response.body.reason) || '';
5658
return cb(err);
5759
}
5860
if (res.status !== 200) {
@@ -77,6 +79,8 @@ module.exports = function (common, deps) {
7779
.send({birthday: birthday, password: password})
7880
.end(function (err, res) {
7981
if (err != null) {
82+
err.error = (err.response && err.response.body && err.response.body.error) || '';
83+
err.message = (err.response && err.response.body && err.response.body.reason) || '';
8084
return cb(err);
8185
}
8286
if (res.status !== 200) {
@@ -98,6 +102,7 @@ module.exports = function (common, deps) {
98102
.post(common.makeAPIUrl('/confirm/resend/signup/' + email))
99103
.end(function (err, res) {
100104
if (err != null) {
105+
err.message = (err.response && err.response.error) || '';
101106
return cb(err);
102107
}
103108
if (res.status !== 200) {
@@ -154,7 +159,10 @@ module.exports = function (common, deps) {
154159
.end(
155160
function (err, res) {
156161
if (err != null) {
157-
return cb(err);
162+
if(err.status === 404) {
163+
return cb(null,[]);
164+
}
165+
return cb( (err.response && err.response.body) || [] );
158166
}
159167
if (res.status === 200) {
160168
return cb(null,res.body);
@@ -253,6 +261,7 @@ module.exports = function (common, deps) {
253261
.post(common.makeAPIUrl('/confirm/send/forgot/' + email))
254262
.end(function (err, res) {
255263
if (err != null) {
264+
err.message = (err.response && err.response.error) || '';
256265
return cb(err);
257266
}
258267
if (res.status !== 200) {
@@ -280,6 +289,7 @@ module.exports = function (common, deps) {
280289
.send(payload)
281290
.end(function (err, res) {
282291
if (err != null) {
292+
err.message = (err.response && err.response.error) || '';
283293
return cb(err);
284294
}
285295
if (res.status !== 200) {

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,9 @@ module.exports = function (config, deps) {
477477
.end(
478478
function (err, res) {
479479
if (err != null) {
480+
if (err.status !== 201) {
481+
return cb(new Error('Unexpected HTTP response: ' + err.status));
482+
}
480483
return cb(err);
481484
} else if (res.error === true) {
482485
if(_.isObject(res.body)) {
@@ -513,6 +516,9 @@ module.exports = function (config, deps) {
513516
function (err, res) {
514517

515518
if (err != null) {
519+
if (err.status !== 200) {
520+
return cb((err.response && err.response.body) || err);
521+
}
516522
return cb(err);
517523
} else if (res.status !== 200) {
518524
return cb(res.body);
@@ -544,6 +550,9 @@ module.exports = function (config, deps) {
544550
function (err, res) {
545551

546552
if (err != null) {
553+
if (err.status !== 200) {
554+
return cb((err.response && err.response.body) || err);
555+
}
547556
return cb(err);
548557
} else if (res.status !== 200) {
549558
return cb(res.body);
@@ -576,6 +585,9 @@ module.exports = function (config, deps) {
576585
function (err, res) {
577586

578587
if (err != null) {
588+
if (err.status !== 200) {
589+
return cb((err.response && err.response.body) || err);
590+
}
579591
return cb(err);
580592
} else if (res.status !== 200) {
581593
return cb(res.body);
@@ -662,6 +674,7 @@ module.exports = function (config, deps) {
662674
.end(
663675
function (err, res) {
664676
if (!_.isEmpty(err)) {
677+
err.body = (err.response && err.response.body) || '';
665678
log.info('Sync failed', JSON.stringify(err));
666679
return done(err);
667680
}
@@ -696,6 +709,7 @@ module.exports = function (config, deps) {
696709
.end(
697710
function (err, res) {
698711
if (!_.isEmpty(err)) {
712+
err.body = (err.response && err.response.body) || '';
699713
log.info('Upload Failed');
700714
return cb(err);
701715
}
@@ -740,6 +754,7 @@ module.exports = function (config, deps) {
740754
.end(
741755
function (err, res) {
742756
if (err) {
757+
err.body = (err.response && err.response.body) || '';
743758
return cb(err);
744759
}
745760

lib/common.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ module.exports = function (cfg, deps) {
106106
return null;
107107
}
108108
// We previously exposed the user's session id here - which we identified as a security risk.
109-
// We would have just removed the token query param altogether, but the URl that we direct user's to
109+
// We would have just removed the token query param altogether, but the URl that we direct user's to
110110
// is configured in a way where the token needs to be set for the Chrome Uploader to be launched
111111
// So for now we are setting the token to a constant.
112112
return makeUploadUrl('', { launchUploader: 'true' });
@@ -213,6 +213,15 @@ module.exports = function (cfg, deps) {
213213
.end(
214214
function (err, res) {
215215
if (err != null) {
216+
if (_.has(codes, err.status)) {
217+
var handler = codes[err.status];
218+
if (typeof(handler) === 'function') {
219+
return cb(null, handler(err.response));
220+
} else {
221+
return cb(null, handler);
222+
}
223+
}
224+
err.body = err.response.body;
216225
return cb(err);
217226
}
218227

@@ -257,6 +266,15 @@ module.exports = function (cfg, deps) {
257266
.end(
258267
function (err, res) {
259268
if (err != null) {
269+
if (_.has(codes, err.status)) {
270+
var handler = codes[err.status];
271+
if (typeof(handler) === 'function') {
272+
return cb(null, handler(err.response));
273+
} else {
274+
return cb(null, handler);
275+
}
276+
}
277+
err.body = err.response.body;
260278
return cb(err);
261279
}
262280

@@ -301,6 +319,15 @@ module.exports = function (cfg, deps) {
301319
.end(
302320
function (err, res) {
303321
if (err != null) {
322+
if (_.has(codes, err.status)) {
323+
var handler = codes[err.status];
324+
if (typeof(handler) === 'function') {
325+
return cb(null, handler(err.response));
326+
} else {
327+
return cb(null, handler);
328+
}
329+
}
330+
err.body = err.response.body;
304331
return cb(err);
305332
}
306333

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tidepool-platform-client",
3-
"version": "0.31.1",
3+
"version": "0.32.0",
44
"description": "Client-side library to interact with the Tidepool platform",
55
"main": "tidepool.js",
66
"scripts": {
@@ -14,7 +14,7 @@
1414
"crypto": "0.0.3",
1515
"lodash": "3.3.1",
1616
"node-uuid": "1.4.3",
17-
"superagent": "0.21.0"
17+
"superagent": "1.2.0"
1818
},
1919
"devDependencies": {
2020
"grunt": "0.4.5",

test/integration/tidepoolPlatform_integration.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ describe('platform client', function () {
248248
it('get another users public profile', function (done) {
249249
//logged in as a_PWD you can get the profile for a_Member
250250
pwdClient.findProfile(a_Member.id, function (error, profile) {
251-
expect(error).to.deep.equal({ status: 401, body: 'Unauthorized' });
251+
expect(error).to.include({ status: 401, body: 'Unauthorized' });
252252
done();
253253
});
254254
});
@@ -279,7 +279,7 @@ describe('platform client', function () {
279279
it('get another user\'s public preferences', function (done) {
280280
//logged in as a_PWD you can get the profile for a_Member
281281
pwdClient.findPreferences(a_Member.id, function (error, settings) {
282-
expect(error).to.deep.equal({ status: 401, body: 'Unauthorized' });
282+
expect(error).to.include({ status: 401, body: 'Unauthorized' });
283283
done();
284284
});
285285
});
@@ -308,7 +308,7 @@ describe('platform client', function () {
308308
it('get another user\'s public settings', function (done) {
309309
//logged in as a_PWD you can get the profile for a_Member
310310
pwdClient.findSettings(a_Member.id, function (error, settings) {
311-
expect(error).to.deep.equal({ status: 401, body: 'Unauthorized' });
311+
expect(error).to.include({ status: 401, body: 'Unauthorized' });
312312
done();
313313
});
314314
});
@@ -545,7 +545,7 @@ describe('platform client', function () {
545545

546546
it('but a_Member cannot see the other team members for a_PWD', function (done) {
547547
memberClient.getTeamMembers(a_PWD.id, function (error, patientsTeam) {
548-
expect(error).to.deep.equal({ status: 401, body: 'Unauthorized' });
548+
expect(error).to.include({ status: 401, body: 'Unauthorized' });
549549
done();
550550
});
551551
});

user.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ module.exports = function (common, config, deps) {
8989
.end(
9090
function (err, res) {
9191
if (err) {
92+
err.body = (err.response && err.response.body) || '';
9293
return cb(err, null);
9394
}
9495
if (res.status !== 200) {
@@ -190,6 +191,7 @@ module.exports = function (common, config, deps) {
190191
function (err, res) {
191192

192193
if (err != null) {
194+
err.body = (err.response && err.response.body) || '';
193195
return cb(err, null);
194196
}
195197

@@ -232,6 +234,7 @@ module.exports = function (common, config, deps) {
232234
.end(
233235
function (err, res) {
234236
if (err != null) {
237+
err.body = (err.response && err.response.body) || '';
235238
return cb(err, null);
236239
}
237240

@@ -300,6 +303,7 @@ module.exports = function (common, config, deps) {
300303
.end(
301304
function (err, res) {
302305
if (err != null) {
306+
err.body = (err.response && err.response.body) || '';
303307
return cb(err);
304308
}
305309
var theUserId = res.body.userid;
@@ -337,6 +341,8 @@ module.exports = function (common, config, deps) {
337341
.end(
338342
function (err, res) {
339343
if (err != null) {
344+
err.body = (err.response && err.response.body) || '';
345+
err.message = (err.response && err.response.error) || '';
340346
return next(err);
341347
}
342348
if(res.status === 201){
@@ -355,6 +361,8 @@ module.exports = function (common, config, deps) {
355361
.end(
356362
function (err, res) {
357363
if (err != null) {
364+
err.body = (err.response && err.response.body) || '';
365+
err.message = (err.response && err.response.error) || '';
358366
return next(err);
359367
}
360368
if(res.status === 200){
@@ -375,6 +383,8 @@ module.exports = function (common, config, deps) {
375383
.end(
376384
function (err, res) {
377385
if (err != null) {
386+
err.body = (err.response && err.response.body) || '';
387+
err.message = (err.response && err.response.error) || '';
378388
return next(err);
379389
}
380390
if(res.status === 200){

0 commit comments

Comments
 (0)