Success and Failure Callbacks¶
Since most of the functions in the Strix Unity SDK are network-based, the results usually aren’t available straight away. Because of this, the functions are made asynchronous. The results and errors are not passed as return values but utilize callbacks instead.
Usually there are two callbacks: one for successful completion and one for failures. You can use the second one to know if the action couldn’t be completed successfully.
Let’s see an example:
void JoinRoom(RoomJoinArgs args, RoomJoinEventHandler handler, FailureEventHandler failureHandler, RequestConfig config = null)
The third argument here is the failure handler we’re looking for.
Most of failure callbacks like these are delegates of type FailureEventHandler
defined as follows:
delegate void FailureEventHandler(FailureEventArgs args);
And here is an example of how it is used:
StrixNetwork.instance.JoinRoom(roomJoinArgs, handler: null, failureHandler: failureArgs => {
});
The argument passed to the callback has the following definition:
public class FailureEventArgs {
public AbstractSession session { get; set; }
public Exception cause { get; set; }
}
As you can see, there are two properties: session
and cause
.
The first one is a reference to the session in which the error occurred.
Usually, it will be the current room session.
The second property is the most useful here, as it lets you know the reason why the failure occurred.
You can print it to console to get more information.
Or you can try downcasting it to ErrorCodeException
, as this will be the most common type of exception,
and then get an error code out of it.
ErrorCodeException
is defined as follows:
public class ErrorCodeException : Exception {
public int errorCode { get; set; }
public override string Message { get; }
}
You can see an error code is defined as an int
field.
Refer to this section for information on different error codes.
Alternatively, you can get a short description of the error as a string using the Message
property.
Here’s an example of how it could be handled:
StrixNetwork.instance.JoinRoom(roomJoinArgs, handler: null, failureHandler: failureArgs => {
var errorCodeException = failureArgs.cause as ErrorCodeException;
// If the exception that caused the failure is an ErrorCodeException, we can analyze the error code
if (errorCodeException != null) {
// If we know the exact error we're looking for, we can refer to the table of error codes and compare it directly to execute custom error handling logic
if (errorCodeException.errorCode == SoftGear.Strix.Client.Room.Error.RoomErrorCode.RoomNotFound)
{
...
// Otherwise, we can print the description of the error to the console
} else {
Debug.Log("Room search failed. Error: " + errorCodeException.Message
}
}
});