Skip to content

Commit

Permalink
fix: Parse.Query.subscribe() does not return a rejected promise on …
Browse files Browse the repository at this point in the history
…error in Cloud Code Triggers `beforeConnect` or `beforeSubscribe` (parse-community#1490)

BREAKING CHANGE: Calling `Parse.Query.subscribe()` will now return a rejected promise if an error is thrown in Cloud Code Triggers `beforeConnect` or `beforeSubscribe`; in previous releases a resolved promise was returned, even if subscribing failed and it was necessary to create an `error.on` listener to handle these errors (parse-community#1490)
  • Loading branch information
dblythy authored and mtrezza committed Jan 22, 2023
1 parent 7e106a1 commit 74e5524
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
52 changes: 52 additions & 0 deletions integration/test/ParseLiveQueryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,56 @@ describe('Parse LiveQuery', () => {
object.set({ foo: 'bar' });
await object.save();
});

it('live query can handle beforeConnect and beforeSubscribe errors', async () => {
await reconfigureServer({
cloud({ Cloud }) {
Cloud.beforeSubscribe('TestError', () => {
throw 'not allowed to subscribe';
});
},
});
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');
const subscription = client.subscribe(query);
await expectAsync(subscription.subscribePromise).toBeRejectedWith(
new Parse.Error(141, 'not allowed to subscribe')
);
client.close();
});

it('connectPromise does throw', async () => {
await reconfigureServer({
cloud({ Cloud }) {
Cloud.beforeConnect((params) => {
if (params.sessionToken === 'testToken') {
throw 'not allowed to connect';
}
});
},
});
const client = new Parse.LiveQueryClient({
applicationId: 'integration',
serverURL: 'ws://localhost:1337',
javascriptKey: null,
masterKey: null,
sessionToken: 'testToken',
installationId: null,
});
client.open();
const query = new Parse.Query('TestError');
const subscription = client.subscribe(query);
await expectAsync(subscription.subscribePromise).toBeRejectedWith(
new Parse.Error(141, 'not allowed to connect')
);
client.close();
});
});
21 changes: 16 additions & 5 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 @@ -217,9 +218,13 @@ class LiveQueryClient extends EventEmitter {
const subscription = new LiveQuerySubscription(this.requestId, query, sessionToken);
this.subscriptions.set(this.requestId, subscription);
this.requestId += 1;
this.connectPromise.then(() => {
this.socket.send(JSON.stringify(subscribeRequest));
});
this.connectPromise
.then(() => {
this.socket.send(JSON.stringify(subscribeRequest));
})
.catch(error => {
subscription.subscribePromise.reject(error);
});

return subscription;
}
Expand Down Expand Up @@ -382,10 +387,15 @@ 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.connectPromise.reject(parseError);
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 +408,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 74e5524

Please sign in to comment.