Attach offending HttpStatusCodeException to VaultException

We now attach HttpStatusCodeException as cause when constructing VaultException for error handling purposes.

Previously, the cause was unset which required message parsing to detect the actual cause.

See gh-319.
This commit is contained in:
Mark Paluch
2018-10-10 09:13:37 +02:00
parent 605de0ab39
commit 2dbf06f4e5
2 changed files with 96 additions and 20 deletions

View File

@@ -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));
}
/**

View File

@@ -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);
}
}