Skip to content

added exception handling to writeNonTransaction #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,18 @@ private CompletableFuture<ClientWriteResponse> writeNonTransaction(
for (int i = 1; i < transactions.size(); i++) {
final int index = i; // Must be final in this scope for closure.

// The resulting completable future of this chain will result in either:
// 1. The first exception thrown in a failed completion. Other thenCompose() will not be evaluated.
// 2. The final successful ClientWriteResponse.
futureResponse = futureResponse.thenCompose(
_response -> this.writeTransactions(storeId, transactions.get(index), options));
_response -> this.writeTransactions(storeId, transactions.get(index), options)
.exceptionally(ex -> {
//handle exception
Map<String, List<String>> headers = new HashMap<>();
headers.put("Content-Type", List.of("application/json"));
headers.put("Retry-After", List.of("30")); // in seconds
headers.put("X-Debug-Info", List.of(ex.getMessage()));
ApiResponse<Object> errResponse = new ApiResponse<>(500,headers, "{\"code\":\"internal_error\",\"message\":\"Internal Server Error\"}", ex.getMessage());
return new ClientWriteResponse(errResponse);
})
);
}

return futureResponse;
Expand Down