I am trying to address Unity Authentication Error codes using a switch statement to direct which codes require which action, e.g:
switch (AuthenticationException.ErrorCode)
{
case AuthenticationErrorCodes.BannedUser:
MethodOne();
break;
case AuthenticationErrorCodes.ClientNoActiveSession:
case AuthenticationErrorCodes.InvalidSessionToken:
case AuthenticationErrorCodes.ClientInvalidUserState:
MethodTwo();
break;
default…
But when I try this I get an error because “A constant value is expected”.
Does anyone know how to resolve this? Do I have to change to if & else if statements instead? I’d prefer not to if I don’t have to.
It doesn’t work because the AuthenticationErrorCodes class is a static class with static readonly fields. These are not “available” as constant values, don’t ask for details because I don’t know it as well ;).
Maybe this helps: Switch case in C# - a constant value is expected - Stack Overflow
An alternative to checking the error code is to use a try/catch block to catch a specific exception type or to check if the exception is of that exception type.
Thanks for the info. I think I’ll have to try if/elseif statements to replace the switch logic and see how it goes. Querying the ExceptionType is not the problem. I can catch AuthenticationException - but my requirement is to treat the outcome of an AuthenticationException differently based on the ErrorCode. For example, if I get ErrorCode of “BannedUser” I need to be able to explain that differently to the user vs a different error code.