@@ -52,6 +52,9 @@ internal class OneSignalImp(
5252 @Volatile
5353 private var initState: InitState = InitState .NOT_STARTED
5454
55+ // Save the exception pointing to the caller that triggered init, not the async worker thread.
56+ private var initFailureException: Exception ? = null
57+
5558 override val sdkVersion: String = OneSignalUtils .sdkVersion
5659
5760 override val isInitialized: Boolean
@@ -279,7 +282,11 @@ internal class OneSignalImp(
279282 val startupService = bootstrapServices()
280283 val result = resolveAppId(appId, configModel, preferencesService)
281284 if (result.failed) {
282- Logging .warn(" suspendInitInternal: no appId provided or found in legacy config." )
285+ val message = " suspendInitInternal: no appId provided or found in local storage. Please pass a valid appId to initWithContext()."
286+ val exception = IllegalStateException (message)
287+ // attach the real crash cause to the init failure exception that will be throw shortly after
288+ initFailureException?.addSuppressed(exception)
289+ Logging .warn(message)
283290 initState = InitState .FAILED
284291 notifyInitComplete()
285292 return false
@@ -395,12 +402,12 @@ internal class OneSignalImp(
395402
396403 // Re-check state after waiting - init might have failed during the wait
397404 if (initState == InitState .FAILED ) {
398- throw IllegalStateException (" Initialization failed. Cannot proceed." )
405+ throw initFailureException ? : IllegalStateException (" Initialization failed. Cannot proceed." )
399406 }
400407 // initState is guaranteed to be SUCCESS here - consistent state
401408 }
402409 InitState .FAILED -> {
403- throw IllegalStateException (" Initialization failed. Cannot proceed." )
410+ throw initFailureException ? : IllegalStateException (" Initialization failed. Cannot proceed." )
404411 }
405412 else -> {
406413 // SUCCESS - already initialized, no need to wait
@@ -506,6 +513,9 @@ internal class OneSignalImp(
506513 ): Boolean {
507514 Logging .log(LogLevel .DEBUG , " initWithContext(context: $context , appId: $appId )" )
508515
516+ // This ensures the stack trace points to the caller that triggered init, not the async worker thread.
517+ initFailureException = IllegalStateException (" OneSignal initWithContext failed." )
518+
509519 // Use IO dispatcher for initialization to prevent ANRs and optimize for I/O operations
510520 return withContext(ioDispatcher) {
511521 // do not do this again if already initialized or init is in progress
0 commit comments