LeoAndo / AndroidGithubSearch

Native Androidで作るGithubリポジトリ検索アプリ
6 stars 0 forks source link

適切な箇所に@Throwsをつける #84

Closed LeoAndo closed 1 year ago

LeoAndo commented 1 year ago

refs: https://kotlinlang.org/docs/multiplatform-mobile-upgrade-app.html#connect-http-client https://zenn.dev/ktrueda/articles/kotlin-jvm-0006

Exceptionをスローするfunctionは@Throws(Exception::class)のようにアノテーションをつけたい。 これによりバイトコードレベルで functionに throws Exceptionが付与される。

import io.ktor.client.call.*
import io.ktor.client.request.*

class Greeting {
    // ...
    @Throws(Exception::class)
    suspend fun greeting(): String {
        val rockets: List<RocketLaunch> =
            httpClient.get("https://api.spacexdata.com/v4/launches").body()
        val lastSuccessLaunch = rockets.last { it.launchSuccess == true }
        return "Guess what it is! > ${platform.name.reversed()}!" +
                "\nThere are only ${daysUntilNewYear()} left until New Year! 🎆" +
                "\nThe last successful launch was ${lastSuccessLaunch.launchDateUTC} 🚀"
    }
}