Hey all,
I’m currently polishing off a game that we’re about to release the web-build for, and i’ve been trying to get the game to work nicely with the error reporting disabled (for the extra performance). There is one remaining issue, that I will likely have to implement a work-around for.
private IEnumerator handleRequest(WWW www, Request request) {
yield return www;
Action onComplete = request.getOnComplete();
Action<JsonData> onSuccess = request.getOnSuccess();
Action<string> onError = request.getOnError();
try {
if(!string.IsNullOrEmpty(www.error)) {
if (www.responseHeaders != null && www.responseHeaders.ContainsKey("STATUS")) {
//extract the error from the status
string[] parts = www.responseHeaders["STATUS"].Split(' ');
if (parts.Length < 3) {
throw new Exception(www.error);
} else {
string error = "";
for (int i = 2; i < parts.Length; i++) {
error += parts[i] + " ";
}
throw new Exception(error);
}
} else {
throw new Exception(www.error);
}
} else {
JsonData data = JsonMapper.ToObject(www.text);
if ((bool)data["error"] == true) {
throw new Exception((string)data["error_message"]);
} else {
if (onSuccess != null) {
onSuccess(data);
}
}
}
} catch(Exception ex) {
if (onError != null) {
onError(ex.Message);
} else {
Debug.LogErrorFormat("ERROR ({0}): {1}", www.url, ex.ToString());
}
}
this.processingRequest = false;
if(onComplete != null) {
onComplete();
}
}
In this code, the internal processing of the first line yield return www; throws an uncaught exception in webgl builds when the server responds with something other than a 200 OK response. In other platforms (tested in standalone builds and android), no exception is thrown, and I am able to handle non-200 responses in the later lines of that code. The same happens when UnityWebRequest is used, and yield return www is replaced with the UnityWebRequest.Send method.
So the issue here is because it causes an un-catchable exception:
- i can’t catch the exception, because it is handled in the internal co-routine procedure and you can’t put a “yield return X” within a try/catch block
- i can’t handle a non-200 response in the later lines of the above code
- it causes the error popup in webgl builds saying that there is an error and errors are disabled, and causes the game to stop running
It’s very common for apis to not respond with 200 and for this to be something that you want to handle. For example, responding with 401 and knowing to notify the user that they need to authenticate. So it’s pretty important that we’re able to handle non-200 responses in webgl builds, without it causing the above issue.
Let me know if you need further information
