Skip to content

Commit 49ce2d9

Browse files
Merge pull request #290 from pusher/add-error-when-presence-data-missing
Return error via SubscriptionListener when presence events are malformed
2 parents 4030c80 + 8816840 commit 49ce2d9

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/main/java/com/pusher/client/channel/SubscriptionEventListener.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,18 @@ public interface SubscriptionEventListener {
3333
* See {@linkplain PusherEvent} for more.
3434
*/
3535
void onEvent(final PusherEvent event);
36+
37+
/**
38+
* Callback that is fired whenever an unexpected error occurs processing
39+
* for this {@linkplain SubscriptionEventListener}.
40+
*
41+
* @param message
42+
* A description of the problem.
43+
* @param e
44+
* An associated exception, if available.
45+
*/
46+
default void onError(String message, Exception e) {
47+
// No-op
48+
return;
49+
};
3650
}

src/main/java/com/pusher/client/channel/impl/PresenceChannelImpl.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,20 @@ public String toString() {
9595

9696
@SuppressWarnings({ "rawtypes", "unchecked" })
9797
private void handleSubscriptionSuccessfulMessage(final String message) {
98+
final ChannelEventListener listener = getEventListener();
9899

99100
// extract data from the JSON message
100101
final PresenceData presenceData = extractPresenceDataFrom(message);
102+
if (presenceData == null) {
103+
if (listener != null) {
104+
listener.onError(
105+
"Subscription failed: Presence data not found",
106+
null
107+
);
108+
}
109+
return;
110+
}
111+
101112
final List<String> ids = presenceData.ids;
102113
final Map<String, Object> hash = presenceData.hash;
103114

@@ -109,7 +120,7 @@ private void handleSubscriptionSuccessfulMessage(final String message) {
109120
idToUserMap.put(id, user);
110121
}
111122
}
112-
final ChannelEventListener listener = getEventListener();
123+
113124
if (listener != null) {
114125
final PresenceChannelEventListener presenceListener = (PresenceChannelEventListener)listener;
115126
presenceListener.onUsersInformationReceived(getName(), getUsers());

src/test/java/com/pusher/client/channel/impl/PresenceChannelImplTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class PresenceChannelImplTest extends PrivateChannelImplTest {
3030
private static final String AUTH_RESPONSE = "\"auth\":\"a87fe72c6f36272aa4b1:f9db294eae7\",\"channel_data\":\"{\\\"user_id\\\":\\\"5116a4519575b\\\",\\\"user_info\\\":{\\\"name\\\":\\\"Phil Leggetter\\\",\\\"twitter_id\\\":\\\"@leggetter\\\"}}\"";
3131
private static final String AUTH_RESPONSE_NUMERIC_ID = "\"auth\":\"a87fe72c6f36272aa4b1:f9db294eae7\",\"channel_data\":\"{\\\"user_id\\\":51169,\\\"user_info\\\":{\\\"name\\\":\\\"Phil Leggetter\\\",\\\"twitter_id\\\":\\\"@leggetter\\\"}}\"";
3232
private static final String USER_ID = "5116a4519575b";
33+
private static final String ERROR_NO_PRESENCE_DATA = "Subscription failed: Presence data not found";
3334

3435
@Mock
3536
private PresenceChannelEventListener mockEventListener;
@@ -77,6 +78,17 @@ public void testIsSubscribedMethod(){
7778
assertTrue(channel.isSubscribed());
7879
}
7980

81+
@Test
82+
public void testInternalSubscriptionSucceededMessageWithNoPresenceDataReturnsError(){
83+
final String eventName = "pusher_internal:subscription_succeeded";
84+
final Map<String, Object> data = new LinkedHashMap<String, Object>();
85+
86+
channel.onMessage(eventName, eventJson(eventName, data, getChannelName()));
87+
88+
final InOrder inOrder = inOrder(mockEventListener);
89+
inOrder.verify(mockEventListener).onError(eq(ERROR_NO_PRESENCE_DATA), eq(null));
90+
}
91+
8092
@Test
8193
@Override
8294
@SuppressWarnings({ "rawtypes", "unchecked" })

0 commit comments

Comments
 (0)