Inheritance: System.Exception
		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"));
	    }
        private void HandleResponseException <TResponse>(Exception ex, string requestUri)
        {
            var webEx = ex as WebException;

            if (webEx != null && webEx.Status == WebExceptionStatus.ProtocolError)
            {
                var errorResponse = ((HttpWebResponse)webEx.Response);
                log.Error(webEx);
                log.DebugFormat("Status Code : {0}", errorResponse.StatusCode);
                log.DebugFormat("Status Description : {0}", errorResponse.StatusDescription);

                var serviceEx = new WebServiceException(errorResponse.StatusDescription)
                {
                    StatusCode = (int)errorResponse.StatusCode,
                };

                try
                {
                    using (var stream = errorResponse.GetResponseStream())
                    {
                        serviceEx.ResponseDto = DeserializeFromStream <TResponse>(stream);
                    }
                }
                catch (Exception innerEx)
                {
                    // Oh, well, we tried
                    throw new WebServiceException(errorResponse.StatusDescription, innerEx)
                          {
                              StatusCode = (int)errorResponse.StatusCode,
                          };
                }

                //Escape deserialize exception handling and throw here
                throw serviceEx;
            }

            var authEx = ex as AuthenticationException;

            if (authEx != null)
            {
                throw WebRequestUtils.CreateCustomException(requestUri, authEx);
            }

            throw ex;
        }
		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"));
		}
        private void HandleResponseError <TResponse>(Exception exception, RequestState <TResponse> requestState)
        {
            var webEx = exception as WebException;

            if (webEx != null
#if !SILVERLIGHT
                && webEx.Status == WebExceptionStatus.ProtocolError
#endif
                )
            {
                var errorResponse = ((HttpWebResponse)webEx.Response);
                Log.Error(webEx);
                Log.DebugFormat("Status Code : {0}", errorResponse.StatusCode);
                Log.DebugFormat("Status Description : {0}", errorResponse.StatusDescription);

                var serviceEx = new WebServiceException(errorResponse.StatusDescription)
                {
                    StatusCode = (int)errorResponse.StatusCode,
                };

                try
                {
                    using (var stream = errorResponse.GetResponseStream())
                    {
                        //Uncomment to Debug exceptions:
                        //var strResponse = new StreamReader(stream).ReadToEnd();
                        //Console.WriteLine("Response: " + strResponse);
                        //stream.Position = 0;
                        serviceEx.ResponseBody = errorResponse.GetResponseStream().ReadFully().FromUtf8Bytes();
#if !MONOTOUCH
                        // MonoTouch throws NotSupportedException when setting System.Net.WebConnectionStream.Position
                        // Not sure if the stream is used later though, so may have to copy to MemoryStream and
                        // pass that around instead after this point?
                        stream.Position = 0;
#endif

                        serviceEx.ResponseDto = this.StreamDeserializer(typeof(TResponse), stream);
                        requestState.HandleError((TResponse)serviceEx.ResponseDto, serviceEx);
                    }
                }
                catch (Exception innerEx)
                {
                    // Oh, well, we tried
                    Log.Debug(string.Format("WebException Reading Response Error: {0}", innerEx.Message), innerEx);
                    requestState.HandleError(default(TResponse), new WebServiceException(errorResponse.StatusDescription, innerEx)
                    {
                        StatusCode = (int)errorResponse.StatusCode,
                    });
                }
                return;
            }

            var authEx = exception as AuthenticationException;
            if (authEx != null)
            {
                var customEx = WebRequestUtils.CreateCustomException(requestState.Url, authEx);

                Log.Debug(string.Format("AuthenticationException: {0}", customEx.Message), customEx);
                requestState.HandleError(default(TResponse), authEx);
            }

            Log.Debug(string.Format("Exception Reading Response Error: {0}", exception.Message), exception);
            requestState.HandleError(default(TResponse), exception);

            _webRequest = null;
        }
 protected void AssertUnAuthorized(WebServiceException webEx)
 {
     Assert.That(webEx.StatusCode, Is.EqualTo((int)HttpStatusCode.Unauthorized));
     Assert.That(webEx.StatusDescription, Is.EqualTo(HttpStatusCode.Unauthorized.ToString()));
 }
Exemplo n.º 7
0
        private void HandleResponseError <TResponse>(Exception exception, RequestState <TResponse> requestState)
        {
            var webEx = exception as WebException;

            if (webEx != null
#if !SILVERLIGHT
                && webEx.Status == WebExceptionStatus.ProtocolError
#endif
                )
            {
                var errorResponse = ((HttpWebResponse)webEx.Response);
                Log.Error(webEx);
                Log.DebugFormat("Status Code : {0}", errorResponse.StatusCode);
                Log.DebugFormat("Status Description : {0}", errorResponse.StatusDescription);

                var serviceEx = new WebServiceException(errorResponse.StatusDescription)
                {
                    StatusCode = (int)errorResponse.StatusCode,
                };

                try
                {
                    using (var stream = errorResponse.GetResponseStream())
                    {
                        //Uncomment to Debug exceptions:
                        //var strResponse = new StreamReader(stream).ReadToEnd();
                        //Console.WriteLine("Response: " + strResponse);
                        //stream.Position = 0;
                        serviceEx.ResponseBody = errorResponse.GetResponseStream().ReadFully().FromUtf8Bytes();
                        stream.Position        = 0;

                        serviceEx.ResponseDto = this.StreamDeserializer(typeof(TResponse), stream);
                        requestState.HandleError((TResponse)serviceEx.ResponseDto, serviceEx);
                    }
                }
                catch (Exception innerEx)
                {
                    // Oh, well, we tried
                    Log.Debug(string.Format("WebException Reading Response Error: {0}", innerEx.Message), innerEx);
                    requestState.HandleError(default(TResponse), new WebServiceException(errorResponse.StatusDescription, innerEx)
                    {
                        StatusCode = (int)errorResponse.StatusCode,
                    });
                }
                return;
            }

            var authEx = exception as AuthenticationException;
            if (authEx != null)
            {
                var customEx = WebRequestUtils.CreateCustomException(requestState.Url, authEx);

                Log.Debug(string.Format("AuthenticationException: {0}", customEx.Message), customEx);
                requestState.HandleError(default(TResponse), authEx);
            }

            Log.Debug(string.Format("Exception Reading Response Error: {0}", exception.Message), exception);
            requestState.HandleError(default(TResponse), exception);

            _webRequest = null;
        }