diff --git a/Assets/Talo Game Services/Talo/Runtime/APIs/BaseAPI.cs b/Assets/Talo Game Services/Talo/Runtime/APIs/BaseAPI.cs index 7470e07..6d36794 100644 --- a/Assets/Talo Game Services/Talo/Runtime/APIs/BaseAPI.cs +++ b/Assets/Talo Game Services/Talo/Runtime/APIs/BaseAPI.cs @@ -145,7 +145,14 @@ protected async Task Call( return await Call(uri, method, content, headers, continuity); } - throw new PlayerAuthException(errorCode, new Exception(message)); + if (uri.AbsolutePath.Contains("/v1/players/auth/")) + { + throw new PlayerAuthException(errorCode, new Exception(message)); + } + else + { + throw new RequestException(www.responseCode, new Exception(message), www.downloadHandler.text); + } } } } diff --git a/Assets/Talo Game Services/Talo/Runtime/APIs/PlayersAPI.cs b/Assets/Talo Game Services/Talo/Runtime/APIs/PlayersAPI.cs index b6bbc2a..4e14c7c 100644 --- a/Assets/Talo Game Services/Talo/Runtime/APIs/PlayersAPI.cs +++ b/Assets/Talo Game Services/Talo/Runtime/APIs/PlayersAPI.cs @@ -18,7 +18,7 @@ public enum DebouncedOperation public event Action OnIdentified; public event Action OnIdentificationStarted; - public event Action OnIdentificationFailed; + public event Action OnIdentificationFailed; public event Action OnIdentityCleared; public event Action OnPropsRejected; @@ -75,21 +75,21 @@ public async Task Identify(string service, string identifier) var uri = new Uri($"{baseUrl}/identify?service={service}&identifier={identifier}"); + PlayersIdentifyResponse res; try { var json = await Call(uri, "GET"); - - var res = JsonUtility.FromJson(json); - var alias = res.alias; - alias.WriteOfflineAlias(); - return await HandleIdentifySuccess(alias, res.socketToken); + res = JsonUtility.FromJson(json); } - catch + catch (Exception ex) { await Talo.PlayerAuth.SessionManager.ClearSession(); - OnIdentificationFailed?.Invoke(); + OnIdentificationFailed?.Invoke(IdentifyException.FromException(ex)); throw; } + + res.alias.WriteOfflineAlias(); + return await HandleIdentifySuccess(res.alias, res.socketToken); } public async Task IdentifySteam(string ticket, string identityClient = "") @@ -209,7 +209,7 @@ private async Task IdentifyOffline(string service, string identifie catch { PlayerAlias.DeleteOfflineAlias(); - OnIdentificationFailed?.Invoke(); + OnIdentificationFailed?.Invoke(new IdentifyException()); throw new Exception("Failed to parse offline player alias"); } @@ -218,7 +218,7 @@ private async Task IdentifyOffline(string service, string identifie return await HandleIdentifySuccess(offlineAlias); } - OnIdentificationFailed?.Invoke(); + OnIdentificationFailed?.Invoke(new IdentifyException()); throw new Exception("No offline player alias found"); } diff --git a/Assets/Talo Game Services/Talo/Runtime/Utils/IdentifyException.cs b/Assets/Talo Game Services/Talo/Runtime/Utils/IdentifyException.cs new file mode 100644 index 0000000..3e019d5 --- /dev/null +++ b/Assets/Talo Game Services/Talo/Runtime/Utils/IdentifyException.cs @@ -0,0 +1,62 @@ +using System; +using UnityEngine; + +namespace TaloGameServices +{ + public enum IdentifyErrorCode + { + UNKNOWN_ERROR, + IDENTIFIER_PROFANITY, + IDENTIFIER_TAKEN + } + + public class IdentifyException : Exception + { + public IdentifyErrorCode ErrorCode { get; } + + public IdentifyException(IdentifyErrorCode code = IdentifyErrorCode.UNKNOWN_ERROR) + : base(code.ToString()) + { + ErrorCode = code; + } + + public IdentifyException(IdentifyErrorCode code, Exception inner) + : base(code.ToString(), inner) + { + ErrorCode = code; + } + + public static IdentifyException FromException(Exception ex) + { + if (ex is RequestException re && !string.IsNullOrEmpty(re.responseBody)) + { + return FromResponse(re.responseBody); + } + + return new IdentifyException(); + } + + private static IdentifyException FromResponse(string body) + { + if (string.IsNullOrEmpty(body)) + { + return new IdentifyException(); + } + + try + { + var parsed = JsonUtility.FromJson(body); + if (parsed != null && !string.IsNullOrEmpty(parsed.errorCode) && + Enum.TryParse(parsed.errorCode, out IdentifyErrorCode code)) + { + return new IdentifyException(code); + } + } + catch + { + } + + return new IdentifyException(); + } + } +} diff --git a/Assets/Talo Game Services/Talo/Runtime/Utils/IdentifyException.cs.meta b/Assets/Talo Game Services/Talo/Runtime/Utils/IdentifyException.cs.meta new file mode 100644 index 0000000..bd2ff2d --- /dev/null +++ b/Assets/Talo Game Services/Talo/Runtime/Utils/IdentifyException.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: a6ca927edafd34429a5ae1699cca53b5 \ No newline at end of file