Skip to content

Commit 2bf65d2

Browse files
authored
Merge pull request #92 from adpi2/http-error-report
Improve HTTP error report
2 parents 42b32d1 + 4179430 commit 2bf65d2

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

sbt-plugin/src/main/scala/ch/epfl/scala/SubmitDependencyGraph.scala

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import ch.epfl.scala.GithubDependencyGraphPlugin.autoImport._
1212
import ch.epfl.scala.JsonProtocol._
1313
import ch.epfl.scala.githubapi.JsonProtocol._
1414
import ch.epfl.scala.githubapi._
15+
import gigahorse.FullResponse
1516
import gigahorse.HttpClient
1617
import gigahorse.support.apachehttp.Gigahorse
1718
import sbt._
@@ -69,35 +70,41 @@ object SubmitDependencyGraph {
6970

7071
private def submitInternal(state: State): State = {
7172
val snapshot = githubDependencySnapshot(state)
72-
val url = new URL(s"${githubApiUrl()}/repos/${githubRepository()}/dependency-graph/snapshots")
73+
val snapshotUrl = s"${githubApiUrl()}/repos/${githubRepository()}/dependency-graph/snapshots"
7374

7475
val snapshotJson = CompactPrinter(Converter.toJsonUnsafe(snapshot))
7576
val request = Gigahorse
76-
.url(url.toString)
77+
.url(snapshotUrl)
7778
.post(snapshotJson, StandardCharsets.UTF_8)
7879
.addHeaders(
7980
"Content-Type" -> "application/json",
8081
"Authorization" -> s"token ${githubToken()}"
8182
)
8283

83-
state.log.info(s"Submiting dependency snapshot to $url")
84+
state.log.info(s"Submiting dependency snapshot to $snapshotUrl")
8485
val result = for {
85-
httpResp <- Try(Await.result(http.run(request), Duration.Inf))
86-
jsonResp <- JsonParser.parseFromByteBuffer(httpResp.bodyAsByteBuffer)
87-
response <- Converter.fromJson[SnapshotResponse](jsonResp)
88-
} yield new URL(url, response.id.toString)
89-
90-
result match {
91-
case scala.util.Success(result) =>
92-
state.log.info(s"Submitted successfully as $result")
93-
state
94-
case scala.util.Failure(cause) =>
95-
throw new MessageOnlyException(
96-
s"Failed to submit the dependency snapshot because of ${cause.getClass.getName}: ${cause.getMessage}"
97-
)
86+
httpResp <- Try(Await.result(http.processFull(request), Duration.Inf))
87+
snapshot <- getSnapshot(httpResp)
88+
} yield {
89+
state.log.info(s"Submitted successfully as $snapshotUrl/${snapshot.id}")
90+
state
9891
}
92+
93+
result.get
9994
}
10095

96+
private def getSnapshot(httpResp: FullResponse): Try[SnapshotResponse] =
97+
httpResp.status match {
98+
case status if status / 100 == 2 =>
99+
JsonParser
100+
.parseFromByteBuffer(httpResp.bodyAsByteBuffer)
101+
.flatMap(Converter.fromJson[SnapshotResponse])
102+
case status =>
103+
val message =
104+
s"Unexpected status $status ${httpResp.statusText} with body:\n${httpResp.bodyAsString}"
105+
throw new MessageOnlyException(message)
106+
}
107+
101108
private def githubDependencySnapshot(state: State): DependencySnapshot = {
102109
val detector = DetectorMetadata(
103110
SbtGithubDependencySubmission.name,

0 commit comments

Comments
 (0)