예제 #1
0
        public void DisplayException(IWin32Window owner, Exception ex)
        {
            // display a custom display message for exceptions that have one
            // registered, otherwise display the generic error form
            if (ex is BlogClientProviderException)
            {
                IBlogProvider provider = BlogProviderManager.FindProvider(_settings.ProviderId);
                if (provider != null)
                {
                    BlogClientProviderException pe = ex as BlogClientProviderException;
                    MessageId messageId            = provider.DisplayMessageForProviderError(pe.ErrorCode, pe.ErrorString);
                    if (messageId != MessageId.None)
                    {
                        DisplayMessage.Show(messageId, owner);
                        return;
                    }
                }
            }
            else if (ex is WebException)
            {
                WebException    we   = (WebException)ex;
                HttpWebResponse resp = we.Response as HttpWebResponse;
                if (resp != null)
                {
                    string friendlyError = HttpRequestHelper.GetFriendlyErrorMessage(we);
                    Trace.WriteLine("Server response body:\r\n" + friendlyError);
                    ex = new BlogClientHttpErrorException(
                        UrlHelper.SafeToAbsoluteUri(resp.ResponseUri),
                        friendlyError,
                        we);
                }
                else
                {
                    DisplayMessage msg = new DisplayMessage(MessageId.ErrorConnecting);
                    ex = new BlogClientException(msg.Title, msg.Text);
                }
                HttpRequestHelper.LogException(we);
            }

            // no custom message, use default UI
            DisplayableExceptionDisplayForm.Show(owner, ex);
        }
예제 #2
0
        private bool ErrorIsInvalidPostId(BlogClientProviderException ex)
        {
            string faultCodePattern   = BlogClient.Options.InvalidPostIdFaultCodePattern;
            string faultStringPattern = BlogClient.Options.InvalidPostIdFaultStringPattern;

            if (faultCodePattern != String.Empty && faultStringPattern != String.Empty)
            {
                return(FaultCodeMatchesInvalidPostId(ex.ErrorCode, faultCodePattern) &&
                       FaultStringMatchesInvalidPostId(ex.ErrorString, faultStringPattern));
            }
            else if (faultCodePattern != String.Empty)
            {
                return(FaultCodeMatchesInvalidPostId(ex.ErrorCode, faultCodePattern));
            }
            else if (faultStringPattern != String.Empty)
            {
                return(FaultStringMatchesInvalidPostId(ex.ErrorString, faultStringPattern));
            }
            else
            {
                return(false);
            }
        }
예제 #3
0
        /// <summary>
        /// Call a method and return the XML result. Will throw an exception of type BlogClientException
        /// if an error occurs.
        /// </summary>
        /// <param name="postUrl"></param>
        /// <param name="methodName"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        protected XmlNode CallMethod(string methodName, params XmlRpcValue[] parameters)
        {
            string url = _postApiUrl;

            if (Options.SupportsHttps)
            {
                if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
                {
                    url = "https://" + url.Substring("http://".Length);
                }
            }

            try
            {
                // create an RpcClient
                XmlRpcClient xmlRpcClient = new XmlRpcClient(url, ApplicationEnvironment.UserAgent, new HttpRequestFilter(BeforeHttpRequest), _clientOptions.CharacterSet);

                // call the method
                XmlRpcMethodResponse response = xmlRpcClient.CallMethod(methodName, parameters);

                // if success, return the response
                if (!response.FaultOccurred)
                {
                    return(response.Response);
                }
                else // throw error indicating problem
                {
                    // prepare to throw exception
                    BlogClientException exception;

                    // allow underlying provider to return a more descriptive exception type
                    exception = ExceptionForFault(response.FaultCode, response.FaultString);

                    if (exception == null) // if it couldn't just go generic
                    {
                        exception = new BlogClientProviderException(response.FaultCode, response.FaultString);
                    }

                    // throw the exception
                    throw exception;
                }
            }
            catch (IOException ex)
            {
                throw new BlogClientIOException("calling XML-RPC method " + methodName, ex);
            }
            catch (WebException ex)
            {
                HttpRequestHelper.LogException(ex);

                // see if this was a 404 not found
                switch (ex.Status)
                {
                case WebExceptionStatus.ProtocolError:
                    HttpWebResponse response = ex.Response as HttpWebResponse;
                    if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new BlogClientPostUrlNotFoundException(url, ex.Message);
                    }
                    else if (response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        throw new BlogClientAuthenticationException(response.StatusCode.ToString(), ex.Message, ex);
                    }
                    else
                    {
                        throw new BlogClientHttpErrorException(url, string.Format(CultureInfo.InvariantCulture, "{0} {1}", (int)response.StatusCode, response.StatusDescription), ex);
                    }

                default:
                    throw new BlogClientConnectionErrorException(url, ex.Message);
                }
            }
            catch (XmlRpcClientInvalidResponseException ex)
            {
                throw new BlogClientInvalidServerResponseException(methodName, ex.Message, ex.Response);
            }
        }