Hi guys. I'm wondering how to throw exception from...
# kotlin-native
s
Hi guys. I'm wondering how to throw exception from suspending function in Swift using Kotlin 1.4-M3. Here's the setup: I have a Kotlin interface like this:
Copy code
interface Foo {
    @Throws(CancellationException::class, MyException::class)
    suspend fun bar(param: Int): String
}
This interface gets translated thusly in Swift:
Copy code
public protocol Foo {
    /**
     @note This method converts instances of CancellationException, MyException to errors.
     Other uncaught Kotlin exceptions are fatal.
    */
    func bar(param: Int32, completionHandler: @escaping (String?, Error?) -> Void)
}
I can (and do) implement this interface in Swift, creating a class with coroutine functions that I can use in Kotlin (btw, blown away by the fact that it actually works, amazing job!). What I don't see, however, is how can the
bar
function use the
completionHandler
with a
MyException
. In other words, I need to express a async failure in Kotlin. In other other words, the bar function may fail asynchronously, and I need the coroutine (not the callback) to throw a
MyException
:
Copy code
class SwiftFoo : Foo {
    func bar(param: Int32, completionHandler: @escaping (TCPSocket?, Error?) -> Void) {
        someSwiftAsyncApi(param) { result in
            if let result = result { completionHandler(result, nil) }
            else { completionHandler(nil, MyException("someSwiftAsyncApi failed!")) } // TYPE ERROR
        }
    }
}
This does not compile because
MyException
is not a Swift
Error
. Any way to do this?