In our most recent app release we see a handful kotlin.NoWhenBranchMatchedException
s reported to Fabric/Crashlytics.
This is the code snippet in question:
private lateinit var welcome: Welcome
// ...
welcome.welcomeStateLoginStatus.let {
val handled = when (it) {
UnknownUser -> {
btn_login.visibility = View.VISIBLE
btn_logout.visibility = View.GONE
secondButtonFocusedInfoText = getString(R.string.welcome_login_button_info)
tv_user_description.text = null
}
is InternalUser -> {
btn_login.visibility = View.GONE
btn_logout.visibility = View.VISIBLE
secondButtonFocusedInfoText = "Logout"
tv_user_description.text = "Logged in as internal user"
}
ExternalUser -> {
btn_login.visibility = View.GONE
btn_logout.visibility = View.VISIBLE
secondButtonFocusedInfoText = "Logout"
tv_user_description.text = "Logged in as external user"
}
}
}
And the class definitions:
data class Welcome(val welcomeStateLoginStatus: WelcomeStateLoginStatus, val userCanBuySubscription: UserCanBuySubscription? = null) : WelcomeState()
sealed class WelcomeStateLoginStatus() : Serializable
object UnknownUser : WelcomeStateLoginStatus()
data class InternalUser(var user: User) : WelcomeStateLoginStatus()
object ExternalUser : WelcomeStateLoginStatus()
I am puzzled as to how this code can even theoretically throw that exception - as you can see we even introduced the handled
value just to force the compiler to make sure that all cases are handled...