Skip to content

[SDK-417] Fail gracefully when managers accessed before init#1073

Merged
franco-zalamena-iterable merged 2 commits into
masterfrom
feature/SDK-417-graceful-preinit
Jul 14, 2026
Merged

[SDK-417] Fail gracefully when managers accessed before init#1073
franco-zalamena-iterable merged 2 commits into
masterfrom
feature/SDK-417-graceful-preinit

Conversation

@franco-zalamena-iterable

@franco-zalamena-iterable franco-zalamena-iterable commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

🔹 Jira Ticket(s) if any

✏️ Description

Fix to prevent calls before initialization to crash the app. We now log an error and have a new way to get the Iterable instance or null on the spot.

getInAppManager() and getEmbeddedManager() threw a RuntimeException when
called before IterableApi.initialize(), crashing the host app for a call-
ordering mistake. They now log a warning and return a no-op manager (empty
lists, ignored commands) instead, matching the iOS SDK's null-object model.

The getters keep their concrete @nonnull return types, so no existing call
site or test needs to change. Two no-op subclasses (EmptyInAppManager,
EmptyEmbeddedManager) back the stub. New getInAppManagerOrNull() /
getEmbeddedManagerOrNull() return null pre-init for callers that want to
detect the uninitialized state explicitly.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@franco-zalamena-iterable franco-zalamena-iterable linked an issue Jul 10, 2026 that may be closed by this pull request
Pre-init errors from the embedded no-op stub were logged under the inherited
base tag "IterableEmbeddedManager" instead of a stub-specific tag, unlike
EmptyInAppManager which uses its own "EmptyInAppManager" tag. Add a LOG_TAG
constant so both stubs log under a findable, consistent name.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@joaodordio joaodordio requested a review from a team July 13, 2026 16:03

@joaodordio joaodordio left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix, this catches up nicely with the Swift SDK's EmptyInAppManager / EmptyEmbeddedManager pattern and also quietly resolves the process-death recovery crash in IterableInAppFragmentHTMLNotification where the fragment could re-hydrate before Application#onCreate re-initializes the SDK.

Approving, with a few small non blockers worth considering before merge:

  • Log noise: the empty stubs log an error on every method call, plus the getter also logs. In a render loop before init this gets loud fast. Consider logging once at the getter and either dropping the per-method logs or dropping them to IterableLogger.d.
  • Debug assertion parity with iOS: Swift pairs the empty return with assertionFailure, which crashes debug builds and no-ops in release. A BuildConfig.DEBUG guarded throw in getInAppManager() / getEmbeddedManager() would give devs the same fail-fast signal locally while keeping prod crash-safe. This is standard operation in iOS world, not sure about Android.
  • Post-init coverage: worth adding two tests that call IterableApi.initialize(...) and assert both getters return the real managers (not instanceof Empty*) and *OrNull return non-null. Locks in the wiring flip.
  • Failure-handler contract test: EmptyInAppManager.setRead(msg, read, success, failure) and removeMessage(..., success, failure) invoke failureHandler.onFailure("Iterable SDK is not initialized", null). That's an observable contract, cheap to lock down with a test.
  • Parent-class safety note: a short comment on IterableInAppManager and IterableEmbeddedManager saying "any new method must be overridden in the Empty* subclass or must not touch api / context / iterableApi" would help future contributors avoid landmines, especially with the new lateinit fields.
  • getMessages(placementId) parity: iOS returns [], Android's empty stub returns null (mirrors the pre-existing nullable signature). Not from this PR, but returning emptyList() here would soften it without touching the public type.
  • Migration guide nit: worth a one-liner that if anyone was catching RuntimeException from these getters to detect the uninitialized state, they should switch to the new *OrNull variants.

None of these are blockers, ship it whenever.

@franco-zalamena-iterable

Copy link
Copy Markdown
Contributor Author

I like the idea of crashing on debug and just no-op on production. But the problem is that the BuildConfig.Debug references the sdk BuildConfig from what i saw, so this would be just ignored in the final version

@franco-zalamena-iterable franco-zalamena-iterable merged commit 88441a2 into master Jul 14, 2026
7 checks passed
@franco-zalamena-iterable franco-zalamena-iterable deleted the feature/SDK-417-graceful-preinit branch July 14, 2026 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RunTime Exceptions are NOT documented

3 participants