-
Notifications
You must be signed in to change notification settings - Fork 21
feat: improve error messaging #258
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
Closed
Closed
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2042e47
feat: improve error messaging
SoulPancake acff828
feat: changelog update
SoulPancake b1912b2
fix: spotless fmt
SoulPancake 7a2c8bd
set metadata and fix tests
SoulPancake b5cc8c1
feat: address copilot comments
SoulPancake 56698c1
fix: changelog update
SoulPancake 768783c
feat: address copilot comments
SoulPancake d954ca6
Merge branch 'main' into feat/improve-err-msging-p1
SoulPancake 3987ace
feat: add integration tests
SoulPancake 2f9f811
feat: addres scomments reduce dupl
SoulPancake 2e7c5b6
fix: spotless fmt
SoulPancake bf18849
feat: strict assertions
SoulPancake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
108 changes: 108 additions & 0 deletions
108
src/main/java/dev/openfga/sdk/errors/FgaApiValidationError.java
This file contains hidden or 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 |
|---|---|---|
| @@ -1,14 +1,122 @@ | ||
| package dev.openfga.sdk.errors; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import java.net.http.HttpHeaders; | ||
|
|
||
| public class FgaApiValidationError extends FgaError { | ||
|
|
||
| // String prefixes for parsing error messages | ||
| private static final String RELATION_PREFIX = "relation '"; | ||
| private static final String TYPE_PREFIX = "type '"; | ||
| private static final String CHECK_REQUEST_TUPLE_KEY_PREFIX = "CheckRequestTupleKey."; | ||
| private static final String TUPLE_KEY_PREFIX = "TupleKey."; | ||
| private static final String QUOTE_SUFFIX = "'"; | ||
| private static final String NOT_FOUND_SUFFIX = "' not found"; | ||
| private static final String MUST_NOT_BE_EMPTY = "must not be empty"; | ||
|
|
||
| private String invalidField; | ||
| private String invalidValue; | ||
|
|
||
| public FgaApiValidationError( | ||
| String message, Throwable cause, int code, HttpHeaders responseHeaders, String responseBody) { | ||
| super(message, cause, code, responseHeaders, responseBody); | ||
| parseValidationDetails(responseBody); | ||
| } | ||
|
|
||
| public FgaApiValidationError(String message, int code, HttpHeaders responseHeaders, String responseBody) { | ||
| super(message, code, responseHeaders, responseBody); | ||
| parseValidationDetails(responseBody); | ||
| } | ||
|
|
||
| /** | ||
| * Try to extract specific validation details from the error message. | ||
| * <p> | ||
| * This parsing is best-effort and based on current OpenFGA API error message formats. | ||
| * If the message format changes or doesn't match expected patterns, fields will be null. | ||
| * The application should not rely on these fields for critical logic. | ||
| * | ||
| * @param responseBody The API error response body | ||
| */ | ||
| private void parseValidationDetails(String responseBody) { | ||
| if (responseBody == null || responseBody.trim().isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| JsonNode root = getErrorMapper().readTree(responseBody); | ||
| String message = root.has("message") ? root.get("message").asText() : null; | ||
|
|
||
| if (message != null) { | ||
| // Parse patterns like: "relation 'document#invalid_relation' not found" | ||
| if (message.contains(RELATION_PREFIX) && message.contains(NOT_FOUND_SUFFIX)) { | ||
| int start = message.indexOf(RELATION_PREFIX) + RELATION_PREFIX.length(); | ||
| int end = message.indexOf(QUOTE_SUFFIX, start); | ||
| if (end > start) { | ||
| this.invalidField = "relation"; | ||
| this.invalidValue = message.substring(start, end); | ||
| addMetadata("invalid_field", invalidField); | ||
| addMetadata("invalid_value", invalidValue); | ||
| } | ||
| } | ||
| // Parse patterns like: "type 'invalid_type' not found" | ||
| else if (message.contains(TYPE_PREFIX) && message.contains(NOT_FOUND_SUFFIX)) { | ||
| int start = message.indexOf(TYPE_PREFIX) + TYPE_PREFIX.length(); | ||
| int end = message.indexOf(QUOTE_SUFFIX, start); | ||
| if (end > start) { | ||
| this.invalidField = "type"; | ||
| this.invalidValue = message.substring(start, end); | ||
| addMetadata("invalid_field", invalidField); | ||
| addMetadata("invalid_value", invalidValue); | ||
| } | ||
| } | ||
| // Parse patterns like: "invalid CheckRequestTupleKey.User: value does not match regex..." | ||
| else if (message.contains(CHECK_REQUEST_TUPLE_KEY_PREFIX)) { | ||
| int start = | ||
| message.indexOf(CHECK_REQUEST_TUPLE_KEY_PREFIX) + CHECK_REQUEST_TUPLE_KEY_PREFIX.length(); | ||
| int end = message.indexOf(":", start); | ||
| if (end > start) { | ||
| this.invalidField = message.substring(start, end); | ||
SoulPancake marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| addMetadata("invalid_field", invalidField); | ||
| } | ||
| } | ||
| // Parse patterns like: "invalid TupleKey.User: value does not match regex..." | ||
| else if (message.contains(TUPLE_KEY_PREFIX)) { | ||
| int start = message.indexOf(TUPLE_KEY_PREFIX) + TUPLE_KEY_PREFIX.length(); | ||
| int end = message.indexOf(":", start); | ||
| if (end > start) { | ||
| this.invalidField = message.substring(start, end); | ||
SoulPancake marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| addMetadata("invalid_field", invalidField); | ||
| } | ||
| } | ||
| // Parse patterns like: "object must not be empty" | ||
| else if (message.contains(MUST_NOT_BE_EMPTY)) { | ||
| String[] parts = message.trim().split("\\s+"); | ||
| if (parts.length > 0 && !parts[0].isEmpty()) { | ||
| this.invalidField = parts[0]; | ||
| addMetadata("invalid_field", invalidField); | ||
| } | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| // Parsing is best-effort, ignore failures | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the field name that failed validation, if it could be parsed from the error message. | ||
| * | ||
| * @return The invalid field name (e.g., "relation", "type", "User"), or null if not parsed | ||
| */ | ||
| public String getInvalidField() { | ||
| return invalidField; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the invalid value that caused the validation error, if available. | ||
| * | ||
| * @return The invalid value, or null if not parsed from the error message | ||
| */ | ||
| public String getInvalidValue() { | ||
| return invalidValue; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.