예제 #1
0
        public static WebException CreateWebException(WebException webException, string title = null)
        {
            if (webException == null)
            {
                throw new ArgumentNullException("webException");
            }

            switch (webException.Status)
            {
            case WebExceptionStatus.NameResolutionFailure:
                return(new NameResolutionFailureWebException(title ?? webException.Message, webException));

            case WebExceptionStatus.ConnectFailure:
                return(new ConnectFailureWebException(title ?? webException.Message, webException));

            case WebExceptionStatus.ConnectionClosed:
                return(new ConnectionClosedWebException(title ?? webException.Message, webException));

            case WebExceptionStatus.ProtocolError:
                HttpWebResponse errorResponse = webException.Response as HttpWebResponse;
                if (errorResponse == null)
                {
                    return(new WebProtocolException(title ?? webException.Message, webException));
                }

                WebProtocolException webProtocolException = CreateProtocolException(webException, title);
                webProtocolException.StatusCode        = (int)errorResponse.StatusCode;
                webProtocolException.StatusDescription = errorResponse.StatusDescription;
                return(webProtocolException);

            case WebExceptionStatus.Timeout:
                return(new TimeoutWebException(title ?? webException.Message, webException));

            case WebExceptionStatus.RequestCanceled:
                return(new RequestCanceledWebException(title ?? webException.Message, webException));

            default:
                return(webException);
            }
        }
        protected internal WebException HandleWebException(ClientExecutionContext context, WebException webEx)
        {
            using (webEx.Response)
            {
                HttpWebResponse errorResponse = webEx.Response as HttpWebResponse;
                if (errorResponse == null)
                {
                    if (webEx is WebExceptionBase)
                    {
                        return(webEx);
                    }
                    return(ExceptionFactory.CreateWebException(webEx));
                }

                if (webEx.Status == WebExceptionStatus.ProtocolError)
                {
                    string title = string.IsNullOrWhiteSpace(errorResponse.StatusDescription) ? webEx.Message : errorResponse.StatusDescription;

                    WebProtocolException serviceEx = null;
                    if (webEx is WebProtocolException)
                    {
                        serviceEx = (WebProtocolException)webEx;
                    }
                    else
                    {
                        serviceEx = (WebProtocolException)ExceptionFactory.CreateWebException(webEx, title);
                    }

                    try
                    {
                        using (Stream stream = errorResponse.GetResponseStream())
                        {
                            if (errorResponse.ContentType.MatchesContentType(context.ContentType))
                            {
                                var bytes = stream.ReadFully();
                                using (var memoryStream = new MemoryStream(bytes))
                                {
                                    serviceEx.ResponseBody   = bytes.FromUtf8Bytes();
                                    serviceEx.ResponseObject = (GenericErrorResponseType)context.CallFormat.StreamDeserializer(typeof(GenericErrorResponseType), memoryStream);
                                }
                            }
                            else
                            {
                                serviceEx.ResponseBody = stream.ToUtf8String();
                            }
                        }
                    }
                    catch (Exception innerEx)
                    {
                        // Oh, well, we tried
                        return(new WebProtocolException(title, innerEx)
                        {
                            StatusCode = (int)errorResponse.StatusCode,
                            StatusDescription = errorResponse.StatusDescription,
                            ResponseBody = serviceEx.ResponseBody
                        });
                    }

                    //Escape deserialize exception handling and throw here
                    return(serviceEx);
                }

                return(webEx);
            }
        }