-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from openfga/feat/errors-and-retries
feat: Introduce error modeling and retries
- Loading branch information
Showing
17 changed files
with
689 additions
and
561 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/main/java/dev/openfga/sdk/api/client/HttpRequestAttempt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package dev.openfga.sdk.api.client; | ||
|
||
import static dev.openfga.sdk.util.StringUtil.isNullOrWhitespace; | ||
|
||
import dev.openfga.sdk.api.configuration.BaseConfiguration; | ||
import dev.openfga.sdk.errors.*; | ||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
import java.util.concurrent.*; | ||
|
||
public class HttpRequestAttempt<T> { | ||
private final ApiClient apiClient; | ||
private final BaseConfiguration configuration; | ||
private final Class<T> clazz; | ||
private final String name; | ||
private final HttpRequest request; | ||
|
||
public HttpRequestAttempt( | ||
HttpRequest request, String name, Class<T> clazz, ApiClient apiClient, BaseConfiguration configuration) | ||
throws FgaInvalidParameterException { | ||
if (configuration.getMaxRetries() == null) { | ||
throw new FgaInvalidParameterException("maxRetries", "Configuration"); | ||
} | ||
this.apiClient = apiClient; | ||
this.configuration = configuration; | ||
this.name = name; | ||
this.request = request; | ||
this.clazz = clazz; | ||
} | ||
|
||
public CompletableFuture<T> attemptHttpRequest() throws ApiException { | ||
int retryNumber = 0; | ||
return attemptHttpRequest(apiClient.getHttpClient(), retryNumber, null); | ||
} | ||
|
||
private CompletableFuture<T> attemptHttpRequest(HttpClient httpClient, int retryNumber, Throwable previousError) { | ||
return httpClient | ||
.sendAsync(request, HttpResponse.BodyHandlers.ofString()) | ||
.thenCompose(response -> { | ||
try { | ||
checkStatus(name, response, previousError); | ||
} catch (FgaApiRateLimitExceededError | FgaApiInternalError retryableError) { | ||
if (retryNumber < configuration.getMaxRetries()) { | ||
HttpClient delayingClient = getDelayedHttpClient(); | ||
return attemptHttpRequest(delayingClient, retryNumber + 1, retryableError); | ||
} | ||
return CompletableFuture.failedFuture(retryableError); | ||
} catch (ApiException e) { | ||
return CompletableFuture.failedFuture(e); | ||
} | ||
|
||
return deserializeResponse(response); | ||
}); | ||
} | ||
|
||
private CompletableFuture<T> deserializeResponse(HttpResponse<String> response) { | ||
if (clazz == Void.class && isNullOrWhitespace(response.body())) { | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
|
||
try { | ||
T deserialized = apiClient.getObjectMapper().readValue(response.body(), clazz); | ||
return CompletableFuture.completedFuture(deserialized); | ||
} catch (IOException e) { | ||
// Malformed response. | ||
return CompletableFuture.failedFuture(new ApiException(e)); | ||
} | ||
} | ||
|
||
private HttpClient getDelayedHttpClient() { | ||
Duration retryDelay = configuration.getMinimumRetryDelay(); | ||
return apiClient | ||
.getHttpClientBuilder() | ||
.executor(CompletableFuture.delayedExecutor(retryDelay.toNanos(), TimeUnit.NANOSECONDS)) | ||
.build(); | ||
} | ||
|
||
private static void checkStatus(String name, HttpResponse<String> response, Throwable previousError) | ||
throws ApiException { | ||
int status = response.statusCode(); | ||
String body = response.body(); | ||
|
||
switch (status) { | ||
case HttpURLConnection.HTTP_BAD_REQUEST: | ||
case 422: // HTTP 422 Unprocessable Entity | ||
throw new FgaApiValidationError(name, previousError, status, response.headers(), body); | ||
|
||
case HttpURLConnection.HTTP_UNAUTHORIZED: | ||
case HttpURLConnection.HTTP_FORBIDDEN: | ||
throw new FgaApiAuthenticationError(name, previousError, status, response.headers(), body); | ||
|
||
case HttpURLConnection.HTTP_NOT_FOUND: | ||
throw new FgaApiNotFoundError(name, previousError, status, response.headers(), body); | ||
|
||
case 429: // HTTP 429 Too Many Requests | ||
throw new FgaApiRateLimitExceededError(name, previousError, status, response.headers(), body); | ||
|
||
case HttpURLConnection.HTTP_INTERNAL_ERROR: | ||
throw new FgaApiInternalError(name, previousError, status, response.headers(), body); | ||
} | ||
|
||
// FGA and OAuth2 servers are only expected to return HTTP 2xx responses. | ||
if (status < 200 || 300 <= status) { | ||
throw new ApiException(name, previousError, status, response.headers(), body); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/dev/openfga/sdk/errors/FgaApiAuthenticationError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.openfga.sdk.errors; | ||
|
||
import java.net.http.HttpHeaders; | ||
|
||
public class FgaApiAuthenticationError extends ApiException { | ||
public FgaApiAuthenticationError( | ||
String message, Throwable throwable, int code, HttpHeaders responseHeaders, String responseBody) { | ||
super(message, code, responseHeaders, responseBody); | ||
} | ||
|
||
public FgaApiAuthenticationError(String message, int code, HttpHeaders responseHeaders, String responseBody) { | ||
this(message, (Throwable) null, code, responseHeaders, responseBody); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/dev/openfga/sdk/errors/FgaApiInternalError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.openfga.sdk.errors; | ||
|
||
import java.net.http.HttpHeaders; | ||
|
||
public class FgaApiInternalError extends ApiException { | ||
public FgaApiInternalError( | ||
String message, Throwable throwable, int code, HttpHeaders responseHeaders, String responseBody) { | ||
super(message, code, responseHeaders, responseBody); | ||
} | ||
|
||
public FgaApiInternalError(String message, int code, HttpHeaders responseHeaders, String responseBody) { | ||
this(message, (Throwable) null, code, responseHeaders, responseBody); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/dev/openfga/sdk/errors/FgaApiNotFoundError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.openfga.sdk.errors; | ||
|
||
import java.net.http.HttpHeaders; | ||
|
||
public class FgaApiNotFoundError extends ApiException { | ||
public FgaApiNotFoundError( | ||
String message, Throwable throwable, int code, HttpHeaders responseHeaders, String responseBody) { | ||
super(message, code, responseHeaders, responseBody); | ||
} | ||
|
||
public FgaApiNotFoundError(String message, int code, HttpHeaders responseHeaders, String responseBody) { | ||
this(message, (Throwable) null, code, responseHeaders, responseBody); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/dev/openfga/sdk/errors/FgaApiRateLimitExceededError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.openfga.sdk.errors; | ||
|
||
import java.net.http.HttpHeaders; | ||
|
||
public class FgaApiRateLimitExceededError extends ApiException { | ||
public FgaApiRateLimitExceededError( | ||
String message, Throwable throwable, int code, HttpHeaders responseHeaders, String responseBody) { | ||
super(message, code, responseHeaders, responseBody); | ||
} | ||
|
||
public FgaApiRateLimitExceededError(String message, int code, HttpHeaders responseHeaders, String responseBody) { | ||
this(message, (Throwable) null, code, responseHeaders, responseBody); | ||
} | ||
} |
Oops, something went wrong.