Inheritance: LoggedException
Example #1
0
        void HandleGDataErrorMessage(Google.GData.Client.GDataRequestException queryEx)
        {
            try
            {
                var xDoc = XDocument.Parse(queryEx.ResponseString);

                var innerReasonElement = xDoc.XPathSelectElement("//*[local-name() = 'internalReason']");
                if (innerReasonElement != null && !string.IsNullOrWhiteSpace(innerReasonElement.Value))
                {
                    throw new OnlineVideosException(innerReasonElement.Value);
                }

                var titleElement = xDoc.XPathSelectElement("//*[local-name() = 'TITLE']");
                if (titleElement != null && !string.IsNullOrWhiteSpace(titleElement.Value))
                {
                    throw new OnlineVideosException(titleElement.Value);
                }

                Log.Debug("Could not get an error text from GData: {0}", queryEx.ResponseString);
            }
            catch (Exception ex)
            {
                Log.Info("Error getting GData error message: {0}{1}{2}", ex.Message, Environment.NewLine, queryEx.ResponseString);
            }

            throw queryEx;
        }
 public void ResponseStringTest()
 {
     GDataRequestException target = new GDataRequestException(); // TODO: Initialize to an appropriate value
     string actual;
     actual = target.ResponseString;
     Assert.IsNull(actual);
 }
        //////////////////////////////////////////////////////////////////////
        /// <summary>Executes the request and prepares the response stream. Also 
        /// does error checking</summary> 
        //////////////////////////////////////////////////////////////////////
        public virtual void Execute() {
            try {
                EnsureWebRequest();
                // if we ever handed out a stream, we want to close it before doing the real excecution
                if (this.requestStream != null) {
                    this.requestStream.Close();
                }

                Tracing.TraceCall("calling the real execution over the webresponse");
                LogRequest(this.webRequest);
                this.webResponse = this.webRequest.GetResponse();
            } catch (WebException e) {
                Tracing.TraceCall("GDataRequest::Execute failed: " + this.targetUri.ToString());
                GDataRequestException gde = new GDataRequestException("Execution of request failed: " + this.targetUri.ToString(), e);
                throw gde;
            }

            if (this.webResponse != null) {
                this.responseStream = this.webResponse.GetResponseStream();
            }

            LogResponse(this.webResponse);
            if (this.webResponse is HttpWebResponse) {
                HttpWebResponse response = this.webResponse as HttpWebResponse;
                HttpWebRequest request = this.webRequest as HttpWebRequest;

                this.useGZip = (string.Compare(response.ContentEncoding, "gzip", true, CultureInfo.InvariantCulture) == 0);
                if (this.useGZip) {
                    this.responseStream = new GZipStream(this.responseStream, CompressionMode.Decompress);
                }

                Tracing.Assert(response != null, "The response should not be NULL");
                Tracing.Assert(request != null, "The request should not be NULL");

                int code = (int)response.StatusCode;

                Tracing.TraceMsg("Returned ContentType is: " + (response.ContentType == null ? "None" : response.ContentType) + " from URI : " + request.RequestUri.ToString()); ;
                Tracing.TraceMsg("Returned StatusCode is: " + response.StatusCode + code);

                if (response.StatusCode == HttpStatusCode.Forbidden) {
                    // that could imply that we need to reauthenticate
                    Tracing.TraceMsg("need to reauthenticate");
                    throw new GDataForbiddenException("Execution of request returned HttpStatusCode.Forbidden: " +
                        this.targetUri.ToString() + response.StatusCode.ToString(), this.webResponse);
                }

                if (response.StatusCode == HttpStatusCode.Conflict) {
                    // a put went bad due to a version conflict
                    throw new GDataVersionConflictException("Execution of request returned HttpStatusCode.Conflict: " +
                        this.targetUri.ToString() + response.StatusCode.ToString(), this.webResponse);
                }

                if ((this.IfModifiedSince != DateTime.MinValue || this.Etag != null) 
                    && response.StatusCode == HttpStatusCode.NotModified) {
                    // Throw an exception for conditional GET
                    throw new GDataNotModifiedException("Content not modified: " + this.targetUri.ToString(), this.webResponse);
                }

                if (response.StatusCode == HttpStatusCode.Redirect ||
                    response.StatusCode == HttpStatusCode.Found ||
                    response.StatusCode == HttpStatusCode.RedirectKeepVerb) {
                    Tracing.TraceMsg("throwing for redirect");
                    throw new GDataRedirectException("Execution resulted in a redirect from " + this.targetUri.ToString(), this.webResponse);
                }

                if (code > 299) {
                    // treat everything else over 300 as errors
                    throw new GDataRequestException("Execution of request returned unexpected result: " + this.targetUri.ToString() +
                        response.StatusCode.ToString(), this.webResponse);
                }

                this.contentLength = response.ContentLength;

                // if we got an etag back, remember it

                this.eTag = response.Headers[GDataRequestFactory.EtagHeader];

                response = null;
                request = null;
            }
        }
 protected virtual void RequestException(GDataRequestException exception)
 {
     if (OnRequestException != null)
         OnRequestException(this, exception);
 }
 public static void LogGoogleException(GDataRequestException e)
 {
     Console.WriteLine("Had the following exception: " + e.Message + " and response string: " + e.ResponseString);
     Console.WriteLine(e);
 }
