Skip to content

Commit b5ec040

Browse files
authored
fix: instead of letting the app crash, handle the exception properly in React Native. (#104)
* fix: instead of letting the app crash, handle the exception properly in React Native. * fix: types in example
1 parent d7b40eb commit b5ec040

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

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

+15-11
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,22 @@ class PusherWebsocketReactNativeModule(reactContext: ReactApplicationContext) :
9393

9494
@ReactMethod
9595
fun subscribe(channelName: String, promise: Promise) {
96-
val channel = when {
97-
channelName.startsWith("private-encrypted-") -> pusher!!.subscribePrivateEncrypted(
98-
channelName, this
99-
)
100-
channelName.startsWith("private-") -> pusher!!.subscribePrivate(channelName, this)
101-
channelName.startsWith("presence-") -> pusher!!.subscribePresence(
102-
channelName, this
103-
)
104-
else -> pusher!!.subscribe(channelName, this)
96+
try {
97+
val channel = when {
98+
channelName.startsWith("private-encrypted-") -> pusher!!.subscribePrivateEncrypted(
99+
channelName, this
100+
)
101+
channelName.startsWith("private-") -> pusher!!.subscribePrivate(channelName, this)
102+
channelName.startsWith("presence-") -> pusher!!.subscribePresence(
103+
channelName, this
104+
)
105+
else -> pusher!!.subscribe(channelName, this)
106+
}
107+
channel.bindGlobal(this)
108+
promise.resolve(null)
109+
} catch (e: Exception) {
110+
promise.reject("Error", "Failed to subscribe to channel: $channelName", e)
105111
}
106-
channel.bindGlobal(this)
107-
promise.resolve(null)
108112
}
109113

110114
@ReactMethod

0 commit comments

Comments
 (0)