Skip to content

Commit

Permalink
catch exception of decoding JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksei-valiano committed Jan 17, 2023
1 parent b93a420 commit 1eac30a
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 28 deletions.
47 changes: 19 additions & 28 deletions Assets/AdaptySDK/Adapty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void Identify(string customerUserId, Action<Error> completionHandl
_Adapty.Identify(customerUserId, (json) =>
{
if (completionHandler == null) return;
var error = JSONNode.Parse(json).GetErrorIfPresent("error");
var error = json.ExtructErrorIfPresent();
try
{
completionHandler(error);
Expand All @@ -39,7 +39,7 @@ public static void Logout(Action<Error> completionHandler)
=> _Adapty.Logout((json) =>
{
if (completionHandler == null) return;
var error = JSONNode.Parse(json).GetErrorIfPresent("error");
var error = json.ExtructErrorIfPresent();
try
{
completionHandler(error);
Expand All @@ -54,12 +54,10 @@ public static void GetPaywall(string id, Action<Paywall, Error> completionHandle
=> _Adapty.GetPaywall(id, (json) =>
{
if (completionHandler == null) return;
var response = JSONNode.Parse(json);
var error = response.GetErrorIfPresent("error");
var result = error != null ? null : response.GetPaywall("success");
var response = json.ExtructPaywalleOrError();
try
{
completionHandler(result, error);
completionHandler(response.Value, response.Error);
}
catch (Exception e)
{
Expand Down Expand Up @@ -94,12 +92,10 @@ public static void GetPaywallProducts(Paywall paywall, IOSProductsFetchPolicy fe
_Adapty.GetPaywallProducts(paywallJson, fetchPolicy.ToJSON(), (json) =>
{
if (completionHandler == null) return;
var response = JSONNode.Parse(json);
var error = response.GetErrorIfPresent("error");
var result = error != null ? null : response.GetPaywallProductList("success");
var response = json.ExtructPaywallProductListOrError();
try
{
completionHandler(result, error);
completionHandler(response.Value, response.Error);
}
catch (Exception e)
{
Expand All @@ -112,12 +108,10 @@ public static void GetProfile(Action<Profile, Error> completionHandler)
=> _Adapty.GetProfile((json) =>
{
if (completionHandler == null) return;
var response = JSONNode.Parse(json);
var error = response.GetErrorIfPresent("error");
var result = error != null ? null : response.GetProfile("success");
var response = json.ExtructProfileOrError();
try
{
completionHandler(result, error);
completionHandler(response.Value, response.Error);
}
catch (Exception e)
{
Expand All @@ -129,12 +123,11 @@ public static void RestorePurchases(Action<Profile, Error> completionHandler)
=> _Adapty.RestorePurchases((json) =>
{
if (completionHandler == null) return;
var response = JSONNode.Parse(json);
var error = response.GetErrorIfPresent("error");
var result = error != null ? null : response.GetProfile("success");
var response = json.ExtructProfileOrError();
try
{
completionHandler(result, error);
completionHandler(response.Value, response.Error);
}
catch (Exception e)
{
Expand Down Expand Up @@ -193,12 +186,10 @@ public static void MakePurchase(PaywallProduct product, AndroidSubscriptionUpdat
_Adapty.MakePurchase(productJson, androidSubscriptionUpdateJson, (json) =>
{
if (completionHandler == null) return;
var response = JSONNode.Parse(json);
var error = response.GetErrorIfPresent("error");
var result = error != null ? null : response.GetProfile("success");
var response = json.ExtructProfileOrError();
try
{
completionHandler(result, error);
completionHandler(response.Value, response.Error);
}
catch (Exception e)
{
Expand Down Expand Up @@ -232,7 +223,7 @@ public static void LogShowPaywall(Paywall paywall, Action<Error> completionHandl
_Adapty.LogShowPaywall(paywallJson, (json) =>
{
if (completionHandler == null) return;
var error = JSONNode.Parse(json).GetErrorIfPresent("error");
var error = json.ExtructErrorIfPresent();
try
{
completionHandler(error);
Expand Down Expand Up @@ -268,7 +259,7 @@ public static void LogShowOnboarding(string name, string screenName, uint screen
_Adapty.LogShowOnboarding(parametersJson, (json) =>
{
if (completionHandler == null) return;
var error = JSONNode.Parse(json).GetErrorIfPresent("error");
var error = json.ExtructErrorIfPresent();
try
{
completionHandler(error);
Expand All @@ -284,7 +275,7 @@ public static void SetFallbackPaywalls(string paywalls, Action<Error> completion
=> _Adapty.SetFallbackPaywalls(paywalls, (json) =>
{
if (completionHandler == null) return;
var error = JSONNode.Parse(json).GetErrorIfPresent("error");
var error = json.ExtructErrorIfPresent();
try
{
completionHandler(error);
Expand Down Expand Up @@ -319,7 +310,7 @@ public static void UpdateProfile(ProfileParameters param, Action<Error> completi
_Adapty.UpdateProfile(parametersJson, (json) =>
{
if (completionHandler == null) return;
var error = JSONNode.Parse(json).GetErrorIfPresent("error");
var error = json.ExtructErrorIfPresent();
try
{
completionHandler(error);
Expand All @@ -342,7 +333,7 @@ public static void UpdateAttribution(string jsonstring, AttributionSource source
=> _Adapty.UpdateAttribution(jsonstring, source.ToJSON(), networkUserId, (json) =>
{
if (completionHandler == null) return;
var error = JSONNode.Parse(json).GetErrorIfPresent("error");
var error = json.ExtructErrorIfPresent();
try
{
completionHandler(error);
Expand All @@ -357,7 +348,7 @@ public static void SetVariationForTransaction(string variationId, string transac
=> _Adapty.SetVariationForTransaction(variationId, transactionId, (json) =>
{
if (completionHandler == null) return;
var error = JSONNode.Parse(json).GetErrorIfPresent("error");
var error = json.ExtructErrorIfPresent();
try
{
completionHandler(error);
Expand Down
16 changes: 16 additions & 0 deletions Assets/AdaptySDK/JSON/Error+JSON.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Aleksei Valiano on 20.12.2022.
//

using System;
using AdaptySDK.SimpleJSON;

namespace AdaptySDK
Expand Down Expand Up @@ -36,5 +37,20 @@ internal static Adapty.Error GetErrorIfPresent(this JSONNode node, string aKey)
if (obj is null) return null;
return new Adapty.Error(obj);
}

internal static Adapty.Error ExtructErrorIfPresent(this string json)
{
Adapty.Error error;
try
{
error = JSONNode.Parse(json).GetErrorIfPresent("error");
}
catch (Exception ex)
{
error = new Adapty.Error(Adapty.ErrorCode.DecodingFailed, "Failed decoding Adapty.Error", $"AdaptyUnityError.DecodingFailed({ex})");
}

return error;
}
}
}
80 changes: 80 additions & 0 deletions Assets/AdaptySDK/JSON/Result+JSON.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// Result+JSON.cs
// Adapty
//
// Created by Aleksei Valiano on 20.12.2022.
//

using System;
using System.Collections.Generic;
using static AdaptySDK.Adapty;

namespace AdaptySDK.SimpleJSON
{
internal static partial class JSONNodeExtensions
{
internal static Adapty.Result<Adapty.Profile> ExtructProfileOrError(this string json)
{
Adapty.Error error = null;
Adapty.Profile profile = null;
try
{
var response = JSONNode.Parse(json);
error = response.GetErrorIfPresent("error");
if (error is null)
{
profile = response.GetProfile("success");
}

}
catch (Exception ex)
{
error = new Adapty.Error(Adapty.ErrorCode.DecodingFailed, "Failed decoding Adapty.Profile Or Adapty.Error ", $"AdaptyUnityError.DecodingFailed({ex})");
}

return new Adapty.Result<Adapty.Profile>(profile, error);
}

internal static Adapty.Result<Adapty.Paywall> ExtructPaywalleOrError(this string json)
{
Adapty.Error error = null;
Adapty.Paywall paywall = null;
try
{
var response = JSONNode.Parse(json);
error = response.GetErrorIfPresent("error");
if (error is null)
{
paywall = response.GetPaywall("success");
}
}
catch (Exception ex)
{
error = new Adapty.Error(Adapty.ErrorCode.DecodingFailed, "Failed decoding Adapty.Paywall Or Adapty.Error ", $"AdaptyUnityError.DecodingFailed({ex})");
}

return new Adapty.Result<Adapty.Paywall>(paywall, error);
}

internal static Adapty.Result<IList<PaywallProduct>> ExtructPaywallProductListOrError(this string json)
{
Adapty.Error error = null;
IList<PaywallProduct> list = null;
try
{
var response = JSONNode.Parse(json);
error = response.GetErrorIfPresent("error");
if (error is null)
{
list = response.GetPaywallProductList("success");
}
}
catch (Exception ex)
{
error = new Adapty.Error(Adapty.ErrorCode.DecodingFailed, "Failed decoding list of Adapty.PaywallProduct Or Adapty.Error ", $"AdaptyUnityError.DecodingFailed({ex})");
}

return new Adapty.Result<IList<PaywallProduct>>(list, error);
}
}
}
11 changes: 11 additions & 0 deletions Assets/AdaptySDK/JSON/Result+JSON.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Assets/AdaptySDK/Models/Result.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Result.cs
// Adapty
//
// Created by Aleksei Valiano on 14.01.2022.
//

namespace AdaptySDK
{
public static partial class Adapty
{
internal class Result<T>
{
public readonly Error Error;
public readonly T Value;

public override string ToString()
{
return $"{nameof(Value)}: {Value}, " +
$"{nameof(Error)}: {Error}";
}

internal Result(T value, Error error)
{
Error = error;
Value = value;
}
}
}
}

11 changes: 11 additions & 0 deletions Assets/AdaptySDK/Models/Result.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1eac30a

Please sign in to comment.