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.aether.Exceptions
import com.google.gson.Gson
import com.google.gson.JsonParser
import com.google.gson.annotations.SerializedName
import com.google.inject.Inject
import okhttp3.OkHttpClient
@ -119,17 +120,24 @@ class GithubApi2 @Inject constructor(
} else {
service.getReleasesNoAuth("cbeust", "kobalt")
}
val releases = req.execute().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")
}
}
val ex = req.execute()
val errorBody = ex.errorBody()
if (errorBody != null) {
val jsonError = JsonParser().parse(errorBody.string())
warn("Couldn't call Github.getReleases(): $jsonError")
} 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) {
kobaltLog(1, "Couldn't retrieve releases from github: " + e.message)