public static TResultType HandleResults(CallRequestContainer callRequest, Delegate resultCallback, ErrorCallback errorCallback, Action <TResultType, CallRequestContainer> resultAction) { if (callRequest.Error == null) // Some other error earlier in the process, just report it below { try { ResultContainer <TResultType> resultEnvelope = new ResultContainer <TResultType>(); JsonConvert.PopulateObject(callRequest.ResultStr, resultEnvelope, Util.JsonSettings); if (!resultEnvelope.errorCode.HasValue || resultEnvelope.errorCode.Value == (int)PlayFabErrorCode.Success) { resultEnvelope.data.Request = callRequest.Request; resultEnvelope.data.CustomData = callRequest.CustomData; if (resultAction != null) { resultAction(resultEnvelope.data, callRequest); } if (resultCallback != null) { _invokeParams[0] = resultEnvelope.data; resultCallback.DynamicInvoke(_invokeParams); } PlayFabSettings.InvokeResponse(callRequest.Url, callRequest.CallId, callRequest.Request, resultEnvelope.data, callRequest.Error, callRequest.CustomData); // Do the globalMessage callback return(resultEnvelope.data); // This is the expected output path for successful api call } // Successful HTTP interaction, but PlayFab server returned an error callRequest.Error = new PlayFabError { HttpCode = resultEnvelope.code, HttpStatus = resultEnvelope.status, Error = (PlayFabErrorCode)resultEnvelope.errorCode.Value, ErrorMessage = resultEnvelope.errorMessage, ErrorDetails = resultEnvelope.errorDetails }; } catch (Exception e) { // Failed to decode the result callRequest.Error = new PlayFabError { HttpCode = (int)HttpStatusCode.OK, // Technically the server returned a result, the sdk just didn't parse it correctly HttpStatus = "Client failed to parse response from server", Error = PlayFabErrorCode.Unknown, ErrorMessage = e.ToString(), ErrorDetails = null }; } } if (errorCallback != null) { errorCallback(callRequest.Error); } if (PlayFabSettings.GlobalErrorHandler != null) { PlayFabSettings.GlobalErrorHandler(callRequest.Error); } return(null); }
public static void HandleResults(string responseStr, ref PlayFabError pfError, out ResultType result) { result = null; if (pfError != null) { if (PlayFabSettings.GlobalErrorHandler != null) { PlayFabSettings.GlobalErrorHandler(pfError); } return; } ResultContainer <ResultType> resultEnvelope = new ResultContainer <ResultType>(); try { JsonConvert.PopulateObject(responseStr, resultEnvelope, Util.JsonSettings); } catch (Exception e) { pfError = new PlayFabError(); pfError.HttpCode = (int)HttpStatusCode.OK; // Technically we did get a result from the server pfError.HttpStatus = "Client failed to parse response from server"; pfError.Error = PlayFabErrorCode.Unknown; pfError.ErrorMessage = e.ToString(); pfError.ErrorDetails = null; if (PlayFabSettings.GlobalErrorHandler != null) { PlayFabSettings.GlobalErrorHandler(pfError); } return; } if (resultEnvelope.errorCode.HasValue) { PlayFabErrorCode errorEnum; try { errorEnum = (PlayFabErrorCode)resultEnvelope.errorCode.Value; } catch { errorEnum = PlayFabErrorCode.Unknown; } pfError = new PlayFabError { HttpCode = resultEnvelope.code, HttpStatus = resultEnvelope.status, Error = errorEnum, ErrorMessage = resultEnvelope.errorMessage, ErrorDetails = resultEnvelope.errorDetails }; if (PlayFabSettings.GlobalErrorHandler != null) { PlayFabSettings.GlobalErrorHandler(pfError); } return; } result = resultEnvelope.data; }
public static void HandleResults(string responseStr, string errorStr, out ResultType result, out PlayFabError error) { result = null; error = null; if (errorStr != null) { error = new PlayFabError(); if (PlayFabSettings.GlobalErrorHandler != null) { PlayFabSettings.GlobalErrorHandler(error); } return; } ResultContainer <ResultType> resultEnvelope = new ResultContainer <ResultType>(); try { JsonConvert.PopulateObject(responseStr, resultEnvelope, Util.JsonSettings); } catch (Exception e) { error = new PlayFabError(); error.Error = PlayFabErrorCode.Unknown; error.ErrorMessage = e.ToString(); if (PlayFabSettings.GlobalErrorHandler != null) { PlayFabSettings.GlobalErrorHandler(error); } return; } if (resultEnvelope.errorCode.HasValue) { PlayFabErrorCode errorEnum; try { errorEnum = (PlayFabErrorCode)resultEnvelope.errorCode.Value; } catch { errorEnum = PlayFabErrorCode.Unknown; } error = new PlayFabError { HttpCode = resultEnvelope.code, HttpStatus = resultEnvelope.status, Error = errorEnum, ErrorMessage = resultEnvelope.errorMessage, ErrorDetails = resultEnvelope.errorDetails }; if (PlayFabSettings.GlobalErrorHandler != null) { PlayFabSettings.GlobalErrorHandler(error); } return; } result = resultEnvelope.data; }
public static void HandleResults(string responseStr, string errorStr, out ResultType result, out PlayFabError error) { result = null; error = null; if (errorStr != null) { error = new PlayFabError(); if (PlayFabSettings.GlobalErrorHandler != null) { PlayFabSettings.GlobalErrorHandler(error); } return; } ResultType parsedResult = null; Dictionary <String, object> rawResultEnvelope = null; try { rawResultEnvelope = (Dictionary <String, object>)JsonReader.Deserialize(responseStr, Util.GlobalJsonReaderSettings); if (rawResultEnvelope.ContainsKey("data")) { Dictionary <String, object> rawResult = (Dictionary <String, object>)rawResultEnvelope["data"]; parsedResult = new ResultType(); parsedResult.Deserialize(rawResult); } } catch (Exception e) { error = new PlayFabError(); error.Error = PlayFabErrorCode.Unknown; error.ErrorMessage = e.ToString(); if (PlayFabSettings.GlobalErrorHandler != null) { PlayFabSettings.GlobalErrorHandler(error); } return; } if (rawResultEnvelope.ContainsKey("errorCode")) { PlayFabErrorCode errorEnum; try { errorEnum = (PlayFabErrorCode)(int)(double)rawResultEnvelope["errorCode"]; } catch { errorEnum = PlayFabErrorCode.Unknown; } Dictionary <string, List <string> > errorDetails = null; if (rawResultEnvelope.ContainsKey("errorDetails")) { Dictionary <string, object> rawErrorDetails = (Dictionary <string, object>)rawResultEnvelope["errorDetails"]; errorDetails = new Dictionary <string, List <string> > (); foreach (string key in rawErrorDetails.Keys) { object[] keyErrors = (object[])rawErrorDetails[key]; List <string> errorList = new List <string>(); for (int i = 0; i < keyErrors.Length; i++) { errorList.Add((string)keyErrors[i]); } errorDetails.Add(key, errorList); } } error = new PlayFabError { HttpCode = (int)(double)rawResultEnvelope["code"], HttpStatus = (string)rawResultEnvelope["status"], Error = errorEnum, ErrorMessage = (string)rawResultEnvelope["errorMessage"], ErrorDetails = errorDetails }; if (PlayFabSettings.GlobalErrorHandler != null) { PlayFabSettings.GlobalErrorHandler(error); } return; } result = parsedResult; }