Skip to content

Commit 957dd71

Browse files
committed
Improving error handling on trigger function
1 parent c49a646 commit 957dd71

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

android/src/main/java/com/pusherwebsocketreactnative/PusherWebsocketReactNativeModule.kt

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,13 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
106106
@ReactMethod
107107
fun subscribe(channelName: String, promise: Promise) {
108108
val channel = when {
109-
channelName.startsWith("private-encrypted-") -> pusher!!.subscribePrivateEncrypted(channelName, this)
109+
channelName.startsWith("private-encrypted-") -> pusher!!.subscribePrivateEncrypted(
110+
channelName, this
111+
)
110112
channelName.startsWith("private-") -> pusher!!.subscribePrivate(channelName, this)
111-
channelName.startsWith("presence-") -> pusher!!.subscribePresence(channelName, this)
113+
channelName.startsWith("presence-") -> pusher!!.subscribePresence(
114+
channelName, this
115+
)
112116
else -> pusher!!.subscribe(channelName, this)
113117
}
114118
channel.bindGlobal(this)
@@ -123,14 +127,18 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
123127

124128
@ReactMethod
125129
fun trigger(channelName: String, eventName: String, data: String, promise: Promise) {
126-
when {
127-
channelName.startsWith("private-encrypted-") -> throw Exception("It's not currently possible to send a message using private encrypted channels.")
128-
channelName.startsWith("private-") -> pusher!!.getPrivateChannel(channelName).trigger(eventName, data)
129-
channelName.startsWith("presence-") -> pusher!!.getPresenceChannel(channelName)
130-
.trigger(eventName, data)
131-
else -> throw Exception("Messages can only be sent to private and presence channels.")
130+
try {
131+
when {
132+
channelName.startsWith("private-encrypted-") -> throw Exception("It's not currently possible to send a message using private encrypted channels.")
133+
channelName.startsWith("private-") -> pusher!!.getPrivateChannel(channelName).trigger(eventName, data)
134+
channelName.startsWith("presence-") -> pusher!!.getPresenceChannel(channelName)
135+
.trigger(eventName, data)
136+
else -> throw Exception("Messages can only be sent to private and presence channels.")
137+
}
138+
promise.resolve(null)
139+
} catch (e: Exception) {
140+
promise.reject(e)
132141
}
133-
promise.resolve(null)
134142
}
135143

136144
@ReactMethod

0 commit comments

Comments
 (0)