Skip to content

[FSSDK-9128] fix: Handle ODP INVALID_IDENTIFIER_EXCEPTION code #348

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 5 commits into from
Apr 25, 2023
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
259 changes: 214 additions & 45 deletions OptimizelySDK.Tests/OdpTests/OdpSegmentApiManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,28 @@ public void Setup()
public void ShouldParseSuccessfulResponse()
{
const string RESPONSE_JSON = @"
{
""data"": {
""customer"": {
""audiences"": {
""edges"": [
{
""node"": {
""name"": ""has_email"",
""state"": ""qualified"",
}
},
{
""node"": {
""name"": ""has_email_opted_in"",
""state"": ""not-qualified""
{
""data"": {
""customer"": {
""audiences"": {
""edges"": [
{
""node"": {
""name"": ""has_email"",
""state"": ""qualified"",
}
},
{
""node"": {
""name"": ""has_email_opted_in"",
""state"": ""not-qualified""
}
},
]
},
}
},
]
},
}
}
}";
}
}";

var response = new OdpSegmentApiManager().DeserializeSegmentsFromJson(RESPONSE_JSON);

Expand Down Expand Up @@ -110,38 +110,207 @@ public void ShouldHandleAttemptToDeserializeInvalidJsonResponse()
}

[Test]
public void ShouldParseErrorResponse()
public void ShouldParseInvalidIdentifierExceptionResponse()
{
const string RESPONSE_JSON = @"
{
""errors"": [
{
""message"": ""Exception while fetching data (/customer) : Exception: could not resolve _fs_user_id = not-real-user-id"",
""locations"": [
{
""line"": 2,
""column"": 3
{
""errors"": [
{
""message"": ""Exception while fetching data (/customer) : Exception: could not resolve _fs_user_id = not-real-user-id"",
""locations"": [
{
""line"": 2,
""column"": 3
}
],
""path"": [
""customer""
],
""extensions"": {
""code"": ""INVALID_IDENTIFIER_EXCEPTION"",
""classification"": ""DataFetchingException""
}
}
],
""data"": {
""customer"": null
}
],
""path"": [
""customer""
],
""extensions"": {
""classification"": ""InvalidIdentifierException""
}
}
],
""data"": {
""customer"": null
}
}";
}";

var response = new OdpSegmentApiManager().DeserializeSegmentsFromJson(RESPONSE_JSON);

Assert.IsNull(response.Data.Customer);
Assert.IsNotNull(response.Errors);
Assert.AreEqual(response.Errors[0].Extensions.Classification,
"InvalidIdentifierException");
Assert.AreEqual("DataFetchingException",
response.Errors[0].Extensions.Classification);
Assert.AreEqual("INVALID_IDENTIFIER_EXCEPTION",
response.Errors[0].Extensions.Code
);
}

[Test]
public void ShouldParseOnlyFirstErrorInvalidIdentifierExceptionResponse()
{
const string RESPONSE_JSON = @"
{
""errors"": [
{
""message"": ""Exception while fetching data (/customer) : Exception: could not resolve _fs_user_id = not-real-user-id"",
""locations"": [
{
""line"": 2,
""column"": 3
}
],
""path"": [
""customer""
],
""extensions"": {
""code"": ""INVALID_IDENTIFIER_EXCEPTION"",
""classification"": ""DataFetchingException""
}
},
{
""message"": ""Second Ignored Exception while fetching data (/desks) : Exception: yet another exception not yet known"",
""locations"": [
{
""line"": 4,
""column"": 5
}
],
""path"": [
""desks/wooden""
],
""extensions"": {
""code"": ""SECOND_IGNORED_UNPLANNED_EXCEPTION"",
""classification"": ""IgnoredNewException""
}
}
],
""data"": {
""customer"": null
}
}";
var httpClient = HttpClientTestUtil.MakeHttpClient(HttpStatusCode.OK, RESPONSE_JSON);
var manager =
new OdpSegmentApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient);

var segments = manager.FetchSegments(
VALID_ODP_PUBLIC_KEY,
ODP_GRAPHQL_HOST,
Constants.FS_USER_ID,
"tester-156",
_segmentsToCheck);


Assert.IsNull(segments);
_mockLogger.Verify(
l => l.Log(LogLevel.WARN, "Audience segments fetch failed (invalid identifier)"),
Times.Once);
}

[Test]
public void ShouldParseOnlyFirstErrorThatOdpThrowsAtUsResponse()
{
const string RESPONSE_JSON = @"
{
""errors"": [
{
""message"": ""Exception while fetching data (/chairs) : Exception: could not the chair = musical"",
""locations"": [
{
""line"": 4,
""column"": 1
}
],
""path"": [
""chair/musical""
],
""extensions"": {
""classification"": ""YetKnownOdpException""
}
},
{
""message"": ""Second Ignored Exception while fetching data (/desks) : Exception: yet another exception not yet known"",
""locations"": [
{
""line"": 4,
""column"": 5
}
],
""path"": [
""desks/wooden""
],
""extensions"": {
""code"": ""SECOND_IGNORED_UNPLANNED_EXCEPTION"",
""classification"": ""IgnoredNewException""
}
}
],
""data"": {
""customer"": null
}
}";
var httpClient = HttpClientTestUtil.MakeHttpClient(HttpStatusCode.OK, RESPONSE_JSON);
var manager =
new OdpSegmentApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient);

var segments = manager.FetchSegments(
VALID_ODP_PUBLIC_KEY,
ODP_GRAPHQL_HOST,
Constants.FS_USER_ID,
"tester-325",
_segmentsToCheck);


Assert.IsNull(segments);
_mockLogger.Verify(
l => l.Log(LogLevel.ERROR,
"Audience segments fetch failed (YetKnownOdpException)"),
Times.Once);
}


