Skip to content

Commit

Permalink
Display HTTP error response to the user (#92)
Browse files Browse the repository at this point in the history
* Log response body in case of error response from Nexus

* Add test that asserts failure with message

* Add assertion for status code

* Botched indent

* And another Spotless style bug
  • Loading branch information
hiddewie authored Apr 15, 2021
1 parent f3c24a7 commit 396d9ce
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.github.tomakehurst.wiremock.client.WireMock.get
import com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor
import com.github.tomakehurst.wiremock.client.WireMock.matching
import com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath
import com.github.tomakehurst.wiremock.client.WireMock.notFound
import com.github.tomakehurst.wiremock.client.WireMock.post
import com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor
import com.github.tomakehurst.wiremock.client.WireMock.put
Expand Down Expand Up @@ -231,6 +232,48 @@ class NexusPublishPluginTests {
assertUploadedToStagingRepo("/org/example/sample/0.0.1/sample-0.0.1.jar")
}

@Test
fun `displays the error response to the user when a request fails`() {
projectDir.resolve("settings.gradle").write("""
rootProject.name = 'sample'
""")
projectDir.resolve("build.gradle").write("""
plugins {
id('java-library')
id('maven-publish')
id('io.github.gradle-nexus.publish-plugin')
}
group = 'org.example'
version = '0.0.1'
publishing {
publications {
mavenJava(MavenPublication) {
from(components.java)
}
}
}
nexusPublishing {
repositories {
myNexus {
nexusUrl = uri('${server.baseUrl()}')
snapshotRepositoryUrl = uri('${server.baseUrl()}/snapshots/')
allowInsecureProtocol = true
username = 'username'
password = 'password'
}
}
}
""")

stubMissingStagingProfileRequest("/staging/profiles")

val result = runAndFail("publishToMyNexus")

assertFailure(result, ":initializeMyNexusStagingRepository")
assertThat(result.output).contains("status code 404")
assertThat(result.output).contains("""{"failure":"message"}""")
}

@Test
fun `publishes to two Nexus repositories`(@MethodScopeWiremockResolver.MethodScopedWiremockServer @Wiremock otherServer: WireMockServer) {
projectDir.resolve("settings.gradle").write("""
Expand Down Expand Up @@ -810,9 +853,11 @@ class NexusPublishPluginTests {
""")
}

private fun run(vararg arguments: String): BuildResult {
return gradleRunner(*arguments).build()
}
private fun run(vararg arguments: String): BuildResult =
gradleRunner(*arguments).build()

private fun runAndFail(vararg arguments: String): BuildResult =
gradleRunner(*arguments).buildAndFail()

private fun gradleRunner(vararg arguments: String): GradleRunner {
return gradleRunner
Expand All @@ -833,6 +878,12 @@ class NexusPublishPluginTests {
.willReturn(aResponse().withBody(gson.toJson(mapOf("data" to listOf(*stagingProfiles))))))
}

private fun stubMissingStagingProfileRequest(url: String, wireMockServer: WireMockServer = server) {
wireMockServer.stubFor(get(urlEqualTo(url))
.withHeader("User-Agent", matching("gradle-nexus-publish-plugin/.*"))
.willReturn(notFound().withBody(gson.toJson(mapOf("failure" to "message")))))
}

private fun stubCreateStagingRepoRequest(url: String, stagedRepositoryId: String, wireMockServer: WireMockServer = server) {
wireMockServer.stubFor(post(urlEqualTo(url))
.willReturn(aResponse().withBody(gson.toJson(mapOf("data" to mapOf("stagedRepositoryId" to stagedRepositoryId))))))
Expand Down Expand Up @@ -903,6 +954,10 @@ class NexusPublishPluginTests {
assertOutcome(result, taskPath, SUCCESS)
}

private fun assertFailure(result: BuildResult, taskPath: String) {
assertOutcome(result, taskPath, FAILED)
}

private fun assertSkipped(result: BuildResult, taskPath: String) {
assertOutcome(result, taskPath, SKIPPED)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import retrofit2.http.Headers
import retrofit2.http.POST
import retrofit2.http.Path
import java.io.IOException
import java.io.UncheckedIOException
import java.net.URI
import java.time.Duration

Expand Down Expand Up @@ -133,11 +132,11 @@ open class NexusClient(private val baseUrl: URI, username: String?, password: St
private fun failure(action: String, response: Response<*>): RuntimeException {
var message = "Failed to $action, server at $baseUrl responded with status code ${response.code()}"
val errorBody = response.errorBody()
if (errorBody != null && errorBody.contentLength() > 0) {
try {
message += ", body: $errorBody"
} catch (e: IOException) {
throw UncheckedIOException("Failed to read body of error response", e)
if (errorBody != null) {
message += try {
", body: ${errorBody.string()}"
} catch (exception: IOException) {
", body: <error while reading body of error response, message: ${exception.message}>"
}
}
return RuntimeException(message)
Expand Down

0 comments on commit 396d9ce

Please sign in to comment.