Skip to content

Commit ef2e54c

Browse files
authored
LiveQueryEvent Error Logging Improvements (#6951)
* LiveQueryEvent Improvements * Update ParseLiveQueryServer.js * Update ParseLiveQueryServer.js * More Tests * Update ParseLiveQueryServer.js * Pass thrown errors to subscription * Update ParseLiveQueryServer.js * Update ParseLiveQueryServer.js * Remove ACL error
1 parent c647c53 commit ef2e54c

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

spec/ParseLiveQuery.spec.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ describe('ParseLiveQuery', function () {
231231
object.set({ foo: 'bar' });
232232
await object.save();
233233
});
234-
235234
it('can handle afterEvent throw', async done => {
236235
await reconfigureServer({
237236
liveQuery: {
@@ -245,6 +244,37 @@ describe('ParseLiveQuery', function () {
245244
const object = new TestObject();
246245
await object.save();
247246

247+
Parse.Cloud.afterLiveQueryEvent('TestObject', () => {
248+
throw 'Throw error from LQ afterEvent.';
249+
});
250+
251+
const query = new Parse.Query(TestObject);
252+
query.equalTo('objectId', object.id);
253+
const subscription = await query.subscribe();
254+
subscription.on('update', () => {
255+
fail('update should not have been called.');
256+
});
257+
subscription.on('error', e => {
258+
expect(e).toBe('Throw error from LQ afterEvent.');
259+
done();
260+
});
261+
object.set({ foo: 'bar' });
262+
await object.save();
263+
});
264+
265+
it('can handle afterEvent sendEvent to false', async done => {
266+
await reconfigureServer({
267+
liveQuery: {
268+
classNames: ['TestObject'],
269+
},
270+
startLiveQueryServer: true,
271+
verbose: false,
272+
silent: true,
273+
});
274+
275+
const object = new TestObject();
276+
await object.save();
277+
248278
Parse.Cloud.afterLiveQueryEvent('TestObject', req => {
249279
const current = req.object;
250280
const original = req.original;
@@ -254,7 +284,7 @@ describe('ParseLiveQuery', function () {
254284
}, 2000);
255285

256286
if (current.get('foo') != original.get('foo')) {
257-
throw "Don't pass an update trigger, or message";
287+
req.sendEvent = false;
258288
}
259289
});
260290

src/LiveQuery/ParseLiveQueryServer.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,33 @@ class ParseLiveQueryServer {
182182
clients: this.clients.size,
183183
subscriptions: this.subscriptions.size,
184184
useMasterKey: client.hasMasterKey,
185-
installationId: client.installationId
185+
installationId: client.installationId,
186+
sendEvent: true,
186187
};
187188
return maybeRunAfterEventTrigger('afterEvent', className, res);
188189
})
189190
.then(() => {
191+
if (!res.sendEvent) {
192+
return;
193+
}
190194
if (res.object && typeof res.object.toJSON === 'function') {
191195
deletedParseObject = res.object.toJSON();
192196
deletedParseObject.className = className;
193197
}
194198
client.pushDelete(requestId, deletedParseObject);
195199
})
196200
.catch(error => {
197-
logger.error('Matching ACL error : ', error);
201+
Client.pushError(
202+
client.parseWebSocket,
203+
error.code || 141,
204+
error.message || error,
205+
false,
206+
requestId
207+
);
208+
logger.error(
209+
`Failed running afterLiveQueryEvent on class ${className} for event ${res.event} with session ${res.sessionToken} with:\n Error: ` +
210+
JSON.stringify(error)
211+
);
198212
});
199213
}
200214
}
@@ -297,7 +311,6 @@ class ParseLiveQueryServer {
297311
isCurrentMatched,
298312
subscription.hash
299313
);
300-
301314
// Decide event type
302315
let type;
303316
if (isOriginalMatched && isCurrentMatched) {
@@ -322,12 +335,16 @@ class ParseLiveQueryServer {
322335
clients: this.clients.size,
323336
subscriptions: this.subscriptions.size,
324337
useMasterKey: client.hasMasterKey,
325-
installationId: client.installationId
338+
installationId: client.installationId,
339+
sendEvent: true,
326340
};
327341
return maybeRunAfterEventTrigger('afterEvent', className, res);
328342
})
329343
.then(
330344
() => {
345+
if (!res.sendEvent) {
346+
return;
347+
}
331348
if (res.object && typeof res.object.toJSON === 'function') {
332349
currentParseObject = res.object.toJSON();
333350
currentParseObject.className =
@@ -349,7 +366,17 @@ class ParseLiveQueryServer {
349366
}
350367
},
351368
error => {
352-
logger.error('Matching ACL error : ', error);
369+
Client.pushError(
370+
client.parseWebSocket,
371+
error.code || 141,
372+
error.message || error,
373+
false,
374+
requestId
375+
);
376+
logger.error(
377+
`Failed running afterLiveQueryEvent on class ${className} for event ${res.event} with session ${res.sessionToken} with:\n Error: ` +
378+
JSON.stringify(error)
379+
);
353380
}
354381
);
355382
}
@@ -658,7 +685,7 @@ class ParseLiveQueryServer {
658685
} catch (error) {
659686
Client.pushError(
660687
parseWebsocket,
661-
error.code || 101,
688+
error.code || 141,
662689
error.message || error,
663690
false
664691
);
@@ -776,7 +803,7 @@ class ParseLiveQueryServer {
776803
} catch (e) {
777804
Client.pushError(
778805
parseWebsocket,
779-
e.code || 101,
806+
e.code || 141,
780807
e.message || e,
781808
false,
782809
request.requestId

src/cloud-code/Parse.Cloud.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ module.exports = ParseCloud;
600600
* @property {Parse.Object} original If set, the object, as currently stored.
601601
* @property {Integer} clients The number of clients connected.
602602
* @property {Integer} subscriptions The number of subscriptions connected.
603+
* @property {Boolean} sendEvent If the LiveQuery event should be sent to the client. Set to false to prevent LiveQuery from pushing to the client.
603604
*/
604605

605606
/**

0 commit comments

Comments
 (0)