Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Parse.Query.subscribe() does not return a rejected promise on error in Cloud Code Triggers beforeConnect or beforeSubscribe #1490

Merged
merged 22 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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