hey could someone help me with courutine exception...
# ios
a
hey could someone help me with courutine exception casting and handling in swift. I can not manage to cast it in to any Kotlin exception I have kotlin code:
Copy code
@Throws(LoginException::class, CancellationException::class, RegistrationException::class)
suspend fun login(email: String, password: String): TSToken {
    throw RegistrationException("Registration exception", "Reg token")
}
and swift code:
Copy code
SharedDependencies.shared.sessionManager.login(
            email: email,
            password: password,
            completionHandler: { session, error in
               
              if (error is SessionRegistrationException){
                alertErrorText = "Registration exception"
                showingAlert = true
              } else if (error is KotlinException){
                alertErrorText = "Kotlin exception"
                showingAlert = true
              } else{
                alertErrorText = "Unknown exception"
                showingAlert = true
              }
            }
          )
And in debug point I see:
f
the error message “No silent login method” doesn’t match with your login method you display.
Are you sure it call correctly? did you debug your code with the IDE?
a
sorry message is random
yep calling it correctly
just screenshot is taken later
f
try debugging with https://github.com/touchlab/xcode-kotlin and see what really happened
or run debug with the IDE, it needs to be enabled jetbrains://AndroidStudio/settings?name=Advanced+Settings -> “Enable experimental Multiplatform...”
a
yep I did that, it is more casting problem, I managed to cast it in to KotlinException, but not in to RegistrationException. It also could be that I have only reference of SessionRegisrationException, but in logs it is missing word Shared
*if* (error *is* SessionRegistrationException)
And session is module of kmm project
f
try
if (let myError = error as? SessionRegistrationException)
a
this is what initially tried
f
but yes, I thinks it a issue about casting
a
Probably SessionRegistrationException vs RegistrationException, trying with different exception inside shared module so it would not have any prefix
f
Or change the way you manage your error management
a
yep might need to return result which has error type instead of different exception
f
use sealed class with Exception Inheritance
a
might also be issue with casting
d
In your Swift code, you need to do a little digging to get the RegistrationException out.
Copy code
let exception = error.userInfo["KotlinException"]

if exception is RegistrationException {
  // ...
}
👀 1
a
thank you, already refactored to use sealed class result not exceptions
f
@Darron Schall not really developer friendly 😄 I guess extending the KotlinException class on the swift side should be a better approach
a
I think my problem was that I was using multi module approach and there is limited visibility of child modules on generated code. Had also some problems with sealed classes casting because inner classes was not generated
ended up with creating unwrap fun
Copy code
@OptIn(ExperimentalObjCName::class)
@ObjCName("LoginResult", exact = true)
sealed class LoginResult {
    data class Success(val token: TSToken) : LoginResult()

    data class Registration(val message: String, val token: String) : LoginResult()

    data class Error(val message: String, val statusCode: Int) : LoginResult()

    val tokenSuccess: TSToken? get() = (this as? Success)?.token

    fun unwrap(
        onLoggedIn: (Success) -> Unit,
        onRegistration: (Registration) -> Unit,
        onError: (Error) -> Unit
    ) {
        return when (this) {
            is Success -> onLoggedIn(this)
            is Registration -> onRegistration(this)
            is Error -> onError(this)
        }
    }
}
and
Copy code
extension LoginResult {
  func handle(promise: Resolver<BaseError>) {
    self.unwrap(
      onLoggedIn: { result in

      },
      onRegistration: { result in

      },
      onError: { result in

      }
    )
  }
}
f
did you try to use SKIE enum capabilities ?
a
trying to not use third parties as much as possible
f
I see, just it an unavoidable tools
a
I have hope that Kotlin in futute improve this kinda stuff 🙂 For now if it does not require to much code I am good with not using third parties
f
For exemple, by default, coroutine are not cancellable on the swift side, SKIE (and the another one) fix that issue.
I also hope Kotlin will improve compatibility
but SKIE is not mandatory , just make dev life easier 😄
a
Oh I need more reading about coroutine are not cancellable on the swift 😄
f
two community approved library
a
thank you, definitely will have a look, because I am integrating stuff in to iOS prod app, cheers
🙌 1