/// <summary> /// Default bad response action /// </summary> protected virtual Task <T> DefaultBadResponseFunctionAsync <T>(HttpResponseMessage response) { var tcs = new TaskCompletionSource <T>(); ThreadPool.QueueUserWorkItem(_ => tcs.SetException(HttpClientException.FromResponse(response))); return(tcs.Task); }
public void Connections_CanConnectToServer_ThrowsException_ExpectErrorResult() { //------------Setup for test-------------------------- const string address = "http://localhost:3142/dsf"; var conn = new Connection { ResourceType = "Server", Address = address }; var hubFactory = new Mock <IHubFactory>(); var clientException = new HttpClientException(new HttpResponseMessage(HttpStatusCode.BadRequest)) { Response = { ReasonPhrase = "error" } }; hubFactory.Setup(factory => factory.CreateHubProxy(conn)).Throws(new Exception("", clientException)); var myConnections = new Connections(() => new List <string>(), hubFactory.Object); //------------Execute Test--------------------------- var canConnectToServer = myConnections.CanConnectToServer(conn); //------------Assert Results------------------------- Assert.IsFalse(canConnectToServer.IsValid); Assert.AreEqual("Connection Error : error", canConnectToServer.ErrorMessage); }
protected String handleErrors(HttpClientException e) { JsonObject response = e.getResponse(); if (response == null) { throw new S4ServiceClientException(e.Message, e); } return(response.ToString()); }
private static void HandleHttpClientException(HttpClientException exception) { System.Console.WriteLine("При обработке команды произошла ошибка:"); if (exception.ResponseStatusCode.HasValue && !TryHandleExceptionCode(exception.ResponseStatusCode.Value, exception.RequestPathAndQuery)) { HandleUnknownException(exception); } else { HandleUnknownException(exception); } }
private static void HandleHttpClientException(HttpClientException exception) { System.Console.WriteLine("При обработке команды произошла ошибка:"); if (exception.Status == WebExceptionStatus.ProtocolError && exception.ResponseStatusCode.HasValue && !TryHandleExceptionCode(exception.ResponseStatusCode.Value, exception.RequestPathAndQuery)) { HandleUnknownException(exception); } else if (exception.Status == WebExceptionStatus.ReceiveFailure) { System.Console.WriteLine("Ошибка подключения: Возможно, неправильные аутентификационные данные для прокси. Попробуйте изменить имя пользователя и пароль, используя команду proxy"); } else { HandleUnknownException(exception); } }
/// <summary> /// Creates mock test client errors /// </summary> /// <param name="status"></param> /// <param name="fileName"></param> /// <returns></returns> protected Client CreateMockTestClientForErrors(int status, string fileName) { var mockPath = GetMockFilePath(fileName); var response = MockFileToString(mockPath); //Create a mock interface var clientInterfaceMock = new Mock <IClient>(); var confMock = MockPaymentData.CreateConfingMock(); var httpClientException = new HttpClientException(status, "An error occured", new Dictionary <string, List <string> >(), response); clientInterfaceMock.Setup(x => x.Request(It.IsAny <string>(), It.IsAny <string>(), confMock)).Throws(httpClientException); var clientMock = new Client(It.IsAny <Config>()) { HttpClient = clientInterfaceMock.Object, Config = confMock }; return(clientMock); }
public virtual ErrorMessage GetErrorMessage(Exception e) { Exception error = ExtractFromAggregateException(e); ErrorMessage result = new ErrorMessage(); if (error is HttpClientException) { HttpClientException clientException = error as HttpClientException; ErrorInfo errorInfo = clientException.ErrorInfo; if (errorInfo?.Code == Constants.ADDIN_VERSION_LOGIN) { result.Message = Resources.Addin_Login_Vesrion_Error; } else if (errorInfo?.Code == Constants.ADDIN_VERSION_LOGOUT) { result.Message = Resources.Addin_Logout_Version_Error; } else { result.Message = $"{GetErrorMessage(clientException.StatusCode)}"; } Log.Debug($"HttpClientException reasonPhrase: {clientException.ReasonPhrase}"); } else if (error is WebException) { result.Message = GetErrorMessage(error as WebException); } else if (error is UnauthorizedAccessException) { result.Message = "You don't have access permission to this item."; } else { result.Message = error.Message; } result.Details = error.ToString(); return(result); }
private void processEx(String content, HttpClientException ex) { //{"error":"invalid_access_token","error_code":21332,"request":"/2/statuses/update.json"} //{"error":"expired_token","error_code":21327,"request":"/2/statuses/update.json"} // 原因不明,请查看已经记录的日志,此处不处理 if (ex.ErrorInfo == null) { return; } if (ex.ErrorInfo.IndexOf("invalid_access_token") >= 0) { refreshToken(); republishPost(content); return; } // token过期处理 if (ex.ErrorInfo.IndexOf("expired_token") >= 0) { } }
public virtual void ProcessError(Exception error, bool showError = true) { Exception innerError = ExtractFromAggregateException(error); if (IsCancelation(innerError) || innerError == null) { return; } innerError.GenerateReferenceId(); Log.Debug(GetErrorMessage(innerError).Message); Log.Error(innerError); if (innerError is HttpClientException) { HttpClientException httpException = innerError as HttpClientException; ErrorInfo errorInfo = httpException.ErrorInfo; if (httpException.StatusCode == HttpStatusCode.Unauthorized) { if (!IsLoogedIn) { return; } LogoutAction(); if (!showError) { return; } using (MessageDialog dlgResult = CreateMessageDialog()) { dlgResult.Title = Resources.Session_Expired; dlgResult.Messsage = GetErrorMessage(httpException).Message; dlgResult.OkText = "Login"; dlgResult.AllowReporting = false; if (dlgResult.ShowDialog() == DialogResult.OK) { LoginAction(); } } return; } if (httpException.StatusCode == HttpStatusCode.Forbidden) { if (errorInfo?.Code == Constants.ADDIN_VERSION_LOGIN || errorInfo?.Code == Constants.ADDIN_VERSION_LOGOUT) { if (!IsLoogedIn) { return; } LogoutAction(); } if (!showError) { return; } using (MessageDialog dialog = CreateMessageDialog()) { dialog.Title = httpException.StatusCode.ToString(); dialog.Messsage = GetErrorMessage(httpException).Message; dialog.ShowCancel = false; dialog.OkText = Resources.Ok; dialog.AllowReporting = true; dialog.ShowHandledDialog(); } return; } if (httpException.StatusCode == HttpStatusCode.BadRequest) { if (!showError) { return; } using (MessageDialog dialog = CreateMessageDialog()) { dialog.Title = httpException.StatusCode.ToString(); dialog.Messsage = GetErrorMessage(httpException).Message; dialog.ShowCancel = false; dialog.OkText = Resources.Ok; dialog.AllowReporting = true; dialog.ShowHandledDialog(); } return; } if (httpException.StatusCode == HttpStatusCode.ServiceUnavailable) { if (!showError) { return; } using (MessageDialog dialog = CreateMessageDialog()) { dialog.Title = httpException.StatusCode.ToString(); dialog.Messsage = GetErrorMessage(httpException).Message; dialog.ShowCancel = false; dialog.OkText = Resources.Ok; dialog.AllowReporting = true; dialog.ShowHandledDialog(); } return; } } if (innerError is HttpRequestException) { if (innerError.InnerException is WebException) { if (!showError) { return; } using (MessageDialog dialog = CreateMessageDialog()) { dialog.Title = Resources.Network_Error; dialog.Messsage = GetErrorMessage((WebException)innerError.InnerException); dialog.ShowCancel = false; dialog.OkText = Resources.Ok; dialog.AllowReporting = true; dialog.ShowHandledDialog(); } return; } } if (innerError is IeVersionException) { if (!showError) { return; } using (MessageDialog dialog = CreateMessageDialog()) { dialog.Title = "Error"; dialog.Messsage = Resources.To_use_addin_please_install_IE_with_version_10_or_upper; dialog.ShowCancel = false; dialog.OkText = Resources.Ok; dialog.AllowReporting = true; dialog.ShowHandledDialog(); } return; } if (!showError) { return; } using (MessageErrorDialog dlg = CreateMessageErrorDialog()) { var info = GetErrorMessage(error); dlg.Message = info.Message; dlg.MessageDetails = info.Details; dlg.AllowReporting = true; dlg.ShowDialog(); } }
public void LogWebError(Exception e, HttpClientException f) { errorLogHubProxy.Invoke("LogWebError", e); }