swiftrustconcurrencytry-catchffi

Swift catch with case let


I'm trying to bridge a Rust-based networking layer written with uniffi-rs to my Swift-based Xcode project. It has some weird type of system now, but I have this until I figure out something else.

I need to catch an enum error type with a single case and some associated values. Is this possible at all?

do {
  try await throwingRustOperation()

} catch let error as RustBasedPackage.SessionError where case let .Error(statusCode, error) = error { // Doesn't work.
  if case let .Error(statusCode, error) = error { // Works.
    pushResetPasswordLaunchEmailAppViewController(email: email)
  } else {
    presentGenericAlert()
  }
}

This is the enum error types:

public enum SessionError {

    
    
    case Error(statusCode: UInt16, error: ErrorDetail
    )
}

public struct ErrorDetail {
    public var message: String
    public var errorCode: String
    public var code: ErrorResponseCode

    // Default memberwise initializers are never public by default, so we
    // declare one manually.
    public init(message: String, errorCode: String, code: ErrorResponseCode) {
        self.message = message
        self.errorCode = errorCode
        self.code = code
    }
}

Solution

  • Thanks to @sweeper, this is what I was looking for:

        do {
          try await throwingRustOperation()
        } catch let RustBasedPackage.SessionError.Error(_, errorDetail) where errorDetail.code == .CommunicationError {
          pushResetPasswordLaunchEmailAppViewController(email: email)
        } catch {
          presentGenericAlert()
        }