Skip to content

Commit e0f9d45

Browse files
authored
Merge pull request #111 from TaloDev/socket-errors
Add OnErrorReceived signal to Talo socket
2 parents dc4bb1b + 79a0e80 commit e0f9d45

File tree

8 files changed

+68
-7
lines changed

8 files changed

+68
-7
lines changed

Assets/Samples/AuthenticationDemo/Scripts/LoginUIController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private async void OnLoginClick()
5252
}
5353
catch (PlayerAuthException e)
5454
{
55-
validationLabel.text = e.GetErrorCode() switch
55+
validationLabel.text = e.ErrorCode switch
5656
{
5757
PlayerAuthErrorCode.INVALID_CREDENTIALS => "Username or password is incorrect",
5858
_ => e.Message

Assets/Samples/AuthenticationDemo/Scripts/RegisterUIController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private async void OnRegisterClick()
4848
}
4949
catch (PlayerAuthException e)
5050
{
51-
validationLabel.text = e.GetErrorCode() switch
51+
validationLabel.text = e.ErrorCode switch
5252
{
5353
PlayerAuthErrorCode.IDENTIFIER_TAKEN => "Username is already taken",
5454
PlayerAuthErrorCode.INVALID_EMAIL => "Invalid email address",

Assets/Samples/AuthenticationDemo/Scripts/VerifyUIController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private async void OnSubmitClicked()
3535
}
3636
catch (PlayerAuthException e)
3737
{
38-
validationLabel.text = e.GetErrorCode() switch
38+
validationLabel.text = e.ErrorCode switch
3939
{
4040
PlayerAuthErrorCode.VERIFICATION_CODE_INVALID => "Verification code is incorrect",
4141
_ => e.Message

Packages/com.trytalo.talo/Runtime/SocketResponses/SocketError.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ public class SocketError
77
public string message;
88
public string errorCode;
99
public string cause;
10+
11+
public void Throw()
12+
{
13+
throw new SocketException(this);
14+
}
1015
}
1116
}

Packages/com.trytalo.talo/Runtime/TaloSocket.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class TaloSocket : MonoBehaviour
99
{
1010
public event Action<SocketResponse> OnMessageReceived;
1111
public event Action OnConnectionClosed;
12+
public event Action<SocketError> OnErrorReceived;
1213

1314
private WebSocketConnection socket;
1415
private string tempSocketToken;
@@ -56,8 +57,8 @@ private void HandleMessage(WebSocketConnection connection, WebSocketMessage wsm)
5657
tempSocketToken = "";
5758
break;
5859
case "v1.error":
59-
var error = response.GetData<SocketError>();
60-
throw new Exception($"Socket error: {error.req} - {error.errorCode}{(string.IsNullOrEmpty(error.cause) ? "" : " - " + error.cause)}");
60+
OnErrorReceived?.Invoke(response.GetData<SocketError>());
61+
break;
6162
}
6263
}
6364

Packages/com.trytalo.talo/Runtime/Utils/PlayerAuthException.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace TaloGameServices
44
{
55
public enum PlayerAuthErrorCode {
6+
API_ERROR,
67
INVALID_CREDENTIALS,
78
VERIFICATION_ALIAS_NOT_FOUND,
89
VERIFICATION_CODE_INVALID,
@@ -18,6 +19,8 @@ public enum PlayerAuthErrorCode {
1819

1920
public class PlayerAuthException : Exception
2021
{
22+
public PlayerAuthErrorCode ErrorCode => GetErrorCode();
23+
2124
public PlayerAuthException()
2225
{
2326
}
@@ -32,9 +35,10 @@ public PlayerAuthException(string errorCode, Exception inner)
3235
{
3336
}
3437

35-
public PlayerAuthErrorCode GetErrorCode()
38+
private PlayerAuthErrorCode GetErrorCode()
3639
{
37-
return (PlayerAuthErrorCode)Enum.Parse(typeof(PlayerAuthErrorCode), Message);
40+
var errorCode = string.IsNullOrEmpty(Message) ? "API_ERROR" : Message;
41+
return (PlayerAuthErrorCode)Enum.Parse(typeof(PlayerAuthErrorCode), errorCode);
3842
}
3943
}
4044
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
3+
namespace TaloGameServices
4+
{
5+
public enum SocketErrorCode {
6+
API_ERROR,
7+
INVALID_MESSAGE,
8+
INVALID_MESSAGE_DATA,
9+
NO_PLAYER_FOUND,
10+
UNHANDLED_REQUEST,
11+
ROUTING_ERROR,
12+
LISTENER_ERROR,
13+
INVALID_SOCKET_TOKEN,
14+
INVALID_SESSION_TOKEN,
15+
MISSING_ACCESS_KEY_SCOPES,
16+
RATE_LIMIT_EXCEEDED
17+
}
18+
19+
public class SocketException : Exception
20+
{
21+
private SocketError errorData;
22+
23+
public string Req => errorData?.req ?? "unknown";
24+
public SocketErrorCode ErrorCode => GetErrorCode();
25+
public string Cause => errorData?.cause ?? "";
26+
27+
public SocketException()
28+
{
29+
}
30+
31+
public SocketException(SocketError errorData)
32+
: base(errorData.message)
33+
{
34+
this.errorData = errorData;
35+
}
36+
37+
public SocketException(SocketError errorData, Exception inner)
38+
: base(errorData.message, inner)
39+
{
40+
this.errorData = errorData;
41+
}
42+
43+
private SocketErrorCode GetErrorCode()
44+
{
45+
var errorCode = string.IsNullOrEmpty(errorData?.errorCode) ? "API_ERROR" : errorData.errorCode;
46+
return (SocketErrorCode)Enum.Parse(typeof(SocketErrorCode), errorCode);
47+
}
48+
}
49+
}

Packages/com.trytalo.talo/Runtime/Utils/SocketException.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)