diff --git a/src/compatTest/kotlin/io/github/gradlenexus/publishplugin/NexusPublishPluginTests.kt b/src/compatTest/kotlin/io/github/gradlenexus/publishplugin/NexusPublishPluginTests.kt index 36189667..91551764 100644 --- a/src/compatTest/kotlin/io/github/gradlenexus/publishplugin/NexusPublishPluginTests.kt +++ b/src/compatTest/kotlin/io/github/gradlenexus/publishplugin/NexusPublishPluginTests.kt @@ -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 @@ -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(""" @@ -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 @@ -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)))))) @@ -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) } diff --git a/src/main/kotlin/io/github/gradlenexus/publishplugin/internal/NexusClient.kt b/src/main/kotlin/io/github/gradlenexus/publishplugin/internal/NexusClient.kt index 696420a1..bec580ef 100644 --- a/src/main/kotlin/io/github/gradlenexus/publishplugin/internal/NexusClient.kt +++ b/src/main/kotlin/io/github/gradlenexus/publishplugin/internal/NexusClient.kt @@ -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 @@ -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: " } } return RuntimeException(message)