Skip to content

Commit

Permalink
add tests. handle parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Petrusevici authored and Vladimir Petrusevici committed Oct 18, 2023
1 parent 1a355ab commit 0404614
Show file tree
Hide file tree
Showing 2 changed files with 381 additions and 12 deletions.
25 changes: 16 additions & 9 deletions src/OpenFeature.Contrib.Providers.Flagsmith/FlagsmithProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Flagsmith;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenFeature.Constant;
using OpenFeature.Model;
using System.Collections.Generic;
Expand Down Expand Up @@ -49,9 +47,9 @@ public FlagsmithProvider(IFlagsmithClient flagsmithClient)
_flagsmithClient = flagsmithClient;
}

private Task<IFlags> GetFlags(EvaluationContext ctx)
private Task<IFlags> GetFlags(EvaluationContext ctx = null)
{
var key = ctx.GetValue("targetingKey").AsString;
var key = ctx?.GetValue("targetingKey")?.AsString;
return string.IsNullOrEmpty(key)
? _flagsmithClient.GetEnvironmentFlags()
: _flagsmithClient.GetIdentityFlags(key, ctx.AsDictionary().Select(x => new Trait(x.Key, x.Value.AsObject) as ITrait).ToList());
Expand Down Expand Up @@ -148,13 +146,22 @@ public override async Task<ResolutionDetails<Value>> ResolveStructureValue(strin
}
var stringValue = await flags.GetFeatureValue(flagKey);

var mappedValue = JsonNode.Parse(stringValue);
var value = ConvertValue(mappedValue);
if (value is null)
try
{
return new ResolutionDetails<Value>(flagKey, defaultValue, ErrorType.ParseError, Reason.Error, errorMessage: "Failed to parse value in structure type or value is null");
var mappedValue = JsonNode.Parse(stringValue);
var value = ConvertValue(mappedValue);
if (value is not null)
{
return new ResolutionDetails<Value>(flagKey, value);

}
}
return new ResolutionDetails<Value>(flagKey, value);
catch
{

}
return new ResolutionDetails<Value>(flagKey, defaultValue, ErrorType.ParseError, Reason.Error, errorMessage: "Failed to parse value in structure type or value is null");

}

/// <summary>
Expand Down
Loading

0 comments on commit 0404614

Please sign in to comment.