-
Notifications
You must be signed in to change notification settings - Fork 50
chore: changing error hook api to contain object #1525
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package dev.openfeature.sdk; | ||
|
|
||
| import dev.openfeature.sdk.exceptions.OpenFeatureError; | ||
| import lombok.Builder; | ||
| import lombok.Value; | ||
|
|
||
| /** | ||
| * Represents details about an error that occurred during a flag evaluation or other operations. | ||
| * This class captures the exception, evaluation details, error code, and an error message. | ||
| * | ||
| * @param <T> The type of the value being evaluated in the {@link FlagEvaluationDetails}. | ||
| */ | ||
| @Value | ||
| @Builder | ||
| public class ErrorDetails<T> { | ||
| Exception error; | ||
| FlagEvaluationDetails<T> details; | ||
| ErrorCode errorCode; | ||
| String errorMessage; | ||
|
|
||
| /** | ||
| * Creates an {@code ErrorDetails} instance from the given {@link FlagEvaluationDetails}. | ||
| * This method extracts the error message and error code from the provided details. | ||
| * | ||
| * @param details The {@link FlagEvaluationDetails} object containing flag evaluation information. | ||
| * @param <T> The type of the value being evaluated in the {@link FlagEvaluationDetails}. | ||
| * @return An {@code ErrorDetails} object populated with the provided evaluation details. | ||
| */ | ||
| public static <T> ErrorDetails<T> from(FlagEvaluationDetails<T> details) { | ||
| return ErrorDetails.<T>builder() | ||
| .details(details) | ||
| .errorMessage(details.getErrorMessage()) | ||
| .errorCode(details.getErrorCode()) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an {@code ErrorDetails} instance from the given exception and {@link FlagEvaluationDetails}. | ||
| * If the exception is an instance of {@link OpenFeatureError}, its error code is extracted | ||
| * and set in the {@link FlagEvaluationDetails}. Otherwise, a general error code is used. | ||
| * The exception's message is also set as the error message. | ||
| * | ||
| * @param exception The exception that occurred during the operation. | ||
| * @param details The {@link FlagEvaluationDetails} object containing flag evaluation information. | ||
| * @param <T> The type of the value being evaluated in the {@link FlagEvaluationDetails}. | ||
| * @return An {@code ErrorDetails} object populated with the exception and evaluation details. | ||
| */ | ||
| public static <T> ErrorDetails<T> from(Exception exception, FlagEvaluationDetails<T> details) { | ||
| if (exception instanceof OpenFeatureError) { | ||
| details.setErrorCode(((OpenFeatureError) exception).getErrorCode()); | ||
| } else { | ||
| details.setErrorCode(ErrorCode.GENERAL); | ||
| } | ||
| details.setErrorMessage(exception.getMessage()); | ||
| return ErrorDetails.<T>builder() | ||
| .error(exception) | ||
| .errorMessage(exception.getMessage()) | ||
| .errorCode(details.getErrorCode()) | ||
| .build(); | ||
| } | ||
|
Comment on lines
+48
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a critical issue in this factory method: the Additionally, this method has a side effect of mutating the The suggestion below fixes the critical bug. You might also consider moving the mutation logic back to the caller in public static <T> ErrorDetails<T> from(Exception exception, FlagEvaluationDetails<T> details) {
if (exception instanceof OpenFeatureError) {
details.setErrorCode(((OpenFeatureError) exception).getErrorCode());
} else {
details.setErrorCode(ErrorCode.GENERAL);
}
details.setErrorMessage(exception.getMessage());
return ErrorDetails.<T>builder()
.error(exception)
.details(details)
.errorMessage(exception.getMessage())
.errorCode(details.getErrorCode())
.build();
} |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
ErrorDetailsclass containserrorCodeanderrorMessagefields, which are also present in theFlagEvaluationDetailsobject stored in thedetailsfield. This introduces data redundancy. After fixing the bug in thefrom(Exception, FlagEvaluationDetails)factory wheredetailsis not set, thedetailsfield will always be present.To simplify the design and avoid redundancy, you could consider removing
errorCodeanderrorMessagefromErrorDetailsand access them viagetDetails().getErrorCode()andgetDetails().getErrorMessage(). Alternatively, you could add getter methods toErrorDetailsthat delegate to thedetailsobject if you want to keep the convenient accessors.