public static EIMException CreateException(Exception ex) { EIMException eimException = null; if (ex is WebServiceException) { WebServiceException webServiceException = ex as WebServiceException; Type exceptionType = Type.GetType(webServiceException.ErrorCode); eimException = Activator.CreateInstance(exceptionType, webServiceException.ErrorMessage) as EIMException; return(eimException); } if (eimException) { return(eimException); } }
public void Can_retrieve_empty_Errors_from_Dto_NoStatusResponse() { var webEx = new WebServiceException { ResponseDto = new NoStatusResponse() }; Assert.That(webEx.ErrorCode, Is.Null); Assert.That(webEx.ErrorMessage, Is.Null); Assert.That(webEx.ServerStackTrace, Is.Null); }
public void Can_Retrieve_Errors_From_ResponseBody_If_ResponseDto_Does_Not_Contain_ResponseStatus() { var webEx = new WebServiceException { ResponseDto = new List<string> {"123"}, ResponseBody = "{\"ResponseStatus\":" + "{\"ErrorCode\":\"UnauthorizedAccessException\"," + "\"Message\":\"Error Message\"," + "\"StackTrace\":\"Some Stack Trace\",\"Errors\":[]}}" }; Assert.That(webEx.ErrorCode, Is.EqualTo("UnauthorizedAccessException")); Assert.That(webEx.ErrorMessage, Is.EqualTo("Error Message")); Assert.That(webEx.ServerStackTrace, Is.EqualTo("Some Stack Trace")); }
public static SamplesApiException CreateSamplesApiExceptionFromResponse(WebServiceException e) { if (e.ResponseHeaders["server"] == "AmazonS3") { return(new SamplesMaintenanceModeException($"{e.Message}: AQUARIUS Samples is in maintenance mode", e)); } var errorResponse = DeserializeErrorFromResponse(e); var message = ComposeMessage(e, errorResponse); if (errorResponse?.ErrorCode == "gaia.domain.exceptions.AuthenticationException") { return(new SamplesAuthenticationException(message, e, errorResponse)); } return(new SamplesApiException(message, e, errorResponse)); }
public void Can_Retrieve_Errors_From_ResponseBody_If_ResponseDto_Does_Not_Contain_ResponseStatus() { var webEx = new WebServiceException { ResponseDto = new List <string> { "123" }, ResponseBody = "{\"ResponseStatus\":" + "{\"ErrorCode\":\"UnauthorizedAccessException\"," + "\"Message\":\"Error Message\"," + "\"StackTrace\":\"Some Stack Trace\",\"Errors\":[]}}" }; Assert.That(webEx.ErrorCode, Is.EqualTo("UnauthorizedAccessException")); Assert.That(webEx.ErrorMessage, Is.EqualTo("Error Message")); Assert.That(webEx.ServerStackTrace, Is.EqualTo("Some Stack Trace")); }
public void GET_returns_ArgumentNullException() { var restClient = CreateRestClient(); WebServiceException webEx = null; HttpErrorResponse response = null; restClient.GetAsync <HttpErrorResponse>(ListeningOn + "errors", r => response = r, (r, ex) => { response = r; webEx = (WebServiceException)ex; }); Thread.Sleep(1000); Assert.That(webEx.StatusCode, Is.EqualTo(400)); Assert.That(response.ResponseStatus.ErrorCode, Is.EqualTo(typeof(ArgumentNullException).Name)); }
public void Can_retrieve_Errors_from_Dto_WithStatusResponse() { var webEx = new WebServiceException { ResponseDto = new WithStatusResponse { ResponseStatus = new ResponseStatus { ErrorCode = "errorCode", Message = "errorMessage", StackTrace = "stackTrace" } } }; Assert.That(webEx.ErrorCode, Is.EqualTo("errorCode")); Assert.That(webEx.ErrorMessage, Is.EqualTo("errorMessage")); Assert.That(webEx.ServerStackTrace, Is.EqualTo("stackTrace")); }
public void GET_returns_custom_Exception_and_StatusCode() { var restClient = CreateRestClient(); WebServiceException webEx = null; HttpErrorResponse response = null; restClient.GetAsync <HttpErrorResponse>(ListeningOn + "errors/FileNotFoundException/404", r => response = r, (r, ex) => { response = r; webEx = (WebServiceException)ex; }); Thread.Sleep(1000); Assert.That(webEx.StatusCode, Is.EqualTo(404)); Assert.That(response.ResponseStatus.ErrorCode, Is.EqualTo(typeof(FileNotFoundException).Name)); }
private static void HandleException <TResponse>(Exception exception, Action <TResponse, Exception> onError) { var response = (TResponse)typeof(TResponse).CreateInstance(); var hasResponseStatus = response as IHasResponseStatus; if (hasResponseStatus != null) { hasResponseStatus.ResponseStatus = new ResponseStatus { ErrorCode = exception.GetType().Name, Message = exception.Message, StackTrace = exception.StackTrace, }; } var webServiceEx = new WebServiceException(exception.Message, exception); if (onError != null) { onError(response, webServiceEx); } }
internal static TRes MakeApiRequest <TReq, TRes>( Uri endpoint, HttpMethod httpMethod, TReq body, string actionDescription, string apiKey) { try { HttpWebRequest request = CreateWebRequest <TReq>( endpoint, httpMethod, body, apiKey); return(GetResponse <TRes>(request)); } catch (WebException ex) { throw WebServiceException.AdaptException(ex, actionDescription, endpoint); } catch (Exception ex) { ExceptionLogger.LogException(actionDescription, ex.Message, ex.StackTrace, endpoint, HttpStatusCode.OK); throw; } }
public override object OnServiceException(ServiceStack.Web.IRequest httpReq, object request, Exception ex) { if (ex != null) { string errorMessage = ex.Message; string rawUrl = ""; if (httpReq != null) { rawUrl = httpReq.RawUrl; } if (ex is WebServiceException) { WebServiceException webServiceException = ex as WebServiceException; errorMessage = webServiceException.ErrorMessage; } if (request != null) { errorMessage = string.Format("Post 出错{0}, requestType: {1}, request: {2}, rawUrl: {3}", errorMessage, request.GetType().Name, JsonConvert.SerializeObject(request), rawUrl); } if (ex is ValidateException) { EIMLog.Logger.Info(errorMessage, ex); } else { EIMLog.Logger.Error(errorMessage, ex); } } else { EIMLog.Logger.Error(request); } return(base.OnServiceException(httpReq, request, ex)); }
public TResponse Send <TResponse>(object request) { var response = ServiceManager.Execute(request); var httpResult = response as IHttpResult; if (httpResult != null) { if (httpResult.StatusCode >= HttpStatusCode.BadRequest) { var webEx = new WebServiceException(httpResult.StatusDescription) { ResponseDto = httpResult.Response, StatusCode = httpResult.Status, }; throw webEx; } return((TResponse)httpResult.Response); } return((TResponse)response); }
public void DELETE_a_non_existing_file_throws_404() { var restClient = CreateAsyncRestClient(); WebServiceException webEx = null; FilesResponse response = null; restClient.DeleteAsync <FilesResponse>("files/non-existing-file.txt", r => response = r, (r, ex) => { response = r; webEx = (WebServiceException)ex; }); Thread.Sleep(1000); Assert.That(webEx.StatusCode, Is.EqualTo(404)); Assert.That(response.ResponseStatus.ErrorCode, Is.EqualTo(typeof(FileNotFoundException).Name)); Assert.That(response.ResponseStatus.Message, Is.EqualTo("Could not find: non-existing-file.txt")); }
public void GET_a_file_that_doesnt_exist_throws_a_404_FileNotFoundException() { var restClient = CreateAsyncRestClient(); WebServiceException webEx = null; FilesResponse response = null; restClient.GetAsync <FilesResponse>("files/UnknownFolder", r => response = r, (r, ex) => { response = r; webEx = (WebServiceException)ex; }); Thread.Sleep(1000); Assert.That(webEx.StatusCode, Is.EqualTo(404)); Assert.That(response.ResponseStatus.ErrorCode, Is.EqualTo(typeof(FileNotFoundException).Name)); Assert.That(response.ResponseStatus.Message, Is.EqualTo("Could not find: UnknownFolder")); }
private static void AssertErrorResponse(WebServiceException ex) { Assert.That(ex.ErrorCode, Is.EqualTo("NotNull")); Assert.That(ex.ErrorMessage, Is.EqualTo("'First Name' must not be empty.")); var status = ex.ResponseStatus; Assert.That(status.Errors.Count, Is.EqualTo(3)); var fieldError = status.Errors.First(x => x.FieldName == nameof(RockstarBase.FirstName)); Assert.That(fieldError.ErrorCode, Is.EqualTo("NotNull")); Assert.That(fieldError.Message, Is.EqualTo("'First Name' must not be empty.")); fieldError = status.Errors.First(x => x.FieldName == nameof(RockstarBase.Age)); Assert.That(fieldError.ErrorCode, Is.EqualTo("NotNull")); Assert.That(fieldError.Message, Is.EqualTo("'Age' must not be empty.")); fieldError = status.Errors.First(x => x.FieldName == nameof(RockstarBase.LastName)); Assert.That(fieldError.ErrorCode, Is.EqualTo("NotNull")); Assert.That(fieldError.Message, Is.EqualTo("'Last Name' must not be empty.")); }
public BaseSpaceException(string message, string errorCode, Exception ex) : base(message, ex) { StatusCode = (HttpStatusCode)RetryLogic.GetStatusCode(ex); ErrorCode = errorCode; WebServiceException wse = ex as WebServiceException; if (wse != null) { Response = wse.ResponseDto as IHasResponseStatus; if (wse.ResponseBody != null) { try { ResponseBodyJson = JsonObject.Parse(wse.ResponseBody); } catch { } } } }
public static void AssertTriggerValidators(this WebServiceException ex) { var errors = ex.ResponseStatus.Errors; Assert.That(errors.First(x => x.FieldName == "CreditCard").ErrorCode, Is.EqualTo("CreditCard")); Assert.That(errors.First(x => x.FieldName == "Email").ErrorCode, Is.EqualTo("Email")); Assert.That(errors.First(x => x.FieldName == "Email").ErrorCode, Is.EqualTo("Email")); Assert.That(errors.First(x => x.FieldName == "Empty").ErrorCode, Is.EqualTo("Empty")); Assert.That(errors.First(x => x.FieldName == "Equal").ErrorCode, Is.EqualTo("Equal")); Assert.That(errors.First(x => x.FieldName == "ExclusiveBetween").ErrorCode, Is.EqualTo("ExclusiveBetween")); Assert.That(errors.First(x => x.FieldName == "GreaterThan").ErrorCode, Is.EqualTo("GreaterThan")); Assert.That(errors.First(x => x.FieldName == "GreaterThanOrEqual").ErrorCode, Is.EqualTo("GreaterThanOrEqual")); Assert.That(errors.First(x => x.FieldName == "InclusiveBetween").ErrorCode, Is.EqualTo("InclusiveBetween")); Assert.That(errors.First(x => x.FieldName == "Length").ErrorCode, Is.EqualTo("Length")); Assert.That(errors.First(x => x.FieldName == "LessThan").ErrorCode, Is.EqualTo("LessThan")); Assert.That(errors.First(x => x.FieldName == "LessThanOrEqual").ErrorCode, Is.EqualTo("LessThanOrEqual")); Assert.That(errors.First(x => x.FieldName == "NotEmpty").ErrorCode, Is.EqualTo("NotEmpty")); Assert.That(errors.First(x => x.FieldName == "NotEqual").ErrorCode, Is.EqualTo("NotEqual")); Assert.That(errors.First(x => x.FieldName == "Null").ErrorCode, Is.EqualTo("Null")); Assert.That(errors.First(x => x.FieldName == "RegularExpression").ErrorCode, Is.EqualTo("RegularExpression")); Assert.That(errors.First(x => x.FieldName == "ScalePrecision").ErrorCode, Is.EqualTo("ScalePrecision")); }
private void ThrowIfError <TResponse>(MockHttpResponse httpRes) { if (httpRes.StatusCode >= 400) { var webEx = new WebServiceException("WebServiceException, StatusCode: " + httpRes.StatusCode) { StatusCode = httpRes.StatusCode, StatusDescription = httpRes.StatusDescription, }; try { var deserializer = HostContext.ContentTypes.GetStreamDeserializer(httpReq.ResponseContentType); webEx.ResponseDto = deserializer(typeof(TResponse), new MemoryStream(httpRes.ReadAsBytes())); } catch (Exception ex) { Console.WriteLine(ex); } throw webEx; } }
private void ThrowIfError <TResponse>(HttpResponseMock httpRes) { if (httpRes.StatusCode >= 400) { var webEx = new WebServiceException("WebServiceException, StatusCode: " + httpRes.StatusCode) { StatusCode = httpRes.StatusCode, StatusDescription = httpRes.StatusDescription, }; try { var deserializer = EndpointHost.AppHost.ContentTypeFilters.GetStreamDeserializer(httpReq.ResponseContentType); webEx.ResponseDto = deserializer(typeof(TResponse), httpRes.OutputStream); } catch (Exception ex) { Console.WriteLine(ex); } throw webEx; } }
public TResponse Send <TResponse>(object request) { var message = MessageFactory.Create(request); var response = ServiceManager.ExecuteMessage(message); var httpResult = response as IHttpResult; if (httpResult != null) { if (httpResult.StatusCode >= HttpStatusCode.BadRequest) { var webEx = new WebServiceException(httpResult.StatusDescription) { ResponseDto = httpResult.Response, StatusCode = httpResult.Status, }; throw webEx; } return((TResponse)httpResult.Response); } var responseStatus = response.GetResponseStatus(); var isError = responseStatus != null && responseStatus.ErrorCode != null; if (isError) { var webEx = new WebServiceException(responseStatus.Message) { ResponseDto = response, StatusCode = responseStatus.Errors != null && responseStatus.Errors.Count > 0 ? 400 : 500, }; throw webEx; } return((TResponse)response); }
internal static ComposerException Create(WebServiceException source) { if (source == null) { throw new ArgumentNullException(nameof(source)); } var errors = source.GetFieldErrors().Select(error => new ErrorViewModel { ErrorCode = error.ErrorCode, ErrorMessage = error.Message }).ToList(); if (errors.All(e => e.ErrorCode != source.ErrorCode)) { errors.Add(new ErrorViewModel { ErrorCode = source.ErrorCode, ErrorMessage = source.ErrorMessage }); } return(new ComposerException(errors)); }
public static WebServiceException[] ParseSoapException(System.Web.Services.Protocols.SoapException soapException) { WebServiceException[] exceptions; try { exceptions = new WebServiceException[soapException.Detail.ChildNodes.Count]; for (int i = 0; i < exceptions.Length; i++) { exceptions[i] = new WebServiceException (Convert.ToInt32(soapException.Detail.ChildNodes[i].Attributes["e:number"].Value) , soapException.Detail.ChildNodes[i].Attributes["e:type"].Value , soapException.Detail.ChildNodes[i].Attributes["e:message"].Value); } return(exceptions); } catch (Exception exception) { throw exception; } finally { exceptions = null; } }
/// <summary> /// Parse the Response For Deployments And Return Them /// </summary> /// <param name="httpWebResponse">The Response From the Web Service</param> /// <returns>List of Deployments</returns> private static ServiceDashboardResponse ParseResponseFromJson(HttpWebResponse httpWebResponse) { //try //{ if (httpWebResponse.StatusCode == HttpStatusCode.OK) { //if (httpWebResponse.ContentType==) DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ServiceDashboardResponse)); if (httpWebResponse.ContentLength > int.MaxValue) { throw new IndexOutOfRangeException(String.Format("Response From Web Service Exceeds {0}", int.MaxValue)); } // WWB: Read The Data From The Response Stream int length = (int)httpWebResponse.ContentLength; byte[] data = new byte[length]; using (Stream responseStream = httpWebResponse.GetResponseStream()) { responseStream.Read(data, 0, length); } // WWB: In A Memory Stream Convert the Json using (MemoryStream stream = new MemoryStream(data)) { var response = serializer.ReadObject(stream) as ServiceDashboardResponse; // WWB: Check The Response For Errors if (response.Status != ServiceDashboardResponseStatus.Success) { var exception = new WebServiceException<ServiceDashboardResponseStatus>() { Status = response.Status }; throw exception; } return response; } } else { ServiceDashboardResponse ErrorResponse = new ServiceDashboardResponse() { Status = ServiceDashboardResponseStatus.IllegalResponse, Dashboard = null }; return ErrorResponse; } //} //catch (Exception ex) //{ // String message = ex.Message; // //if (ex is SerializationException) // //{ // // return ServiceDashboardResponse ErrorResponse = new ServiceDashboardResponse() // // { // // Status = ServiceDashboardResponse.IllegalResponse, //Dashboard = null, //ServiceCode = String.Empty // // }; // //} // throw new Exception(); //} }
protected void AssertUnAuthorized(WebServiceException webEx) { Assert.That(webEx.StatusCode, Is.EqualTo((int)HttpStatusCode.Unauthorized)); Assert.That(webEx.StatusDescription, Is.EqualTo(HttpStatusCode.Unauthorized.ToString())); }
private static bool IsDuplicateAttachmentException(WebServiceException exception) { return(exception.ErrorCode?.Equals("DuplicateImportedAttachmentException", StringComparison.InvariantCultureIgnoreCase) ?? false); }
private static bool IsDuplicateFailure(WebServiceException exception) { return((exception.ErrorCode?.Equals("FieldDataFileImportFailureException", StringComparison.InvariantCultureIgnoreCase) ?? false) && exception.ErrorMessage?.IndexOf("Saving parsed data would result in duplicates", StringComparison.InvariantCultureIgnoreCase) >= 0); }
private static Configuration ParseConfiguration(HttpWebResponse httpWebResponse) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ConfigurationResponse)); var response = serializer.ReadObject(httpWebResponse.GetResponseStream()) as ConfigurationResponse; // WWB: Check The Response For Errors if (response.Status != ConfigurationResponseStatus.Success) { var exception = new WebServiceException<ConfigurationResponseStatus>() { Status = response.Status }; throw exception; } return response.Configuration; }
public SamplesApiException(string message, WebServiceException originalException) : base(message, originalException) { StatusCode = originalException.StatusCode; }
private static void Assert404(WebServiceException webEx) { Assert.That(webEx.StatusCode, Is.EqualTo(404)); Assert.That(webEx.ResponseStatus.ErrorCode, Is.EqualTo(HttpStatusCode.NotFound.ToString())); Assert.That(webEx.ResponseStatus.Message, Is.EqualTo("Custom Status Description")); }
private static bool IsTimeSeriesLocked(WebServiceException exception) { return(exception.ErrorCode == "DeleteLockedTimeSeriesException"); }
public SamplesApiException(string message, WebServiceException originalException, SamplesErrorResponse errorResponse) : base(message, originalException) { StatusCode = originalException.StatusCode; SamplesError = errorResponse; }
private static AzureFunctionCheckResponse ParseResponseObjectsOpStatusResponse(HttpWebResponse httpWebResponse) { //try //{ if (httpWebResponse.StatusCode == HttpStatusCode.OK) { //if (httpWebResponse.ContentType==) DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AzureFunctionCheckResponse)); if (httpWebResponse.ContentLength > int.MaxValue) { throw new IndexOutOfRangeException(String.Format("Response From Web Service Exceeds {0}", int.MaxValue)); } // WWB: Read The Data From The Response Stream int length = (int)httpWebResponse.ContentLength; byte[] data = new byte[length]; using (Stream responseStream = httpWebResponse.GetResponseStream()) { responseStream.Read(data, 0, length); } // WWB: In A Memory Stream Convert the Json using (MemoryStream stream = new MemoryStream(data)) { var response = serializer.ReadObject(stream) as AzureFunctionCheckResponse; // WWB: Check The Response For Errors if (response.WindowsAzureStatus != AzureFunctionCheckResponseStatus.Success) { var exception = new WebServiceException<AzureFunctionCheckResponseStatus>() { Status = response.WindowsAzureStatus }; throw exception; } return response; } } else { AzureFunctionCheckResponse ErrorResponse = new AzureFunctionCheckResponse() { WindowsAzureStatus = AzureFunctionCheckResponseStatus.IllegalResponse, FunctionCheckResult = null, RequestId = String.Empty, FunctionName = String.Empty }; return ErrorResponse; } }
public static string FormatExceptionForWeb(int currentUserId, string componentName, string pageName, WebServiceException ex) { StringBuilder msg = new StringBuilder(); msg.Append(HtmlBuilder.GetLineBreak(3)); msg.AppendLine(HtmlBuilder.WrapInBoldTags("An unexpected error has occurred. Please notify your System Administrator.") + HtmlBuilder.GetLineBreak(2)); msg.Append(HtmlBuilder.WrapInBoldTags("****ENVIRONMENT DETAILS****") + HtmlBuilder.GetLineBreak()); msg.Append(HtmlBuilder.WrapInBoldTags("Component Name: ") + componentName + HtmlBuilder.GetLineBreak()); msg.Append(HtmlBuilder.WrapInBoldTags("Page Name: ") + pageName + HtmlBuilder.GetLineBreak()); msg.Append(HtmlBuilder.WrapInBoldTags("UserId: ") + currentUserId.ToString() + HtmlBuilder.GetLineBreak()); msg.Append(HtmlBuilder.WrapInBoldTags("Date/time: ") + DateTime.Now.ToString() + HtmlBuilder.GetLineBreak(2)); do { msg.Append(HtmlBuilder.WrapInBoldTags("****EXCEPTION DETAILS****") + HtmlBuilder.GetLineBreak()); msg.Append(HtmlBuilder.WrapInBoldTags("Exception Type: ") + ex.SourceExceptionTypeName + HtmlBuilder.GetLineBreak()); msg.Append(HtmlBuilder.WrapInBoldTags("Source: ") + ex.Source + HtmlBuilder.GetLineBreak()); msg.Append(HtmlBuilder.WrapInBoldTags("Message: ") + ex.Message + HtmlBuilder.GetLineBreak()); msg.Append(HtmlBuilder.WrapInBoldTags("Target Site: ") + ex.TargetSite + HtmlBuilder.GetLineBreak()); msg.Append(HtmlBuilder.WrapInBoldTags("Stack Trace: ") + ex.StackTrace); msg.Append(HtmlBuilder.GetLineBreak(2)); ex = ex.InnerException; }while (ex != null); Literal lit = new Literal(); lit.Text = msg.ToString(); Panel pnl = new Panel(); pnl.ForeColor = ColorTranslator.FromHtml("#4d4f53"); pnl.Font.Names = new string[] { "Tahoma", "Arial" }; pnl.Font.Size = new FontUnit("13px"); pnl.Controls.Add(lit); return(HtmlBuilder.GetServerControlHtml(pnl)); }
public SamplesAuthenticationException(string message, WebServiceException originalException) : base(message, originalException) { }
public SamplesAuthenticationException(string message, WebServiceException originalException, SamplesErrorResponse errorResponse) : base(message, originalException, errorResponse) { }