Hello. I am using Apollo to query the server. I wa...
# multiplatform
a
Hello. I am using Apollo to query the server. I want to check if I have an IOException returned and if so, i consider it to be a case where there's no internet. Problem is that i can't check IOException for iOS. I tried this, usinv expect-actual but i get an error in the github action when building the library for iOS
Copy code
actual fun isNetworkError(throwable: Throwable): Boolean {
    return (throwable as? NSError)?.domain == NSURLErrorDomain &&
            (throwable.code == NSURLErrorNotConnectedToInternet ||
                    throwable.code == NSURLErrorNetworkConnectionLost)
}
Copy code
public val Char.code: Int defined in kotlin
e: file:///Users/runner/work/../shared/src/iosMain/kotlin/com/../shared/Platform.kt:93:31 Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
93
public val Char.code: Int defined in kotlin
i'm guessing throwable.code is a Char instead of Int ?! I got the above from chatGPT, i tried to add throwable.code.toInt() but did not work. Got the same error I'd do it another way entirely, but I have no clue how ...
m
Maybe it's a smart cast thing?
Copy code
val cause = apolloNetworkException.platformCause
    // assertIs doesn't work with Obj-C classes so we rely on `check` instead
    // assertIs<NSError>(cause)
    check(cause is NSError)

    assertEquals(cause.domain, NSURLErrorDomain)
    assertEquals(cause.code, NSURLErrorCannotConnectToHost)
You'll have to check first that your exception is an instance of
ApolloNetworkException
and then that
exception.platformCause
is an instance of
NSError
and you shuold be ok
a
Copy code
CUCU :: userModel.throwable?.cause :: null
CUCU :: userModel.throwable :: com.apollographql.apollo3.exception.ApolloNetworkException: Failed to execute GraphQL http network request
CUCU :: (throwable as? NSError) :: null
CUCU :: throwable as? NSError)?.domain == NSURLErrorDomain :: false
CUCU :: (throwable as? NSError)?.code :: null
It seems the throwable is an ApolloNetworkException, but tehre's no cause ... it's null. either i'm doing something wrong or ... i have no idea...
m
It's
platformCause
Apologies, I've just edited above