Skip to content

Commit 85e093d

Browse files
author
Barry Klawans
committed
Adding the ability to not treat 4xx response codes as errors that should be retried and converted into exceptions
1 parent c5f733f commit 85e093d

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/main/java/com/bettercloud/vault/VaultConfig.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class VaultConfig implements Serializable {
4040
private int retryIntervalMilliseconds;
4141
private Integer globalEngineVersion;
4242
private String nameSpace;
43+
private Boolean treatInvalidRequestsAsErrors = true;
4344
private EnvironmentLoader environmentLoader;
4445

4546
/**
@@ -207,6 +208,26 @@ public VaultConfig readTimeout(final Integer readTimeout) {
207208
return this;
208209
}
209210

211+
/**
212+
* <p>Specifies whether 4xx class status codes should be treated as errors or invalid calls.</p>
213+
*
214+
* <p>If 4xx status codes are treated as errors, any call that returns them will retry (if specified)
215+
* and will result in an exception being thrown. If disabled, a 4xx return code does not retry, does
216+
* not generate an exception, and instead returns the error in the response body</p>
217+
*
218+
* <p>If you set this to <code>false</code> you need to check the response code before using the data
219+
* in a response</p>
220+
*
221+
* <p>The default behavior is to treat 4xx status codes as errors.</p>
222+
*
223+
* @param treatInvalidRequestsAsErrors Should 4xx class status codes be treated as errors.
224+
* @return This object, with treatInvalidRequestsAsErrors populated, ready for additional builder-pattern method calls or else finalization with the build() method
225+
*/
226+
public VaultConfig treatInvalidRequestsAsErrors(final Boolean treatInvalidRequestsAsErrors) {
227+
this.treatInvalidRequestsAsErrors = treatInvalidRequestsAsErrors;
228+
return this;
229+
}
230+
210231
/**
211232
* <p>Sets the maximum number of times that an API operation will retry upon failure.</p>
212233
*
@@ -314,6 +335,10 @@ public Integer getReadTimeout() {
314335
return readTimeout;
315336
}
316337

338+
public Boolean isTreatInvalidRequestsAsErrors() {
339+
return treatInvalidRequestsAsErrors;
340+
}
341+
317342
public int getMaxRetries() {
318343
return maxRetries;
319344
}

src/main/java/com/bettercloud/vault/api/Logical.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private LogicalResponse read(final String path, Boolean shouldRetry, final logic
9292
.get();
9393

9494
// Validate response
95-
if (restResponse.getStatus() != 200) {
95+
if (restResponse.getStatus() != 200 && !(!config.isTreatInvalidRequestsAsErrors() && restResponse.getStatus() >= 400 && restResponse.getStatus() < 500)) {
9696
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
9797
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
9898
restResponse.getStatus());
@@ -161,7 +161,7 @@ public LogicalResponse read(final String path, Boolean shouldRetry, final Intege
161161
.get();
162162

163163
// Validate response
164-
if (restResponse.getStatus() != 200) {
164+
if (restResponse.getStatus() != 200 && !(!config.isTreatInvalidRequestsAsErrors() && restResponse.getStatus() >= 400 && restResponse.getStatus() < 500)) {
165165
throw new VaultException("Vault responded with HTTP status code: " + restResponse.getStatus()
166166
+ "\nResponse body: " + new String(restResponse.getBody(), StandardCharsets.UTF_8),
167167
restResponse.getStatus());
@@ -261,7 +261,7 @@ private LogicalResponse write(final String path, final Map<String, Object> nameV
261261

262262
// HTTP Status should be either 200 (with content - e.g. PKI write) or 204 (no content)
263263
final int restStatus = restResponse.getStatus();
264-
if (restStatus == 200 || restStatus == 204) {
264+
if (restStatus == 200 || restStatus == 204 || (!config.isTreatInvalidRequestsAsErrors() && restResponse.getStatus() >= 400 && restResponse.getStatus() < 500)) {
265265
return new LogicalResponse(restResponse, retryCount, operation);
266266
} else {
267267
throw new VaultException("Expecting HTTP status 204 or 200, but instead receiving " + restStatus

src/test/java/com/bettercloud/vault/VaultConfigTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,12 @@ public void testConfigBuilder_WithNamespace() throws VaultException {
256256
Assert.assertEquals(vaultConfig.getNameSpace(), "namespace");
257257
}
258258

259+
@Test
260+
public void testConfigBuiler_WithInvalidRequestAsNonError() throws VaultException {
261+
VaultConfig vaultConfig = new VaultConfig().address("address").token("token").treatInvalidRequestsAsErrors(false).build();
262+
assertEquals("address", vaultConfig.getAddress());
263+
assertEquals("token", vaultConfig.getToken());
264+
assertEquals(Boolean.FALSE, vaultConfig.isTreatInvalidRequestsAsErrors());
265+
266+
}
259267
}

0 commit comments

Comments
 (0)