open-meteo / open-meteo-api-kotlin

A Kotlin library for the Open-Meteo.com APIs.
MIT License
24 stars 2 forks source link

Crash with no network connection, or server down #4

Closed dzungpv closed 1 year ago

dzungpv commented 1 year ago

Describe the bug When I call forecast API data, if server down or no internet, app will crash.

To Reproduce

        val query = Forecast.Query(
            latitude = location!!.latitude.toFloat(),
            longitude = location.longitude.toFloat(),
            daily = Forecast.Daily { listOf(
                weathercode,temperature2mMax, temperature2mMin
            ) },
            hourly = Forecast.Hourly { listOf(
                temperature2m
            ) },
            currentWeather = true,
            timezone = Timezone.auto
        )
        //Forecast(query).getOrThrow().run {
        Forecast.invoke(query, URL(Config.WEATHER_API_URL))

Logs

Fatal Exception: java.net.UnknownHostException: Unable to resolve host "api.server.com": No address associated with hostname
       at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:156)
       at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
       at java.net.InetAddress.getAllByName(InetAddress.java:1152)
       at com.android.okhttp.Dns$1.lookup(Dns.java:41)
       at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178)
       at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:144)
       at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:86)
       at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:176)
       at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
       at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
       at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
       at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
       at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
       at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
       at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
       at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
       at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
       at com.openmeteo.api.common.http.Endpoint.response(Endpoint.kt:65)
       at com.openmeteo.api.common.http.Endpoint.get(Endpoint.kt:76)
       at com.openmeteo.api.Forecast.invoke-gIAlu-s(Forecast.kt:361)
Caused by android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
       at libcore.io.Linux.android_getaddrinfo(Linux.java)
       at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:133)
       at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:222)
       at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:133)
       at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135)
       at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
       at java.net.InetAddress.getAllByName(InetAddress.java:1152)
       at com.android.okhttp.Dns$1.lookup(Dns.java:41)
       at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178)
       at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:144)
       at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:86)
       at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:176)
       at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
       at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
       at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
       at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
       at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
       at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
       at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
       at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
       at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
       at com.openmeteo.api.common.http.Endpoint.response(Endpoint.kt:65)
       at com.openmeteo.api.common.http.Endpoint.get(Endpoint.kt:76)
       at com.openmeteo.api.Forecast.invoke-gIAlu-s(Forecast.kt:361)
dzungpv commented 1 year ago

I new to Kotlin, it may cause by getOrThrow() if api failed. But you can add a sample app and rewrite the docs

DadiBit commented 1 year ago

new to Kotlin, it may cause by getOrThrow() if api failed.

Yes, you're right. You can use .getOrNull()?.run { ... } ?: println("Error") or something similar to avoid the throw

But you can add a sample app and rewrite the docs.

I know, I'm sorry, I've been super busy in the past months, and now I want to stabilize more the SDK + properly document everything. Hopefully I can start writing documentation and testing half an hour every evening in the upcoming weeks. I'll try and do my best :)

Unluckily, I can't really create a sample android app since it would be too much time-consuming, but I could try to create a simple CLI Kotlin interface.

dzungpv commented 1 year ago

@DadiBit thanks, how can I call API from java?

DadiBit commented 1 year ago

@DadiBit thanks, how can I call API from java?

Haven't tested it, but:

I had barely no time to work on the project in the past months, so please let me know if you're facing issues with the import or the usage.

I have some old commits to push, plus a few fixes, features and overall I really need to get some documentation written, I know :sob: