Hello, what is the best practice for error handlin...
# multiplatform
j
Hello, what is the best practice for error handling for Kotlin errors thrown in KMP shared code to iOS? Right now, basic errors in shared code don't show full stack trace or inner errors. Do I have to handle all KMP errors on Kotlin side then return custom Error result type which I have explicitly defined so iOS can read the error from there? Looks like bridging of Kotlin Error/Exception to Swift is not really well thought out right now. I was hoping to define custom Errors in Kotlin and just throw them, having Swift code catch it and be able to determine exact error/ root cause from that but looks like i can't do that.
e
as the docs say, you need @Throws to propagate Kotlin exceptionsto ObjC/Swift
j
@ephemient I already do that, but honestly that might not be the best route for error handling. I think you should have @throws for unexpected error, but in general don't throw a handled exception. It seems its best to catch any exception in the KMP code and return a result type of error to the caller which contains all the error information you can collect from Kotlin, including stack trace. If you just throw the exception to Native, like I mentioned, the entire stack trace is lost and the inner exception chain is lost when it converts to an NSError.
Actually after further investigation, I am able to get all the inner exceptions (cause) and a full stack trace printed out with just throwing exceptions. Will pursue this route for now to handle Kotlin exceptions on iOS side.
👍 1
👀 1