Example #6
0
        /// <summary>Executes the request and prepares the response stream. Also
        /// does error checking</summary>
        public virtual void Execute()
        {
            try {
                EnsureWebRequest();
                // if we ever handed out a stream, we want to close it before doing the real excecution
                if (this.requestStream != null)
                {
                    this.requestStream.Close();
                }

                Tracing.TraceCall("calling the real execution over the webresponse");
                LogRequest(this.webRequest);
                this.webResponse = this.webRequest.GetResponse();
            } catch (WebException e) {
                Tracing.TraceCall("GDataRequest::Execute failed: " + this.targetUri.ToString());
                GDataRequestException gde = new GDataRequestException("Execution of request failed: " + this.targetUri.ToString(), e);
                throw gde;
            }

            if (this.webResponse != null)
            {
                this.responseStream = this.webResponse.GetResponseStream();
            }

            LogResponse(this.webResponse);
            if (this.webResponse is HttpWebResponse)
            {
                HttpWebResponse response = this.webResponse as HttpWebResponse;
                HttpWebRequest  request  = this.webRequest as HttpWebRequest;

                this.useGZip = (string.Compare(response.ContentEncoding, "gzip", true, CultureInfo.InvariantCulture) == 0);
                if (this.useGZip)
                {
                    this.responseStream = new GZipStream(this.responseStream, CompressionMode.Decompress);
                }

                Tracing.Assert(response != null, "The response should not be NULL");
                Tracing.Assert(request != null, "The request should not be NULL");

                int code = (int)response.StatusCode;

                Tracing.TraceMsg("Returned ContentType is: " + (response.ContentType == null ? "None" : response.ContentType) + " from URI : " + request.RequestUri.ToString());;
                Tracing.TraceMsg("Returned StatusCode is: " + response.StatusCode + code);

                if (response.StatusCode == HttpStatusCode.Forbidden)
                {
                    // that could imply that we need to reauthenticate
                    Tracing.TraceMsg("need to reauthenticate");
                    throw new GDataForbiddenException("Execution of request returned HttpStatusCode.Forbidden: " +
                                                      this.targetUri.ToString() + response.StatusCode.ToString(), this.webResponse);
                }

                if (response.StatusCode == HttpStatusCode.Conflict)
                {
                    // a put went bad due to a version conflict
                    throw new GDataVersionConflictException("Execution of request returned HttpStatusCode.Conflict: " +
                                                            this.targetUri.ToString() + response.StatusCode.ToString(), this.webResponse);
                }

                if ((this.IfModifiedSince != DateTime.MinValue || this.Etag != null) &&
                    response.StatusCode == HttpStatusCode.NotModified)
                {
                    // Throw an exception for conditional GET
                    throw new GDataNotModifiedException("Content not modified: " + this.targetUri.ToString(), this.webResponse);
                }

                if (response.StatusCode == HttpStatusCode.Redirect ||
                    response.StatusCode == HttpStatusCode.Found ||
                    response.StatusCode == HttpStatusCode.RedirectKeepVerb)
                {
                    Tracing.TraceMsg("throwing for redirect");
                    throw new GDataRedirectException("Execution resulted in a redirect from " + this.targetUri.ToString(), this.webResponse);
                }

                if (code > 299)
                {
                    // treat everything else over 300 as errors
                    throw new GDataRequestException("Execution of request returned unexpected result: " + this.targetUri.ToString() +
                                                    response.StatusCode.ToString(), this.webResponse);
                }

                this.contentLength = response.ContentLength;

                // if we got an etag back, remember it
                this.eTag = response.Headers[GDataRequestFactory.EtagHeader];

                response = null;
                request  = null;
            }
        }
        public void GDataRequestExceptionConstructorTest()
        {
            string msg = "TestValue"; // TODO: Initialize to an appropriate value
            WebException exception = new WebException();
            GDataRequestException target = new GDataRequestException(msg, exception);

            Assert.IsNotNull(target);
            Assert.AreEqual(msg, target.Message);
            Assert.AreEqual(exception, target.InnerException);
        }
