Skip to content
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

refactor: Use the newly introduced ConfigCat error codes to determine error type #201

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
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
34 changes: 16 additions & 18 deletions src/OpenFeature.Contrib.Providers.ConfigCat/ConfigCatProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public override async Task<ResolutionDetails<Value>> ResolveStructureValue(strin
var user = context?.BuildUser();
var result = await Client.GetValueDetailsAsync(flagKey, defaultValue?.AsObject, user);
var returnValue = result.IsDefaultValue ? defaultValue : new Value(result.Value);
var details = new ResolutionDetails<Value>(flagKey, returnValue, ParseErrorType(result.ErrorMessage), errorMessage: result.ErrorMessage, variant: result.VariationId);
var details = new ResolutionDetails<Value>(flagKey, returnValue, TranslateErrorCode(result.ErrorCode), errorMessage: result.ErrorMessage, variant: result.VariationId);
if (details.ErrorType == ErrorType.None)
{
return details;
Expand All @@ -77,7 +77,7 @@ private async Task<ResolutionDetails<T>> ResolveFlag<T>(string flagKey, Evaluati
{
var user = context?.BuildUser();
var result = await Client.GetValueDetailsAsync(flagKey, defaultValue, user);
var details = new ResolutionDetails<T>(flagKey, result.Value, ParseErrorType(result.ErrorMessage), errorMessage: result.ErrorMessage, variant: result.VariationId);
var details = new ResolutionDetails<T>(flagKey, result.Value, TranslateErrorCode(result.ErrorCode), errorMessage: result.ErrorMessage, variant: result.VariationId);
if (details.ErrorType == ErrorType.None)
{
return details;
Expand All @@ -86,25 +86,23 @@ private async Task<ResolutionDetails<T>> ResolveFlag<T>(string flagKey, Evaluati
throw new FeatureProviderException(details.ErrorType, details.ErrorMessage);
}

private static ErrorType ParseErrorType(string errorMessage)
private static ErrorType TranslateErrorCode(EvaluationErrorCode errorCode)
{
if (string.IsNullOrEmpty(errorMessage))
switch (errorCode)
{
return ErrorType.None;
case EvaluationErrorCode.None:
return ErrorType.None;
case EvaluationErrorCode.InvalidConfigModel:
return ErrorType.ParseError;
case EvaluationErrorCode.SettingValueTypeMismatch:
return ErrorType.TypeMismatch;
case EvaluationErrorCode.ConfigJsonNotAvailable:
return ErrorType.ProviderNotReady;
case EvaluationErrorCode.SettingKeyMissing:
return ErrorType.FlagNotFound;
default:
return ErrorType.General;
}
if (errorMessage.Contains("Config JSON is not present"))
{
return ErrorType.ParseError;
}
if (errorMessage.Contains("the key was not found in config JSON"))
{
return ErrorType.FlagNotFound;
}
if (errorMessage.Contains("The type of a setting must match the type of the specified default value"))
{
return ErrorType.TypeMismatch;
}
return ErrorType.General;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<PackageReference Include="ConfigCat.Client" Version="[9,)"/>
<PackageReference Include="ConfigCat.Client" Version="9.2.0"/>
beeme1mr marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
</Project>
Loading