1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Manage github API errors better.

This commit is contained in:
Cedric Beust 2017-01-11 15:03:16 -08:00
parent 3bf91feffe
commit 1efbabe6bd

View file

@ -6,6 +6,7 @@ import com.beust.kobalt.internal.KobaltSettings
import com.beust.kobalt.maven.Http import com.beust.kobalt.maven.Http
import com.beust.kobalt.maven.aether.Exceptions import com.beust.kobalt.maven.aether.Exceptions
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.JsonParser
import com.google.gson.annotations.SerializedName import com.google.gson.annotations.SerializedName
import com.google.inject.Inject import com.google.inject.Inject
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
@ -119,17 +120,24 @@ class GithubApi2 @Inject constructor(
} else { } else {
service.getReleasesNoAuth("cbeust", "kobalt") service.getReleasesNoAuth("cbeust", "kobalt")
} }
val releases = req.execute().body() val ex = req.execute()
if (releases != null) { val errorBody = ex.errorBody()
releases.firstOrNull()?.let { if (errorBody != null) {
try { val jsonError = JsonParser().parse(errorBody.string())
result = listOf(it.name, it.tagName).filterNotNull().first { !it.isBlank() } warn("Couldn't call Github.getReleases(): $jsonError")
} catch(ex: NoSuchElementException) {
throw KobaltException("Couldn't find the latest release")
}
}
} else { } else {
warn("Didn't receive any body in the response to GitHub.getReleases()") val releases = ex.body()
if (releases != null) {
releases.firstOrNull()?.let {
try {
result = listOf(it.name, it.tagName).filterNotNull().first { !it.isBlank() }
} catch(ex: NoSuchElementException) {
throw KobaltException("Couldn't find the latest release")
}
}
} else {
warn("Didn't receive any body in the response to GitHub.getReleases()")
}
} }
} catch(e: Exception) { } catch(e: Exception) {
kobaltLog(1, "Couldn't retrieve releases from github: " + e.message) kobaltLog(1, "Couldn't retrieve releases from github: " + e.message)