Ejemplo n.º 1
0
        public ODataError(Exception ex)
        {
            if (ex == null)
            {
                throw new ArgumentNullException();
            }

            Type errorType = ex.GetType();

            // Awful code
            if (ex is DataServiceRequestException)
            {
                DataServiceRequestException dsre = ex as DataServiceRequestException;

                this.ProcessDataServiceRequest(dsre);
                return;
            }
            if (ex is DataServiceClientException)
            {
                DataServiceClientException dsce = ex as DataServiceClientException;

                this.ProcessDataServiceClient(dsce);
                return;
            }
            if (ex is DataServiceTransportException)
            {
                DataServiceTransportException dste = ex as DataServiceTransportException;

                this.ProcessDataServiceTransport(dste);
                return;
            }
            if (ex is DataServiceQueryException)
            {
                DataServiceQueryException dsqe = ex as DataServiceQueryException;

                this.ProcessDataServiceQuery(dsqe);
                return;
            }
            if (ex is InvalidOperationException)
            {
                InvalidOperationException inoper = ex as InvalidOperationException;

                this.ProcessInvalidOperation(inoper);

                return;
            }
            if (ex is NotImplementedException)
            {
                NotImplementedException notimpl = ex as NotImplementedException;

                this.ProcessNotImplemented(notimpl);

                return;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Indicates whether the specified <see cref="Exception"/> is
        /// related to an endpoint not being available.
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        public static bool IsEndpointDown(System.Exception exception)
        {
            if (exception == null)
            {
                throw new ArgumentNullException("exception");
            }

            EndpointNotFoundException endpointNotFoundException = exception as EndpointNotFoundException;

            if (endpointNotFoundException != null)
            {
                return(true);
            }

            ServerTooBusyException serverTooBusyException = exception as ServerTooBusyException;

            if (serverTooBusyException != null)
            {
                WebException webException = serverTooBusyException.InnerException as WebException;
                return(IsEndpointDown(webException));
            }

            TimeoutException timeoutException = exception as TimeoutException;

            if (timeoutException != null)
            {
                return(true);
            }

            DataServiceTransportException dataServiceTransportException = exception as DataServiceTransportException;

            if (dataServiceTransportException != null)
            {
                WebException webException = dataServiceTransportException.InnerException as WebException;
                return(IsEndpointDown(webException));
            }

            DataServiceRequestException dataServiceRequestException = exception as DataServiceRequestException;

            if (dataServiceRequestException != null)
            {
                DataServiceClientException dataServiceClientException =
                    dataServiceRequestException.InnerException as DataServiceClientException;
                if (dataServiceClientException != null)
                {
                    WebException webException = dataServiceClientException.InnerException as WebException;
                    return(IsEndpointDown(webException));
                }
            }

            return(false);
        }
        public void WebRequestTransientErrorDetectionStrategyDataServiceTransportExceptionTest()
        {
            HttpStatusCode[] allHttpStatusCodeValues = (HttpStatusCode[])Enum.GetValues(typeof(HttpStatusCode));

            WebRequestTransientErrorDetectionStrategy strategy = new WebRequestTransientErrorDetectionStrategy();

            foreach (HttpStatusCode status in allHttpStatusCodeValues)
            {
                DataServiceTransportException exception = QueryErrorDetectionStrategyTest.GetMockedTransportException(status);

                Assert.IsFalse(strategy.IsTransient(exception));
            }
        }
Ejemplo n.º 4
0
        /// <summary>get response object from possible WebException</summary>
        /// <param name="exception">exception to probe</param>
        /// <param name="response">http web response object from exception</param>
        /// <returns>an instance of InvalidOperationException.</returns>
        internal static InvalidOperationException GetHttpWebResponse(InvalidOperationException exception, ref IODataResponseMessage response)
        {
            if (response == null)
            {
                DataServiceTransportException webexception = (exception as DataServiceTransportException);
                if (webexception != null)
                {
                    response = webexception.Response;
                    return((InvalidOperationException)webexception.InnerException);
                }
            }

            return(exception);
        }
        public void SaveChangesErrorDetectionStrategyDataServiceTransportExceptionTest()
        {
            HttpStatusCode[] allHttpStatusCodeValues = (HttpStatusCode[])Enum.GetValues(typeof(HttpStatusCode));

            SaveChangesErrorDetectionStrategy strategy = new SaveChangesErrorDetectionStrategy();

            foreach (HttpStatusCode status in allHttpStatusCodeValues)
            {
                DataServiceTransportException exception = QueryErrorDetectionStrategyTest.GetMockedTransportException(status);

                if (strategy.IsTransient(exception))
                {
                    Assert.IsTrue(SupportedRetryableHttpStatusCodes.Contains(status), status.ToString());
                }
                else
                {
                    Assert.IsFalse(SupportedRetryableHttpStatusCodes.Contains(status), status.ToString());
                }
            }
        }
Ejemplo n.º 6
0
        public void RequestNonExistingEntityShouldReturnNotFound()
        {
            var requestMessage = new HttpWebRequestMessage(
                new DataServiceClientRequestMessageArgs(
                    "GET",
                    new Uri(this.ServiceBaseUri.OriginalString + "Airlines('NonExisting')", UriKind.Absolute),
                    useDefaultCredentials: true,
                    usePostTunneling: false,
                    headers: new Dictionary <string, string>()));

            DataServiceTransportException exception = null;

            try
            {
                requestMessage.GetResponse();
            }
            catch (DataServiceTransportException e)
            {
                exception = e;
            }

            Assert.NotNull(exception);
            Assert.Equal(404, exception.Response.StatusCode);
        }
Ejemplo n.º 7
0
 private void ProcessDataServiceTransport(DataServiceTransportException dste)
 {
     this.errorMessage = dste.Message;
 }
Ejemplo n.º 8
0
        public void CURDSingleNavigationPropertyAndRef()
        {
            this.TestClientContext.MergeOption = Microsoft.OData.Client.MergeOption.OverwriteChanges;

            Airline airline = new Airline()
            {
                Name           = "American Delta",
                AirlineCode    = "DL",
                TimeStampValue = new byte[] { 0 }
            };

            this.TestClientContext.AddToAirlines(airline);
            this.TestClientContext.SaveChanges();

            // Post an entity
            Flight flight = new Flight()
            {
                ConfirmationCode = "JH58496",
                FlightNumber     = "DL589",
                StartsAt         = new DateTimeOffset(new DateTime(2014, 2, 10, 15, 00, 0)),
                EndsAt           = new DateTimeOffset(new DateTime(2014, 2, 10, 16, 30, 0)),
                AirlineId        = null,
                SeatNumber       = "C32",
                FromId           = "KSEA",
                ToId             = "ZSSS"
            };

            this.TestClientContext.AddToFlights(flight);
            this.TestClientContext.SaveChanges();

            // Set $ref
            this.TestClientContext.SetLink(flight, "Airline", airline);
            this.TestClientContext.SaveChanges();

            this.TestClientContext.Detach(airline);
            // Query an Navigation Property
            var airline2 = this.TestClientContext.Flights
                           .ByKey(new Dictionary <string, object>()
            {
                { "FlightId", flight.FlightId }
            })
                           .Airline.GetValue();

            Assert.Equal(airline.AirlineCode, airline2.AirlineCode);

            // Expand an Navigation Property
            var flight2 = this.TestClientContext.Flights
                          .Expand(f => f.From)
                          .Expand(f => f.To)
                          .Where(f => f.FlightId == flight.FlightId)
                          .SingleOrDefault();

            Assert.Equal(flight.FromId, flight2.From.IcaoCode);
            Assert.Equal(flight.ToId, flight2.To.IcaoCode);

            // Expand with select
            this.TestClientContext.Detach(flight2.From);
            var flight3 = this.TestClientContext.Flights
                          .AddQueryOption("$expand", "From($select=IcaoCode)")
                          .Where(f => f.FlightId == flight.FlightId)
                          .SingleOrDefault();

            Assert.Equal(flight.FromId, flight3.From.IcaoCode);
            Assert.Null(flight3.From.IataCode);

            // Get $ref
            Dictionary <string, string> headers        = new Dictionary <string, string>();
            ODataMessageReaderSettings  readerSettings = new ODataMessageReaderSettings {
                BaseUri = this.TestClientContext.BaseUri
            };

            HttpWebRequestMessage request = new HttpWebRequestMessage(
                new DataServiceClientRequestMessageArgs(
                    "Get",
                    new Uri(string.Format(this.TestClientContext.BaseUri + "/Flights({0})/Airline/$ref", flight.FlightId),
                            UriKind.Absolute),
                    false,
                    false,
                    headers));

            using (HttpWebResponseMessage response = request.GetResponse() as HttpWebResponseMessage)
            {
                Assert.Equal(200, response.StatusCode);
                using (var stream = response.GetStream())
                {
                    StreamReader reader          = new StreamReader(stream);
                    var          expectedPayload = "{"
                                                   + "\r\n"
                                                   + @"  ""@odata.context"":""http://*****:*****@"""@odata.id"":""http://localhost:18384/api/Trippin/Airlines('{0}')""", airline2.AirlineCode)
                                                   + "\r\n"
                                                   + "}";
                    var content = reader.ReadToEnd();
                    Assert.Equal(expectedPayload, content);
                }
            }

            // Delete $ref
            this.TestClientContext.SetLink(flight, "Airline", null);
            this.TestClientContext.SaveChanges();

            this.TestClientContext.Detach(airline);

            HttpWebRequestMessage request2 = new HttpWebRequestMessage(
                new DataServiceClientRequestMessageArgs(
                    "Get",
                    new Uri(string.Format(this.TestClientContext.BaseUri + "/Flights({0})/Airline/$ref", flight.FlightId)
                            , UriKind.Absolute),
                    false,
                    false,
                    headers));

            DataServiceTransportException exception = null;

            try
            {
                request2.GetResponse();
            }
            catch (DataServiceTransportException e)
            {
                exception = e;
            }
            Assert.NotNull(exception);
            Assert.Equal(404, exception.Response.StatusCode);

            // TODO GitHubIssue#288 : 204 is expected.
            // Query an Navigation Property
            try
            {
                airline2 = this.TestClientContext.Flights.ByKey(new Dictionary <string, object>()
                {
                    { "FlightId", flight.FlightId }
                }).Airline.GetValue();
            }
            catch (DataServiceQueryException e)
            {
                Assert.Equal(404, e.Response.StatusCode);
            }
        }