Skip to content

Commit

Permalink
refactor: Use the newly introduced ConfigCat error codes to determine…
Browse files Browse the repository at this point in the history
… error type (#201)

Signed-off-by: Adam Simon <adam@configcat.com>
  • Loading branch information
adams85 committed May 13, 2024
1 parent c7b30de commit a9eef05
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
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"/>
</ItemGroup>
</Project>

0 comments on commit a9eef05

Please sign in to comment.