fix: prevent NoSuchElementException in session chain selection#212
fix: prevent NoSuchElementException in session chain selection#212developerfred wants to merge 1 commit intoreown-com:developfrom
Conversation
|
Thank you for your contribution! We ask that you please read and sign our CTA Document before we can accept your contribution. You can sign the CTA simply by posting a Pull Request Comment with the following text: I have read the CTA Document and I hereby sign the CTA You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot. |
|
|
I have read the CTA Document and I hereby sign the CTA |
|
recheck |
There was a problem hiding this comment.
Review: Request Changes
Thanks for the contribution and for identifying a real crash — the NoSuchElementException from first() on an empty AppKit.chains is a valid bug. However, this PR has several issues that need to be addressed before it can be merged.
P0 — Won't Compile
1. Broken braces/indentation in AppKitDelegate.kt
The onSessionApproved method has its indentation shifted and introduces an extra closing } that would prematurely close the AppKitDelegate object class, leaving onSessionAuthenticateResponse and all subsequent methods outside the class body:
override fun onSessionApproved(approvedSession: Modal.Model.ApprovedSession) {
scope.launch { // ← wrong indent (4sp instead of 8sp)
val chain = ...
...
}
} // ← extra brace closes the object class2. Missing import android.util.Log
Log.e(...) is used but the import is never added.
3. Nullable return type breaks all other callers
Changing getSelectedChain from ?: first() to ?: firstOrNull() changes the return type from Modal.Model.Chain to Modal.Model.Chain?. Only onSessionApproved is updated, but the same function is called in several other places that still assume a non-null return:
AppKitDelegate.onSessionAuthenticateResponse— accesseschain.idwithout null checkAppKitDelegate.onSessionUpdate— passes chain totoSession()without null checkAppKitEngine.connectCoinbase— passes chain totoSession()without null checkAppKitState.mapToAccountButtonState— accessesselectedChain.chainImage,.chainNameetc. without null check
P1 — Behavioral Issues
4. Silent session event drop
When chain is null in onSessionApproved, the event emission (_wcEventModels.emit(approvedSession)) is skipped. Downstream listeners (UI, state management) will never learn the session was approved, which could cause the modal to hang or appear broken. Silently swallowing events can be harder to debug than the original crash.
5. getChain() still throws
The change from AppKit.chains.first() to AppKit.chains.firstOrNull() ?: throw IllegalStateException(...) just swaps one exception type for another — not a meaningful improvement.
P2 — Unnecessary Changes
6. getDefaultChain() refactoring is a no-op
The original code already returns emptyList() when there are no valid chains, since mapNotNull on an empty list returns an empty list. The added if (chainIds.isNotEmpty()) check is redundant.
Suggestions for a revised approach
- Keep
getSelectedChainnon-nullable and guard at specific call sites, OR make it nullable and update all callers consistently — not just one. - Don't silently drop session events — either always emit the event, or propagate the error through
onError()so the UI can react. - Investigate the root cause: why is
AppKit.chainsempty duringonSessionApproved? This may indicate an initialization ordering issue that should be fixed upstream rather than papered over with null checks at every call site. - Maintain the existing 4-space indentation consistent with the rest of the file.
@developerfred Please address the feedback, or let me know so I can push it



close #211