Skip to content

Commit

Permalink
fix: allow Parse.Cloud.beforeSubscribe rejections to be caught by q…
Browse files Browse the repository at this point in the history
…uery.subscribe
  • Loading branch information
dblythy committed Jun 5, 2022
1 parent ab5be44 commit 3073b77
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
27 changes: 27 additions & 0 deletions integration/test/ParseLiveQueryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,31 @@ describe('Parse LiveQuery', () => {
object.set({ foo: 'bar' });
await object.save();
});

it('live query can handle beforeConnect and beforeSubscribe errors', async () => {
const client = new Parse.LiveQueryClient({
applicationId: 'integration',
serverURL: 'ws://localhost:1337',
javascriptKey: null,
masterKey: null,
sessionToken: null,
installationId: null,
});
client.open();
const query = new Parse.Query('TestError');
let subscription = client.subscribe(query);
await expectAsync(subscription.subscribePromise).toBeRejectedWith(
new Parse.Error(141, 'not allowed to subscribe')
);
client.close();
await reconfigureServer({
cloud: `${__dirname}/cloud/before_connect.js`,
});
client.open();
subscription = client.subscribe(query);
await expectAsync(subscription.subscribePromise).toBeRejectedWith(
new Parse.Error(141, 'not allowed to connect')
);
client.close();
});
});
3 changes: 3 additions & 0 deletions integration/test/cloud/before_connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Parse.Cloud.beforeConnect(() => {
throw 'not allowed to connect';
});
4 changes: 4 additions & 0 deletions integration/test/cloud/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ Parse.Cloud.job('CloudJob2', function () {
Parse.Cloud.job('CloudJobFailing', function () {
throw 'cloud job failed';
});

Parse.Cloud.beforeSubscribe('TestError', () => {
throw 'not allowed to subscribe';
});
16 changes: 14 additions & 2 deletions src/LiveQueryClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import EventEmitter from './EventEmitter';
import ParseObject from './ParseObject';
import LiveQuerySubscription from './LiveQuerySubscription';
import { resolvingPromise } from './promiseUtils';
import ParseError from './ParseError';

// The LiveQuery client inner state
const CLIENT_STATE = {
Expand Down Expand Up @@ -218,6 +219,10 @@ class LiveQueryClient extends EventEmitter {
this.subscriptions.set(this.requestId, subscription);
this.requestId += 1;
this.connectPromise.then(() => {
if (this.connectError) {
subscription.subscribePromise.reject(this.connectError);
return;
}
this.socket.send(JSON.stringify(subscribeRequest));
});

Expand Down Expand Up @@ -382,10 +387,16 @@ class LiveQueryClient extends EventEmitter {
setTimeout(() => subscription.emit(SUBSCRIPTION_EMMITER_TYPES.OPEN, response), 200);
}
break;
case OP_EVENTS.ERROR:
case OP_EVENTS.ERROR: {
const parseError = new ParseError(data.code, data.error);
if (!this.id) {
this.connectError = parseError;
this.connectPromise.resolve();
this.state = CLIENT_STATE.DISCONNECTED;
}
if (data.requestId) {
if (subscription) {
subscription.subscribePromise.resolve();
subscription.subscribePromise.reject(parseError);
setTimeout(() => subscription.emit(SUBSCRIPTION_EMMITER_TYPES.ERROR, data.error), 200);
}
} else {
Expand All @@ -398,6 +409,7 @@ class LiveQueryClient extends EventEmitter {
this._handleReconnect();
}
break;
}
case OP_EVENTS.UNSUBSCRIBED:
// We have already deleted subscription in unsubscribe(), do nothing here
break;
Expand Down

0 comments on commit 3073b77

Please sign in to comment.