internal async Task <SimpleHttpResponse> SendAsync(SimpleHttpClient client, int expectedReturnStatus)
        {
            var result = await client.SendAsync();

            if (((int)result.StatusCode) != expectedReturnStatus)
            {
                // Check if we can deserialize the response
                CloudFoundryException cloudFoundryException;
                try
                {
                    string response = await result.Content.ReadAsStringAsync();

                    var exceptionObject = Utilities.DeserializeJson <CloudFoundryExceptionObject>(response);
                    cloudFoundryException          = new CloudFoundryException(exceptionObject);
                    cloudFoundryException.Response = result;
                    throw cloudFoundryException;
                }
                catch (CloudFoundryException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw new CloudFoundryException(string.Format(CultureInfo.InvariantCulture, "An error occurred while talking to the server ({0})", result.StatusCode), ex);
                }
            }

            return(result);
        }
Пример #2
0
        public void TestException()
        {
            CloudFoundryExceptionObject exceptionObject = new CloudFoundryExceptionObject();

            exceptionObject.Code        = "object";
            exceptionObject.Description = "this is an exception";
            exceptionObject.ErrorCode   = "errorCode";

            CloudFoundryException exception = new CloudFoundryException(exceptionObject);

            Assert.AreEqual(exception.Message, "this is an exception");
        }
Пример #3
0
        internal async Task <HttpResponseMessage> SendAsync(SimpleHttpClient client, int[] expectedReturnStatus)
        {
            var result = await client.SendAsync();

            bool success = false;

            foreach (int code in expectedReturnStatus)
            {
                if (((int)result.StatusCode) == code)
                {
                    success = true;
                    break;
                }
            }

            if (!success && !this.Client.UseStrictStatusCodeChecking)
            {
                success = IsSuccessStatusCode(result.StatusCode);
            }

            if (!success)
            {
                // Check if we can deserialize the response
                CloudFoundryException cloudFoundryException;
                try
                {
                    string response = await result.Content.ReadAsStringAsync();

                    var exceptionObject = Utilities.DeserializeJson <CloudFoundryExceptionObject>(response);
                    cloudFoundryException          = new CloudFoundryException(exceptionObject);
                    cloudFoundryException.Response = result;
                    throw cloudFoundryException;
                }
                catch (CloudFoundryException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw new CloudFoundryException(string.Format(CultureInfo.InvariantCulture, "An error occurred while talking to the server ({0})", result.StatusCode), ex);
                }
            }

            return(result);
        }
        public async Task throwCfException(HttpResponseMessage result)
        {
            try
            {
                string response = await result.Content.ReadAsStringAsync();

                var ex = JsonConvert.DeserializeObject <NewCloudFoundryExceptionObject>(response);
                //TODO: something with multiple errors?
                var cloudFoundryException = new CloudFoundryException($"{ex.errors[0].Code}: {ex.errors[0].Title}: {ex.errors[0].Detail} ");
                cloudFoundryException.Response = result;
                throw cloudFoundryException;
            }
            catch (CloudFoundryException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new CloudFoundryException($"An error occurred while talking to the server (status: {result.StatusCode}) {ex.Message}", ex);
            }
        }