Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ public class SocketError
public string message;
public string errorCode;
public string cause;

public void Throw()
{
throw new SocketException(this);
}
}
}
5 changes: 3 additions & 2 deletions Packages/com.trytalo.talo/Runtime/TaloSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class TaloSocket : MonoBehaviour
{
public event Action<SocketResponse> OnMessageReceived;
public event Action OnConnectionClosed;
public event Action<SocketError> OnErrorReceived;

private WebSocketConnection socket;
private string tempSocketToken;
Expand Down Expand Up @@ -56,8 +57,8 @@ private void HandleMessage(WebSocketConnection connection, WebSocketMessage wsm)
tempSocketToken = "";
break;
case "v1.error":
var error = response.GetData<SocketError>();
throw new Exception($"Socket error: {error.req} - {error.errorCode}{(string.IsNullOrEmpty(error.cause) ? "" : " - " + error.cause)}");
OnErrorReceived?.Invoke(response.GetData<SocketError>());
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TaloGameServices
{
public enum PlayerAuthErrorCode {
API_ERROR,
INVALID_CREDENTIALS,
VERIFICATION_ALIAS_NOT_FOUND,
VERIFICATION_CODE_INVALID,
Expand All @@ -18,6 +19,8 @@ public enum PlayerAuthErrorCode {

public class PlayerAuthException : Exception
{
public PlayerAuthErrorCode ErrorCode => GetErrorCode();

public PlayerAuthException()
{
}
Expand All @@ -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);
}
}
}
49 changes: 49 additions & 0 deletions Packages/com.trytalo.talo/Runtime/Utils/SocketException.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

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