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 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"));
		}
Esempio n. 4
0
        /// <summary>Assert un authorized.</summary>
        ///
        /// <param name="webEx">Details of the exception.</param>
		protected void AssertUnAuthorized(WebServiceException webEx)
		{
			Assert.That(webEx.StatusCode, Is.EqualTo((int)HttpStatusCode.Unauthorized));
			Assert.That(webEx.StatusDescription, Is.EqualTo(HttpStatusCode.Unauthorized.ToString()));
		}
        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;
        }