private async Task <Exception> HandleError(HttpResponseMessage response) { var responseJson = await response.Content.ReadAsStringAsync(); JsonSerializerSettings settings = new JsonSerializerSettings() { ContractResolver = new LowercaseContractResolver() }; if (response.StatusCode == HttpStatusCode.Unauthorized) { if (App.Current.MainPage.GetType() != typeof(LoginView)) { await App.Current.MainPage.DisplayAlert("Security Issue", "Invalid session, please login again.", "Ok"); App.Current.MainPage = new LoginView(); } } if (response.StatusCode == HttpStatusCode.Forbidden) { throw new SecurityException("Email already registered, request Reset password or use different email."); } WebAPIException logData = JsonConvert.DeserializeObject <WebAPIException>(responseJson, settings); throw new Exception(logData.Details); }
public override IEnumerator OnAlert(Exception ex) { WebAPIException exception = ex as WebAPIException; APIAlert apiAlert = new APIAlert(); if (!exception.IsBackTopScreenError()) { bool isDisplayRetryButton = this.isRequestRetry; if (APIUtil.Instance().alertOnlyCloseButton) { isDisplayRetryButton = false; } apiAlert.NetworkAPIError(exception, isDisplayRetryButton); while (apiAlert.IsOpen()) { yield return(null); } if (apiAlert.SelectedRetryButton) { this.requestStatus = WebAPI.RequestStatus.RETRY; this.afterAlertClosedBehavior = TaskBase.AfterAlertClosed.RETRY; } else { this.afterAlertClosedBehavior = TaskBase.AfterAlertClosed.RETURN; } yield break; } apiAlert.NetworkAPIException(exception); for (;;) { yield return(null); } }
public void NetworkAPIException(WebAPIException error) { if (Loading.IsShow()) { Loading.Invisible(); } if (error.responseDataError == null) { this.SetJsonParseExceptionMessage(error); this.ShowJsonParseError(); } else { WWWResponse.VenusStatus venus_status = (WWWResponse.VenusStatus)error.responseDataError.venus_status; if (venus_status != WWWResponse.VenusStatus.RESPONSE_MAINTENANCE) { if (venus_status != WWWResponse.VenusStatus.RESPONSE_OLDVERSION) { this.SetJsonParseExceptionMessage(error); this.ShowJsonParseError(); } else { this.ShowVersionError(error.responseDataError); } } else { this.ShowServerMaintenance(error.responseDataError.message); } } }
public IEnumerator OAuthLogin(Action <string> onCompleted) { string result = null; Action onSuccess = delegate() { result = "Success"; }; WebAPIException exception = null; Action <WebAPIException> onException = delegate(WebAPIException ex) { exception = ex; result = "Failed"; }; this.AuthStart(onSuccess, onException); while (string.IsNullOrEmpty(result)) { yield return(null); } if ("Success" == result) { onCompleted(result); } else if (exception != null) { APIAlert apialert = new APIAlert(); if (!exception.IsBackTopScreenError()) { RestrictionInput.SuspensionLoad(); apialert.NetworkAPIError(exception, true); apialert.ClosedAction = delegate() { RestrictionInput.ResumeLoad(); onCompleted(result); }; } else { apialert.NetworkAPIException(exception); } } else { string @string = StringMaster.GetString("AuthFailedTitle"); string string2 = StringMaster.GetString("AuthFailedInfo"); Action <int> action = delegate(int nop) { RestrictionInput.ResumeLoad(); onCompleted(result); }; RestrictionInput.SuspensionLoad(); AlertManager.ShowAlertDialog(action, @string, string2, AlertManager.ButtonActionType.Retry, false); } yield break; }
public static async Task <WebAPIResult> ExecuteWebAPIRequest(HttpMethod httpMethod, string URL, string headerKeyName, string headerKeyValue, string contentBody = "", bool generateException = true) { var result = new WebAPIResult(); // Create the Http Request Message using (var request = new HttpRequestMessage(httpMethod, URL)) { // Add the access token and specify that the return value is required in JSON format request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // For API Key based auth request.Headers.Add(headerKeyName, headerKeyValue); //If the contentBody != "", attach it if (contentBody != "") { request.Content = new StringContent(contentBody, Encoding.UTF8, "application/json"); request.Content.Headers.ContentLength = contentBody.Length; } using (var response = await HTTPConnection.SendAsync(request)) { result.ReponseCode = response.StatusCode; result.ReponseContent = await response.Content.ReadAsStringAsync(); } //Check to see if there are exceptions thrown from the Web API, thrown an exception //if (result.ReponseCode == HttpStatusCode.OK) return result; if (generateException && result.ReponseCode != HttpStatusCode.OK) { // Get the error details from the SAP message JObject errorInfo = JObject.Parse(result.ReponseContent); string errorCode = (string)errorInfo["error"]["statusCode"]; string errorMessage = (string)errorInfo["error"]["message"]; var error = new WebAPIException(errorCode, errorMessage); throw error; } else { return(result); } } }
public void NetworkAPIError(WebAPIException error, bool isRetry) { string errorCode = string.Empty; string message = string.Empty; if (error.localErrorStatus != WWWResponse.LocalErrorStatus.NONE) { this.SetJsonParseExceptionMessage(error); errorCode = AlertManager.GetErrorCode(error.localErrorStatus); } else { errorCode = error.responseDataError.subject; message = error.responseDataError.message; } this.SelectedRetryButton = false; this.Open(errorCode, StringMaster.GetString("AlertDataErrorTitle"), message, isRetry, new Action <int>(this.OnClosed), null); }
private async Task HandleError(HttpResponseMessage response) { JsonSerializerSettings settings = new JsonSerializerSettings() { ContractResolver = new LowercaseContractResolver() }; if (response.StatusCode == HttpStatusCode.Forbidden) { throw new SecurityException("Forbidden"); } var responseJson = await response.Content.ReadAsStringAsync(); WebAPIException logData = JsonConvert.DeserializeObject <WebAPIException>(responseJson, settings); throw new Exception(logData.Details); }
public static async Task <WebAPIResult> ExecuteOAuthWebAPIRequest(HttpMethod httpMethod, string URL, string authBearerToken, string contentBody = "", bool generateException = true, string headerKeyName = null, string headerKeyValue = null) { //LoggerService.Debug("Sending request to API for URI: " + URL); var result = new WebAPIResult(); // Create the Http Request Message using (var request = new HttpRequestMessage(httpMethod, URL)) { // Add the access token and specify that the return value is required in JSON format request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // For OAuth2, Bearer Token based APIs request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authBearerToken); if (!string.IsNullOrEmpty(headerKeyName) && !string.IsNullOrEmpty(headerKeyValue)) { request.Headers.Add(headerKeyName, headerKeyValue); } //If the contentBody != "", attach it if (contentBody != "") { request.Content = new StringContent(contentBody, Encoding.UTF8, "application/json"); request.Content.Headers.ContentLength = contentBody.Length; } using (var response = await HTTPConnection.SendAsync(request)) { //LoggerService.Debug("Response Status: " + response.StatusCode); result.ReponseCode = response.StatusCode; result.ReponseContent = await response.Content.ReadAsStringAsync(); } //Check to see if there are exceptions thrown from the Web API, thrown an exception if (generateException && (result.ReponseCode != HttpStatusCode.OK) && (result.ReponseCode != HttpStatusCode.Created) && (result.ReponseCode != HttpStatusCode.NoContent)) { if (!string.IsNullOrEmpty(result.ReponseContent)) { //Default Values string errorCode = result.ReponseCode.ToString(); string errorMessage = result.ReponseContent; //Body may not be a valid json try { // Get the error details from the SAP message JObject errorInfo = JObject.Parse(result.ReponseContent); errorCode = (string)errorInfo["error"]["code"]; errorMessage = (string)errorInfo["error"]["message"]; } catch { } var error = new WebAPIException(errorCode, errorMessage); throw error; } else { throw new WebAPIException(result.ReponseCode.ToString(), result.ReponseCode.ToString()); } } } return(result); }
public override bool IsBackTopScreen(Exception ex) { WebAPIException ex2 = ex as WebAPIException; return(ex2.IsBackTopScreenError()); }
private void SetJsonParseExceptionMessage(WebAPIException error) { }