Skip to content

Commit 8c12f80

Browse files
committed
Merge pull request #1269 from ParsePlatform/flovilmart.reportPushError
Improves report for Push error in logs and _PushStatus
2 parents c346e15 + 6055f2a commit 8c12f80

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

spec/PushController.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,45 @@ describe('PushController', () => {
317317

318318
});
319319

320+
it('should properly report failures in _PushStatus', (done) => {
321+
var pushAdapter = {
322+
send: function(body, installations) {
323+
return installations.map((installation) => {
324+
return Promise.resolve({
325+
deviceType: installation.deviceType
326+
})
327+
})
328+
},
329+
getValidPushTypes: function() {
330+
return ["ios"];
331+
}
332+
}
333+
let where = { 'channels': {
334+
'$ins': ['Giants', 'Mets']
335+
}};
336+
var payload = {data: {
337+
alert: "Hello World!",
338+
badge: 1,
339+
}}
340+
var config = new Config(Parse.applicationId);
341+
var auth = {
342+
isMaster: true
343+
}
344+
var pushController = new PushController(pushAdapter, Parse.applicationId);
345+
pushController.sendPush(payload, where, config, auth).then(() => {
346+
fail('should not succeed');
347+
done();
348+
}).catch(() => {
349+
let query = new Parse.Query('_PushStatus');
350+
query.find({useMasterKey: true}).then((results) => {
351+
expect(results.length).toBe(1);
352+
let pushStatus = results[0];
353+
expect(pushStatus.get('status')).toBe('failed');
354+
done();
355+
});
356+
})
357+
});
358+
320359
it('should support full RESTQuery for increment', (done) => {
321360
var payload = {data: {
322361
alert: "Hello World!",

src/Controllers/PushController.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export class PushController extends AdaptableController {
5252
let badgeUpdate = () => {
5353
return Promise.resolve();
5454
}
55-
5655
if (body.data && body.data.badge) {
5756
let badge = body.data.badge;
5857
let op = {};
@@ -89,10 +88,16 @@ export class PushController extends AdaptableController {
8988
}).then(() => {
9089
return rest.find(config, auth, '_Installation', where);
9190
}).then((response) => {
92-
pushStatus.setRunning();
91+
if (!response.results) {
92+
return Promise.reject({error: 'PushController: no results in query'})
93+
}
94+
pushStatus.setRunning(response.results);
9395
return this.sendToAdapter(body, response.results, pushStatus, config);
9496
}).then((results) => {
9597
return pushStatus.complete(results);
98+
}).catch((err) => {
99+
pushStatus.fail(err);
100+
return Promise.reject(err);
96101
});
97102
}
98103

src/pushStatusHandler.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { md5Hash, newObjectId } from './cryptoUtils';
2+
import { logger } from './logger';
23

34
export function flatten(array) {
45
return array.reduce((memo, element) => {
@@ -20,8 +21,9 @@ export default function pushStatusHandler(config) {
2021
return config.database.adaptiveCollection('_PushStatus');
2122
}
2223

23-
let setInitial = function(body, where, options = {source: 'rest'}) {
24+
let setInitial = function(body = {}, where, options = {source: 'rest'}) {
2425
let now = new Date();
26+
let data = body.data || {};
2527
let object = {
2628
objectId: newObjectId(),
2729
pushTime: now.toISOString(),
@@ -33,7 +35,7 @@ export default function pushStatusHandler(config) {
3335
expiry: body.expiration_time,
3436
status: "pending",
3537
numSent: 0,
36-
pushHash: md5Hash(JSON.stringify(body.data)),
38+
pushHash: md5Hash(JSON.stringify(data)),
3739
// lockdown!
3840
_wperm: [],
3941
_rperm: []
@@ -49,7 +51,8 @@ export default function pushStatusHandler(config) {
4951
return initialPromise;
5052
}
5153

52-
let setRunning = function() {
54+
let setRunning = function(installations) {
55+
logger.verbose('sending push to %d installations', installations.length);
5356
return initialPromise.then(() => {
5457
return collection();
5558
}).then((collection) => {
@@ -86,17 +89,31 @@ export default function pushStatusHandler(config) {
8689
return memo;
8790
}, update);
8891
}
89-
92+
logger.verbose('sent push! %d success, %d failures', update.numSent, update.numFailed);
9093
return initialPromise.then(() => {
9194
return collection();
9295
}).then((collection) => {
9396
return collection.updateOne({status:"running", objectId: pushStatus.objectId}, {$set: update});
9497
});
9598
}
9699

100+
let fail = function(err) {
101+
let update = {
102+
errorMessage: JSON.stringify(err),
103+
status: 'failed'
104+
}
105+
logger.error('error while sending push', err);
106+
return initialPromise.then(() => {
107+
return collection();
108+
}).then((collection) => {
109+
return collection.updateOne({objectId: pushStatus.objectId}, {$set: update});
110+
});
111+
}
112+
97113
return Object.freeze({
98114
setInitial,
99115
setRunning,
100-
complete
116+
complete,
117+
fail
101118
})
102119
}

0 commit comments

Comments
 (0)