public static ValidationErrorServiceClientException ShouldHaveFailureNumber(
     this ValidationErrorServiceClientException ex,
     int expectedFailureNumber)
 {
     Assert.That(ex.Failures, Has.Count.EqualTo(expectedFailureNumber));
     return(ex);
 }
        public static ValidationErrorServiceClientException ShouldContainFailure(
            this ValidationErrorServiceClientException ex,
            string expectedKey,
            string expectedCode,
            string expectedMessage)
        {
            Assert.That(ex.Failures, Does.ContainKey(expectedKey));
            var failure = ex.Failures[expectedKey];

            Assert.That(failure.Code, Is.EqualTo(expectedCode));
            Assert.That(failure.Message, Is.EqualTo(expectedMessage));

            return(ex);
        }
Ejemplo n.º 3
0
        private static async Task <Exception> GetFailureException(HttpResponseMessage failMessage)
        {
            try
            {
                var content = await failMessage.Content.ReadAsStringAsync();

                var payloadType = failMessage.Headers.TryGetSingleHeader(DtoHelper.PayloadTypeHeaderName);

                Exception ex = null;

                if (payloadType == DtoHelper.ErrorPayloadType)
                {
                    var error = JsonConvert.DeserializeObject <ErrorDto>(content);

                    switch (failMessage.StatusCode)
                    {
                    case HttpStatusCode.BadRequest:
                        ex = new BadRequestErrorServiceClientException(error.Code, error.Message);
                        break;

                    case HttpStatusCode.Conflict:
                        ex = new ConflictErrorServiceClientException(error.Code, error.Message);
                        break;

                    case HttpStatusCode.Forbidden:
                        ex = new ForbiddenErrorServiceClientException(error.Code, error.Message);
                        break;

                    case HttpStatusCode.NotFound:
                        ex = new NotFoundErrorServiceClientException(error.Code, error.Message);
                        break;

                    default:
                        ex = new ErrorServiceClientException(failMessage.StatusCode, error.Code, error.Message);
                        break;
                    }
                }
                else if (payloadType == DtoHelper.ValidationErrorPayloadType)
                {
                    var validationError = JsonConvert.DeserializeObject <ValidationErrorDto>(content);

                    if (failMessage.StatusCode == HttpStatusCode.BadRequest)
                    {
                        ex = new ValidationErrorServiceClientException(
                            validationError.Code,
                            validationError.Message,
                            validationError.Failures);
                    }
                    else
                    {
                        // actually, something is wrong.
                        ex = new ErrorServiceClientException(
                            failMessage.StatusCode,
                            validationError.Code,
                            validationError.Message);
                    }
                }
                else
                {
                    // Generic error.
                    ex = new HttpServiceClientException(failMessage.StatusCode, content);
                }

                return(ex);
            }
            catch (Exception e)
            {
                return(e);
            }
        }