Example #8
0
        /// <summary>
        /// Parses a GDataRequestException, which wraps the HTTP
        /// error responses, into an AppsException.
        /// </summary>
        /// <param name="e">the GDataRequestException to parse</param>
        /// <returns>a new AppsException object. The object's ErrorCode,
        /// InvalidInput and Reason properties will be set if the XML
        /// in the HTTP response could be parsed, or null otherwise.</returns>
        public static AppsException ParseAppsException(GDataRequestException e)
        {
            AppsException result = null;

            if (e == null)
                return (null);

            if (e.ResponseString == null)
                return (null);

            try
            {
                XmlReader reader = new XmlTextReader(e.ResponseString, XmlNodeType.Document, null);
                // now find the ErrorElement
                while (reader.Read())
                    if (reader.NodeType == XmlNodeType.Element && reader.LocalName == AppsNameTable.AppsError)
                    {
                        result = new AppsException(e);
                        result.ErrorCode =
                            reader.GetAttribute(AppsNameTable.AppsErrorErrorCode);
                        result.InvalidInput =
                            reader.GetAttribute(AppsNameTable.AppsErrorInvalidInput);
                        result.Reason =
                            reader.GetAttribute(AppsNameTable.AppsErrorReason);
                        break;
                    }
            }
            catch (XmlException)
            {
                    
            }

            return result;
        }
Example #9
0
 /// <summary>
 /// Constructs a new AppsException to be parsed from the specified
 /// GDataRequestException.
 /// </summary>
 /// <param name="e"></param>
 /// <seealso cref="ParseAppsException(GDataRequestException)"/>
 public AppsException(GDataRequestException e)
     : base("A Google Apps error occurred.", e)
 {
     this.errorCode = null;
     this.invalidInput = null;
     this.reason = null;
 }
Example #10
0
        /////////////////////////////////////////////////////////////////////////////

    

        //////////////////////////////////////////////////////////////////////
        /// <summary>Executes the request and prepares the response stream. Also 
        /// does error checking</summary> 
        //////////////////////////////////////////////////////////////////////
        public virtual void Execute()
        {
            try 
            {
                EnsureWebRequest();
                // if we ever handed out a stream, we want to close it before doing the real excecution
                if (this.requestStream != null)
                {
                    this.requestStream.Close(); 
                }
                Tracing.TraceCall("calling the real execution over the webresponse");
                this.webResponse = this.webRequest.GetResponse(); 
            }
            catch (WebException e)
            {
                Tracing.TraceCall("GDataRequest::Execute failed: " + this.targetUri.ToString()); 
                GDataRequestException gde = new GDataRequestException("Execution of request failed: " + this.targetUri.ToString(), e);
                throw gde;
            }
            if (this.webResponse != null)
                this.responseStream = this.webResponse.GetResponseStream();
            if (this.webResponse is HttpWebResponse)
            {
                HttpWebResponse response = this.webResponse as HttpWebResponse;
                HttpWebRequest request = this.webRequest as HttpWebRequest;

                this.useGZip = (string.Compare(response.ContentEncoding, "gzip", true) == 0);
                if (this.useGZip == true)
                    this.responseStream = new GZipStream(this.responseStream, CompressionMode.Decompress);

                Tracing.Assert(response != null, "The response should not be NULL"); 
                Tracing.Assert(request != null, "The request should not be NULL"); 

                int code = (int)response.StatusCode;

                Tracing.TraceMsg("Returned contenttype is: " + (response.ContentType == null ? "None" : response.ContentType) + " from URI : " + request.RequestUri.ToString()); ; 
                Tracing.TraceMsg("Returned statuscode is: " + response.StatusCode + code); 

                // check for a returned set-cookie header and store it
                if (response != null && 
                    response.Headers != null)
                {
                    String cookie = response.Headers[GDataRequestFactory.SetCookieHeader];
                    if (cookie != null)
                    {
                        this.factory.Cookie = cookie; 
                    }
                }

                if (response.StatusCode == HttpStatusCode.Forbidden)
                {
                    // that could imply that we need to reauthenticate
                    Tracing.TraceMsg("need to reauthenticate");
                    throw new GDataForbiddenException("Execution of request returned HttpStatusCode.Forbidden: " + 
                                                    this.targetUri.ToString() + response.StatusCode.ToString(), this.webResponse); 
                }

            
                if (response.StatusCode == HttpStatusCode.Redirect ||
                    response.StatusCode == HttpStatusCode.Found ||
                    response.StatusCode == HttpStatusCode.RedirectKeepVerb)
                {
                    Tracing.TraceMsg("throwing for redirect");
                    throw new GDataRedirectException("Execution resulted in a redirect from " + this.targetUri.ToString(), this.webResponse);
                }

                if (request.Method == HttpMethods.Delete && response.StatusCode != HttpStatusCode.OK)
                {
                    Tracing.TraceCall("GDataRequest::Deletion returned unxepected result: " + response.StatusCode.ToString()); 
                    throw new GDataRequestException("Execution of DELETE returned unexpected result: " + this.targetUri.ToString(), this.webResponse); 
                }


                if (code > 299)
                {
                    // treat everything else over 300 as errors
                    throw new GDataRequestException("Execution of request returned unexpected result: " + this.targetUri.ToString() + 
                                                    response.StatusCode.ToString(), this.webResponse); 
                }

                response = null;
                request = null; 
            }
            // now we need to check all kinds of error returns... 

        }