-
Notifications
You must be signed in to change notification settings - Fork 53
fix: collect and propagate per-provider errors in multi-provider strategies #1901
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
Open
suthar26
wants to merge
2
commits into
open-feature:main
Choose a base branch
from
suthar26:fix/multiprovider-error-propagation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
123 changes: 123 additions & 0 deletions
123
src/main/java/dev/openfeature/sdk/multiprovider/MultiProviderEvaluation.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 |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package dev.openfeature.sdk.multiprovider; | ||
|
|
||
| import dev.openfeature.sdk.ErrorCode; | ||
| import dev.openfeature.sdk.ImmutableMetadata; | ||
| import dev.openfeature.sdk.ProviderEvaluation; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A {@link ProviderEvaluation} subtype returned by multi-provider strategies that carries | ||
| * per-provider error details. | ||
| * | ||
| * <p>This type can represent both successful and failed evaluations. When a strategy exhausts | ||
| * all providers without a successful result, the per-provider errors describe why each provider | ||
| * failed. Custom strategies may also use this type for successful results to surface information | ||
| * about providers that were skipped or failed before the successful one. | ||
| * | ||
| * <p>Usage: | ||
| * <pre>{@code | ||
| * ProviderEvaluation<String> result = strategy.evaluate(...); | ||
| * if (result instanceof MultiProviderEvaluation<String> multiResult) { | ||
| * for (ProviderError error : multiResult.getProviderErrors()) { | ||
| * log.warn("Provider {} failed: {} - {}", | ||
| * error.getProviderName(), error.getErrorCode(), error.getErrorMessage()); | ||
| * } | ||
| * } | ||
| * }</pre> | ||
| * | ||
| * @param <T> the type of the flag being evaluated | ||
| */ | ||
| public class MultiProviderEvaluation<T> extends ProviderEvaluation<T> { | ||
|
|
||
| private final List<ProviderError> providerErrors; | ||
|
|
||
| private MultiProviderEvaluation( | ||
| T value, | ||
| String variant, | ||
| String reason, | ||
| ErrorCode errorCode, | ||
| String errorMessage, | ||
| ImmutableMetadata flagMetadata, | ||
| List<ProviderError> providerErrors) { | ||
| super(value, variant, reason, errorCode, errorMessage, flagMetadata); | ||
| this.providerErrors = | ||
| providerErrors != null ? Collections.unmodifiableList(providerErrors) : Collections.emptyList(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the per-provider error details. | ||
| * | ||
| * <p>Each entry describes why a specific provider failed during multi-provider evaluation. | ||
| * | ||
| * @return an unmodifiable list of per-provider errors, never {@code null} | ||
| */ | ||
| public List<ProviderError> getProviderErrors() { | ||
| return providerErrors; | ||
| } | ||
|
|
||
| /** | ||
| * Create a new builder for {@link MultiProviderEvaluation}. | ||
| * | ||
| * @param <T> the flag value type | ||
| * @return a new builder | ||
| */ | ||
| public static <T> Builder<T> multiProviderBuilder() { | ||
| return new Builder<>(); | ||
| } | ||
|
|
||
| /** | ||
| * Builder for {@link MultiProviderEvaluation}. | ||
| * | ||
| * @param <T> the flag value type | ||
| */ | ||
| public static class Builder<T> { | ||
| private T value; | ||
| private String variant; | ||
| private String reason; | ||
| private ErrorCode errorCode; | ||
| private String errorMessage; | ||
| private ImmutableMetadata flagMetadata; | ||
| private List<ProviderError> providerErrors; | ||
|
|
||
| public Builder<T> value(T value) { | ||
| this.value = value; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder<T> variant(String variant) { | ||
| this.variant = variant; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder<T> reason(String reason) { | ||
| this.reason = reason; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder<T> errorCode(ErrorCode errorCode) { | ||
| this.errorCode = errorCode; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder<T> errorMessage(String errorMessage) { | ||
| this.errorMessage = errorMessage; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder<T> flagMetadata(ImmutableMetadata flagMetadata) { | ||
| this.flagMetadata = flagMetadata; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder<T> providerErrors(List<ProviderError> providerErrors) { | ||
| this.providerErrors = providerErrors; | ||
| return this; | ||
| } | ||
|
|
||
| public MultiProviderEvaluation<T> build() { | ||
| return new MultiProviderEvaluation<>( | ||
| value, variant, reason, errorCode, errorMessage, flagMetadata, providerErrors); | ||
| } | ||
| } | ||
| } |
70 changes: 70 additions & 0 deletions
70
src/main/java/dev/openfeature/sdk/multiprovider/ProviderError.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 |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package dev.openfeature.sdk.multiprovider; | ||
|
|
||
| import dev.openfeature.sdk.ErrorCode; | ||
| import dev.openfeature.sdk.exceptions.OpenFeatureError; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Data; | ||
|
|
||
| /** | ||
| * Represents an error from a single provider during multi-provider evaluation. | ||
| * | ||
| * <p>Captures the provider name, error code, error message, and optionally the original exception | ||
| * that occurred during flag evaluation. This allows callers to inspect per-provider error details | ||
| * when a multi-provider strategy exhausts all providers without a successful result. | ||
| */ | ||
| @Data | ||
| @Builder | ||
| @AllArgsConstructor | ||
| public class ProviderError { | ||
| private String providerName; | ||
| private ErrorCode errorCode; | ||
| private String errorMessage; | ||
| private Exception exception; | ||
|
|
||
| /** | ||
| * Create a ProviderError from an error-coded {@code ProviderEvaluation} result. | ||
| * | ||
| * @param providerName the name of the provider that returned the error | ||
| * @param errorCode the error code from the evaluation result | ||
| * @param errorMessage the error message from the evaluation result (may be {@code null}) | ||
| * @return a new ProviderError | ||
| */ | ||
| public static ProviderError fromResult(String providerName, ErrorCode errorCode, String errorMessage) { | ||
| return new ProviderError(providerName, errorCode, errorMessage, null); | ||
| } | ||
|
|
||
| /** | ||
| * Create a ProviderError from a thrown exception. | ||
| * | ||
| * @param providerName the name of the provider that threw the exception | ||
| * @param exception the exception that was thrown | ||
| * @return a new ProviderError | ||
| */ | ||
| public static ProviderError fromException(String providerName, Exception exception) { | ||
| ErrorCode code = ErrorCode.GENERAL; | ||
| if (exception instanceof OpenFeatureError) { | ||
| code = ((OpenFeatureError) exception).getErrorCode(); | ||
| } | ||
| return new ProviderError(providerName, code, exception.getMessage(), exception); | ||
| } | ||
|
|
||
| /** | ||
| * Build an aggregate error message from a list of provider errors. | ||
| * | ||
| * @param baseMessage the base message to use (e.g. "No provider successfully responded") | ||
| * @param errors the list of per-provider errors | ||
| * @return an aggregate message including per-provider details | ||
| */ | ||
| public static String buildAggregateMessage(String baseMessage, List<ProviderError> errors) { | ||
| String details = errors.stream().map(ProviderError::toString).collect(Collectors.joining(", ")); | ||
| return baseMessage + ". Provider errors: [" + details + "]"; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return providerName + ": " + errorCode + " (" + (errorMessage != null ? errorMessage : "unknown") + ")"; | ||
| } | ||
| } | ||
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
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
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.
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 error code will always be
not found, right? So should we maybe convert this to aNotFoundclass, or maybe even just save the names of the providers that did not find the flag?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.
ProviderError is used across the strategies and we need the full error codes in the other strategies,
if you believe it is a better approach to simplify FirstMatchStrategy to just get the name of the providers I can implement that.