Fields of InvalidResposneException made final

Deprecated pseudo-builder methods withStatusCode() and withResponse() in
favor of additional constructor arguments to enforce fixed state.
This commit is contained in:
Stefan Kalscheuer 2017-08-18 17:43:57 +02:00
parent 4bf9df5f73
commit 52a1abfb87
2 changed files with 88 additions and 11 deletions

View File

@ -814,7 +814,7 @@ public class HTTPVaultConnector implements VaultConnector {
new InputStreamReader(response.getEntity().getContent()))) {
return br.lines().collect(Collectors.joining("\n"));
} catch (IOException ignored) {
throw new InvalidResponseException("Could not parse response body").withStatusCode(200);
throw new InvalidResponseException("Could not parse response body", 200);
}
case 204:
return "";
@ -827,8 +827,6 @@ public class HTTPVaultConnector implements VaultConnector {
return request(base, retries - 1);
} else {
/* Fail on different error code and/or no retries left */
InvalidResponseException ex = new InvalidResponseException("Invalid response code")
.withStatusCode(response.getStatusLine().getStatusCode());
if (response.getEntity() != null) {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()))) {
@ -837,12 +835,14 @@ public class HTTPVaultConnector implements VaultConnector {
/* Check for "permission denied" response */
if (!er.getErrors().isEmpty() && er.getErrors().get(0).equals("permission denied"))
throw new PermissionDeniedException();
throw ex.withResponse(er.toString());
throw new InvalidResponseException("Invalid response code",
response.getStatusLine().getStatusCode(), er.toString());
} catch (IOException ignored) {
// Exception ignored.
}
}
throw ex;
throw new InvalidResponseException("Invalid response code",
response.getStatusLine().getStatusCode());
}
}
} catch (IOException e) {

View File

@ -24,13 +24,15 @@ package de.stklcode.jvault.connector.exception;
* @since 0.1
*/
public final class InvalidResponseException extends VaultConnectorException {
private Integer statusCode;
private String response;
private final Integer statusCode;
private final String response;
/**
* Constructs a new empty exception.
*/
public InvalidResponseException() {
this.statusCode = null;
this.response = null;
}
/**
@ -40,6 +42,8 @@ public final class InvalidResponseException extends VaultConnectorException {
*/
public InvalidResponseException(final String message) {
super(message);
this.statusCode = null;
this.response = null;
}
/**
@ -49,6 +53,8 @@ public final class InvalidResponseException extends VaultConnectorException {
*/
public InvalidResponseException(final Throwable cause) {
super(cause);
this.statusCode = null;
this.response = null;
}
/**
@ -59,6 +65,75 @@ public final class InvalidResponseException extends VaultConnectorException {
*/
public InvalidResponseException(final String message, final Throwable cause) {
super(message, cause);
this.statusCode = null;
this.response = null;
}
/**
* Constructs a new exception with the specified detail message and status code.
* <p>
* The HTTP status code can be retrieved by {@link #getStatusCode()} later.
*
* @param message the detail message
* @param statusCode status code of the HTTP response
* @since 0.6.2
*/
public InvalidResponseException(final String message, final Integer statusCode) {
super(message);
this.statusCode = statusCode;
this.response = null;
}
/**
* Constructs a new exception with the specified detail message, cause and status code.
* <p>
* The HTTP status code can be retrieved by {@link #getStatusCode()} later.
*
* @param message the detail message
* @param statusCode status code of the HTTP response
* @param cause the cause
* @since 0.6.2
*/
public InvalidResponseException(final String message, final Integer statusCode, final Throwable cause) {
this(message, statusCode, null, cause);
}
/**
* Constructs a new exception with the specified detail message, cause and status code.
* <p>
* The HTTP status code can be retrieved by {@link #getStatusCode()} later.
*
* @param message the detail message
* @param statusCode status code of the HTTP response
* @param response HTTP response string
* @since 0.6.2
*/
public InvalidResponseException(final String message,
final Integer statusCode,
final String response) {
super(message);
this.statusCode = statusCode;
this.response = response;
}
/**
* Constructs a new exception with the specified detail message, cause and status code.
* <p>
* The HTTP status code can be retrieved by {@link #getStatusCode()} later.
*
* @param message the detail message
* @param statusCode status code of the HTTP response
* @param response HTTP response string
* @param cause the cause
* @since 0.6.2
*/
public InvalidResponseException(final String message,
final Integer statusCode,
final String response,
final Throwable cause) {
super(message, cause);
this.statusCode = statusCode;
this.response = response;
}
/**
@ -66,10 +141,11 @@ public final class InvalidResponseException extends VaultConnectorException {
*
* @param statusCode the status code
* @return self
* @deprecated as of 0.6.2, use constructor with status code argument instead
*/
@Deprecated
public InvalidResponseException withStatusCode(final Integer statusCode) {
this.statusCode = statusCode;
return this;
return new InvalidResponseException(getMessage(), statusCode, getResponse(), getCause());
}
/**
@ -77,10 +153,11 @@ public final class InvalidResponseException extends VaultConnectorException {
*
* @param response response text
* @return self
* @deprecated use constructor with response argument instead
*/
@Deprecated
public InvalidResponseException withResponse(final String response) {
this.response = response;
return this;
return new InvalidResponseException(getMessage(), getStatusCode(), response, getCause());
}
/**