Skip to content

Commit 8bb6d12

Browse files
committed
Return error via SubscriptionListener when presence events are malformed
1 parent 93c25bc commit 8bb6d12

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-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());

0 commit comments

Comments
 (0)