diff --git a/spring-vault-core/src/main/java/org/springframework/vault/client/VaultResponses.java b/spring-vault-core/src/main/java/org/springframework/vault/client/VaultResponses.java index bcded107..99f85e0c 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/client/VaultResponses.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/client/VaultResponses.java @@ -61,11 +61,11 @@ public abstract class VaultResponses { if (StringUtils.hasText(message)) { return new VaultException(String.format("Status %s %s: %s", - e.getRawStatusCode(), e.getStatusText(), message)); + e.getRawStatusCode(), e.getStatusText(), message), e); } - return new VaultException(String.format("Status %s %s", e.getStatusCode(), - e.getStatusText())); + return new VaultException(String.format("Status %s %s", e.getRawStatusCode(), + e.getStatusText()), e); } /** @@ -79,32 +79,27 @@ public abstract class VaultResponses { Assert.notNull(e, "HttpStatusCodeException must not be null"); - return buildException(e.getStatusCode(), e.getStatusText(), path, - VaultResponses.getError(e.getResponseBodyAsString())); + String message = VaultResponses.getError(e.getResponseBodyAsString()); + + if (StringUtils.hasText(message)) { + return new VaultException(String.format("Status %s %s [%s]: %s", + e.getRawStatusCode(), e.getStatusText(), path, message), e); + } + + return new VaultException(String.format("Status %s %s [%s]", + e.getRawStatusCode(), e.getStatusText(), path), e); } public static VaultException buildException(HttpStatus statusCode, String path, String message) { if (StringUtils.hasText(message)) { - return new VaultException(String.format("Status %s %s: %s", statusCode, path, + return new VaultException(String.format("Status %s [%s]: %s", statusCode, + path, message)); } - return new VaultException(String.format("Status %s %s", statusCode, path)); - } - - private static VaultException buildException(HttpStatus statusCode, - String statusText, String path, String message) { - - if (StringUtils.hasText(message)) { - return new VaultException(String.format("Status %s %s %s: %s", - statusCode.value(), - statusText, path, message)); - } - - return new VaultException(String.format("Status %s %s %s", statusCode.value(), - statusText, path)); + return new VaultException(String.format("Status %s [%s]", statusCode, path)); } /** diff --git a/spring-vault-core/src/test/java/org/springframework/vault/client/VaultResponsesUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/client/VaultResponsesUnitTests.java new file mode 100644 index 00000000..0630335f --- /dev/null +++ b/spring-vault-core/src/test/java/org/springframework/vault/client/VaultResponsesUnitTests.java @@ -0,0 +1,81 @@ +/* + * Copyright 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.vault.client; + +import java.nio.charset.StandardCharsets; + +import org.junit.Test; + +import org.springframework.http.HttpStatus; +import org.springframework.vault.VaultException; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.HttpStatusCodeException; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for {@link VaultResponses}. + * + * @author Mark Paluch + */ +public class VaultResponsesUnitTests { + + @Test + public void shouldBuildException() { + + HttpStatusCodeException cause = new HttpClientErrorException( + HttpStatus.BAD_REQUEST, "Bad Request"); + + VaultException vaultException = VaultResponses.buildException(cause); + assertThat(vaultException).hasMessageContaining("Status 400 Bad Request;") + .hasCause(cause); + } + + @Test + public void shouldBuildExceptionWithErrorMessage() { + + HttpStatusCodeException cause = new HttpClientErrorException( + HttpStatus.BAD_REQUEST, "Bad Request", + "{\"errors\":[\"some-error\"]}".getBytes(), StandardCharsets.US_ASCII); + + VaultException vaultException = VaultResponses.buildException(cause); + assertThat(vaultException).hasMessageContaining( + "Status 400 Bad Request: some-error;").hasCause(cause); + } + + @Test + public void shouldBuildExceptionWithPath() { + + HttpStatusCodeException cause = new HttpClientErrorException( + HttpStatus.BAD_REQUEST, "Bad Request"); + + VaultException vaultException = VaultResponses.buildException(cause, "sys/path"); + assertThat(vaultException).hasMessageContaining( + "Status 400 Bad Request [sys/path];").hasCause(cause); + } + + @Test + public void shouldBuildExceptionWithPathAndErrorMessage() { + + HttpStatusCodeException cause = new HttpClientErrorException( + HttpStatus.BAD_REQUEST, "Bad Request", + "{\"errors\":[\"some-error\"]}".getBytes(), StandardCharsets.US_ASCII); + + VaultException vaultException = VaultResponses.buildException(cause, "sys/path"); + assertThat(vaultException).hasMessageContaining( + "Status 400 Bad Request [sys/path]: some-error;").hasCause(cause); + } +}