Skip to content

Commit e98b3fa

Browse files
fix(demo): propagate CancellationException through outer catches
The previous commit added `catch (e: CancellationException) { throw e }` inside `postNotification`, but the re-thrown cancellation was caught again one frame up by `sendNotification` / `sendCustomNotification`'s outer `catch (e: Exception)`, silently undoing the fix. The same anti-pattern lived in `OneSignalService.fetchUser` (wrapping `withContext(Dispatchers.IO)` + `connection.responseCode`) and `MainViewModel.fetchUserDataFromApi` (wrapping `repository.fetchUser`, the inner `withContext(Dispatchers.Main)`, and `delay(100)`). Add a `CancellationException` re-throw clause ahead of each generic `Exception` catch so structured-concurrency cancellation propagates all the way up to the cancelled scope instead of being swallowed as a misleading I/O error. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent a813284 commit e98b3fa

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

examples/demo/app/src/main/java/com/onesignal/example/data/network/OneSignalService.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ object OneSignalService {
7676
}
7777

7878
return@withContext postNotification(notificationJson, "notification")
79+
} catch (e: CancellationException) {
80+
throw e
7981
} catch (e: Exception) {
8082
Log.e(TAG, "Error sending notification", e)
8183
return@withContext false
@@ -110,6 +112,8 @@ object OneSignalService {
110112
}
111113

112114
return@withContext postNotification(notificationJson, "custom notification")
115+
} catch (e: CancellationException) {
116+
throw e
113117
} catch (e: Exception) {
114118
Log.e(TAG, "Error sending custom notification", e)
115119
return@withContext false
@@ -256,6 +260,8 @@ object OneSignalService {
256260
Log.e(TAG, "Failed to fetch user (HTTP $responseCode): $errorResponse")
257261
return@withContext null
258262
}
263+
} catch (e: CancellationException) {
264+
throw e
259265
} catch (e: Exception) {
260266
Log.e(TAG, "Error fetching user", e)
261267
return@withContext null

examples/demo/app/src/main/java/com/onesignal/example/ui/main/MainViewModel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import com.onesignal.user.state.IUserStateObserver
1717
import com.onesignal.user.state.UserChangedState
1818
import com.onesignal.user.subscriptions.IPushSubscriptionObserver
1919
import com.onesignal.user.subscriptions.PushSubscriptionChangedState
20+
import kotlinx.coroutines.CancellationException
2021
import kotlinx.coroutines.Dispatchers
2122
import kotlinx.coroutines.channels.Channel
2223
import kotlinx.coroutines.flow.Flow
@@ -234,6 +235,8 @@ class MainViewModel(application: Application) : AndroidViewModel(application), I
234235
}
235236
_isLoading.value = false
236237
}
238+
} catch (e: CancellationException) {
239+
throw e
237240
} catch (e: Exception) {
238241
android.util.Log.e("MainViewModel", "Error fetching user data", e)
239242
withContext(Dispatchers.Main) {

0 commit comments

Comments
 (0)