diff --git a/Assets/Samples/AuthenticationDemo/Scripts/LoginUIController.cs b/Assets/Samples/AuthenticationDemo/Scripts/LoginUIController.cs index 0d2c7ed..e037dda 100644 --- a/Assets/Samples/AuthenticationDemo/Scripts/LoginUIController.cs +++ b/Assets/Samples/AuthenticationDemo/Scripts/LoginUIController.cs @@ -52,7 +52,7 @@ private async void OnLoginClick() } catch (PlayerAuthException e) { - validationLabel.text = e.GetErrorCode() switch + validationLabel.text = e.ErrorCode switch { PlayerAuthErrorCode.INVALID_CREDENTIALS => "Username or password is incorrect", _ => e.Message diff --git a/Assets/Samples/AuthenticationDemo/Scripts/RegisterUIController.cs b/Assets/Samples/AuthenticationDemo/Scripts/RegisterUIController.cs index 5f2f75a..a81b95c 100644 --- a/Assets/Samples/AuthenticationDemo/Scripts/RegisterUIController.cs +++ b/Assets/Samples/AuthenticationDemo/Scripts/RegisterUIController.cs @@ -48,7 +48,7 @@ private async void OnRegisterClick() } catch (PlayerAuthException e) { - validationLabel.text = e.GetErrorCode() switch + validationLabel.text = e.ErrorCode switch { PlayerAuthErrorCode.IDENTIFIER_TAKEN => "Username is already taken", PlayerAuthErrorCode.INVALID_EMAIL => "Invalid email address", diff --git a/Assets/Samples/AuthenticationDemo/Scripts/VerifyUIController.cs b/Assets/Samples/AuthenticationDemo/Scripts/VerifyUIController.cs index 4c2f793..07e9921 100644 --- a/Assets/Samples/AuthenticationDemo/Scripts/VerifyUIController.cs +++ b/Assets/Samples/AuthenticationDemo/Scripts/VerifyUIController.cs @@ -35,7 +35,7 @@ private async void OnSubmitClicked() } catch (PlayerAuthException e) { - validationLabel.text = e.GetErrorCode() switch + validationLabel.text = e.ErrorCode switch { PlayerAuthErrorCode.VERIFICATION_CODE_INVALID => "Verification code is incorrect", _ => e.Message diff --git a/Packages/com.trytalo.talo/Runtime/SocketResponses/SocketError.cs b/Packages/com.trytalo.talo/Runtime/SocketResponses/SocketError.cs index 79a62ce..d99cb39 100644 --- a/Packages/com.trytalo.talo/Runtime/SocketResponses/SocketError.cs +++ b/Packages/com.trytalo.talo/Runtime/SocketResponses/SocketError.cs @@ -7,5 +7,10 @@ public class SocketError public string message; public string errorCode; public string cause; + + public void Throw() + { + throw new SocketException(this); + } } } diff --git a/Packages/com.trytalo.talo/Runtime/TaloSocket.cs b/Packages/com.trytalo.talo/Runtime/TaloSocket.cs index d712df9..84abca6 100644 --- a/Packages/com.trytalo.talo/Runtime/TaloSocket.cs +++ b/Packages/com.trytalo.talo/Runtime/TaloSocket.cs @@ -9,6 +9,7 @@ public class TaloSocket : MonoBehaviour { public event Action OnMessageReceived; public event Action OnConnectionClosed; + public event Action OnErrorReceived; private WebSocketConnection socket; private string tempSocketToken; @@ -56,8 +57,8 @@ private void HandleMessage(WebSocketConnection connection, WebSocketMessage wsm) tempSocketToken = ""; break; case "v1.error": - var error = response.GetData(); - throw new Exception($"Socket error: {error.req} - {error.errorCode}{(string.IsNullOrEmpty(error.cause) ? "" : " - " + error.cause)}"); + OnErrorReceived?.Invoke(response.GetData()); + break; } } diff --git a/Packages/com.trytalo.talo/Runtime/Utils/PlayerAuthException.cs b/Packages/com.trytalo.talo/Runtime/Utils/PlayerAuthException.cs index 6467b4c..47e7f6d 100644 --- a/Packages/com.trytalo.talo/Runtime/Utils/PlayerAuthException.cs +++ b/Packages/com.trytalo.talo/Runtime/Utils/PlayerAuthException.cs @@ -3,6 +3,7 @@ namespace TaloGameServices { public enum PlayerAuthErrorCode { + API_ERROR, INVALID_CREDENTIALS, VERIFICATION_ALIAS_NOT_FOUND, VERIFICATION_CODE_INVALID, @@ -18,6 +19,8 @@ public enum PlayerAuthErrorCode { public class PlayerAuthException : Exception { + public PlayerAuthErrorCode ErrorCode => GetErrorCode(); + public PlayerAuthException() { } @@ -32,9 +35,10 @@ public PlayerAuthException(string errorCode, Exception inner) { } - public PlayerAuthErrorCode GetErrorCode() + private PlayerAuthErrorCode GetErrorCode() { - return (PlayerAuthErrorCode)Enum.Parse(typeof(PlayerAuthErrorCode), Message); + var errorCode = string.IsNullOrEmpty(Message) ? "API_ERROR" : Message; + return (PlayerAuthErrorCode)Enum.Parse(typeof(PlayerAuthErrorCode), errorCode); } } } diff --git a/Packages/com.trytalo.talo/Runtime/Utils/SocketException.cs b/Packages/com.trytalo.talo/Runtime/Utils/SocketException.cs new file mode 100644 index 0000000..6241aa6 --- /dev/null +++ b/Packages/com.trytalo.talo/Runtime/Utils/SocketException.cs @@ -0,0 +1,49 @@ +using System; + +namespace TaloGameServices +{ + public enum SocketErrorCode { + API_ERROR, + INVALID_MESSAGE, + INVALID_MESSAGE_DATA, + NO_PLAYER_FOUND, + UNHANDLED_REQUEST, + ROUTING_ERROR, + LISTENER_ERROR, + INVALID_SOCKET_TOKEN, + INVALID_SESSION_TOKEN, + MISSING_ACCESS_KEY_SCOPES, + RATE_LIMIT_EXCEEDED + } + + public class SocketException : Exception + { + private SocketError errorData; + + public string Req => errorData?.req ?? "unknown"; + public SocketErrorCode ErrorCode => GetErrorCode(); + public string Cause => errorData?.cause ?? ""; + + public SocketException() + { + } + + public SocketException(SocketError errorData) + : base(errorData.message) + { + this.errorData = errorData; + } + + public SocketException(SocketError errorData, Exception inner) + : base(errorData.message, inner) + { + this.errorData = errorData; + } + + private SocketErrorCode GetErrorCode() + { + var errorCode = string.IsNullOrEmpty(errorData?.errorCode) ? "API_ERROR" : errorData.errorCode; + return (SocketErrorCode)Enum.Parse(typeof(SocketErrorCode), errorCode); + } + } +} diff --git a/Packages/com.trytalo.talo/Runtime/Utils/SocketException.cs.meta b/Packages/com.trytalo.talo/Runtime/Utils/SocketException.cs.meta new file mode 100644 index 0000000..daff351 --- /dev/null +++ b/Packages/com.trytalo.talo/Runtime/Utils/SocketException.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 64db8aa0a5f3b41339abaa00982036b0 \ No newline at end of file