[Test]
public void ShouldParseBadExtensionsShapeInResponse()
{
const string RESPONSE_JSON = @"
{
""errors"": [
{
""message"": ""Exception while fetching data (/chairs) : Exception: could not the chair = musical"",
""locations"": [
{
""line"": 4,
""column"": 1
}
],
""path"": [
""chair/musical""
],
""extensions"": { }
}
],
""data"": {
""customer"": null
}
}";
var httpClient = HttpClientTestUtil.MakeHttpClient(HttpStatusCode.OK, RESPONSE_JSON);
var manager =
new OdpSegmentApiManager(_mockLogger.Object, _mockErrorHandler.Object, httpClient);

var segments = manager.FetchSegments(
VALID_ODP_PUBLIC_KEY,
ODP_GRAPHQL_HOST,
Constants.FS_USER_ID,
"tester-895",
_segmentsToCheck);


Assert.IsNull(segments);
_mockLogger.Verify(
l => l.Log(LogLevel.ERROR, "Audience segments fetch failed (decode error)"),
Times.Once);
}

[Test]
Expand Down
7 changes: 6 additions & 1 deletion OptimizelySDK/Odp/Entity/Extension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Optimizely
* Copyright 2022-2023 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,5 +25,10 @@ public class Extension
/// Named exception type from the error
/// </summary>
public string Classification { get; set; }

/// <summary>
/// Code of exception
/// </summary>
public string Code { get; set; }
}
}
28 changes: 18 additions & 10 deletions OptimizelySDK/Odp/OdpSegmentApiManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,17 @@ public string[] FetchSegments(string apiKey, string apiHost, string userKey,

if (segments.HasErrors)
{
var errors = string.Join(";", segments.Errors.Select(e => e.ToString()));

_logger.Log(LogLevel.ERROR, $"{AUDIENCE_FETCH_FAILURE_MESSAGE} ({errors})");
var firstError = segments.Errors.First();
if (firstError.Extensions?.Code == "INVALID_IDENTIFIER_EXCEPTION")
{
var message = $"{AUDIENCE_FETCH_FAILURE_MESSAGE} (invalid identifier)";
_logger.Log(LogLevel.WARN, message);
}
else
{
var errorMessage = firstError.Extensions?.Classification ?? "decode error";
_logger.Log(LogLevel.ERROR, $"{AUDIENCE_FETCH_FAILURE_MESSAGE} ({errorMessage})");
}

return null;
}
Expand All @@ -131,10 +139,10 @@ public string[] FetchSegments(string apiKey, string apiHost, string userKey,
return null;
}

return segments.Data.Customer.Audiences.Edges.
Where(e => e.Node.State == BaseCondition.QUALIFIED).
Select(e => e.Node.Name).
ToArray();
return segments.Data.Customer.Audiences.Edges
.Where(e => e.Node.State == BaseCondition.QUALIFIED)
.Select(e => e.Node.Name)
.ToArray();
}

/// <summary>
Expand All @@ -155,9 +163,9 @@ IEnumerable segmentsToCheck
""userId"": ""{userValue}"",
""audiences"": {audiences}
}
}".Replace("{userKey}", userKey).
Replace("{userValue}", userValue).
Replace("{audiences}", JsonConvert.SerializeObject(segmentsToCheck));
}".Replace("{userKey}", userKey)
.Replace("{userValue}", userValue)
.Replace("{audiences}", JsonConvert.SerializeObject(segmentsToCheck));
}

/// <summary>
Expand Down