public void GivenExceptionWhenContentHasSerializedErrorAndLevelOfDetailsIsStandardThenReturnInteralErrorsOnly()
        {
            // Arrange
            var sut       = new ApiExceptionErrorBuilder();
            var exception = BuildApiException("{\"Message\":\"My Error!\",\"UserErrors\":[],\"RawErrors\":[]}");

            // Act
            var result = sut.Build(exception, new List <Error>
            {
                new Error("InternalError", "Exception")
            }, false);

            // Assert
            Assert.Equal(
                "GET \"api/health\" failed because response status code does not indicate success: 400 (reason).",
                result.Message);
            Assert.Equal("RestEase.ApiException", result.ErrorType);
            Assert.Collection(result.ChildErrors,
                              ep =>
            {
                Assert.Collection(ep.ChildErrors,
                                  e =>
                {
                    Assert.Equal("InternalError", e.Message);
                    Assert.Equal("Exception", e.ErrorType);
                });
            });
        }
        public void GivenAnyExceptionWhenExceptionIsNotSuppliedThenBuilderShouldThrow()
        {
            // Arrange
            var sut = new ApiExceptionErrorBuilder();

            // Act
            // Assert
            Assert.Throws <ArgumentNullException>(
                () => sut.Build(null, new List <Error>(), false));
        }
        public void GivenAnyExceptionWhenInternalErrorsAreNotSuppliedThenBuilderShouldThrow()
        {
            // Arrange
            var sut = new ApiExceptionErrorBuilder();

            // Act
            // Assert
            Assert.Throws <ArgumentNullException>(
                () => sut.Build(
                    BuildApiException(), null, false));
        }
        public void GivenExceptionWhenNoContentFoundThenErrorWithMessageShouldBeReturned()
        {
            // Arrange
            var sut            = new ApiExceptionErrorBuilder();
            var exception      = BuildApiException();
            var internalErrors = new List <Error>();

            // Act
            var result = sut.Build(exception, internalErrors, false);

            // Assert
            Assert.Equal(
                "GET \"api/health\" failed because response status code does not indicate success: 400 (reason).",
                result.Message);
            Assert.Equal("RestEase.ApiException", result.ErrorType);
            Assert.Same(internalErrors, result.ChildErrors);
        }
        public void GivenExceptionWhenContentHasMalformedErrorThenReturnErrorWithContentShouldBeReturned()
        {
            // Arrange
            var sut            = new ApiExceptionErrorBuilder();
            var exception      = BuildApiException("{\"Message\":\"My Error!\",\"UserErrors\":[XXXXXX],\"RawErrors\":[]}");
            var internalErrors = new List <Error>();

            // Act
            var result = sut.Build(exception, internalErrors, false);

            // Assert
            Assert.Equal(
                "GET \"api/health\" failed because response status code does not indicate success: 400 (reason).",
                result.Message);
            Assert.Equal("RestEase.ApiException", result.ErrorType);
            Assert.Collection(result.ChildErrors,
                              e =>
            {
                Assert.Equal("{\"Message\":\"My Error!\",\"UserErrors\":[XXXXXX],\"RawErrors\":[]}", e.Message);
                Assert.Equal("RestEase.ApiException", e.ErrorType);
                Assert.Same(internalErrors, e.ChildErrors);
            });
        }
        GivenExceptionWhenContentHasSerializedErrorAndLevelOfDetailsIsDetailedErrorsThenReturnInteralErrorsAndDetailedErrors()
        {
            // Arrange
            var content =
                "{\"Message\":\"My Error!\"," +
                "\"UserErrors\":[ { \"Message\":\"UserMessage\", \"ErrorType\":\"ExternalUserError\", \"ChildErrors\":[] } ]," +
                "\"RawErrors\":[ { \"Message\":\"RawMessage\", \"ErrorType\":\"ExternalRawError\", \"ChildErrors\":[] } ]}";

            var sut       = new ApiExceptionErrorBuilder();
            var exception = BuildApiException(content);

            // Act
            var result = sut.Build(exception, new List <Error>
            {
                new Error("InternalError", "Exception")
            }, true);

            // Assert
            Assert.Equal(
                "GET \"api/health\" failed because response status code does not indicate success: 400 (reason).",
                result.Message);
            Assert.Equal("RestEase.ApiException", result.ErrorType);
            Assert.Collection(result.ChildErrors,
                              ep =>
            {
                Assert.Collection(ep.ChildErrors,
                                  e =>
                {
                    Assert.Equal("InternalError", e.Message);
                    Assert.Equal("Exception", e.ErrorType);
                }, e =>
                {
                    Assert.Equal("RawMessage", e.Message);
                    Assert.Equal("ExternalRawError", e.ErrorType);
                });
            });
        }
        public void GivenExceptionWhenContentDoesntHaveRequiredKeywordsThenErrorWithContentShouldBeReturned(
            string content)
        {
            // Arrange
            var sut            = new ApiExceptionErrorBuilder();
            var exception      = BuildApiException(content);
            var internalErrors = new List <Error>();

            // Act
            var result = sut.Build(exception, internalErrors, false);

            // Assert
            Assert.Equal(
                "GET \"api/health\" failed because response status code does not indicate success: 400 (reason).",
                result.Message);
            Assert.Equal("RestEase.ApiException", result.ErrorType);
            Assert.Collection(result.ChildErrors,
                              e =>
            {
                Assert.Equal(content, e.Message);
                Assert.Equal("RestEase.ApiException", e.ErrorType);
                Assert.Same(internalErrors, e.ChildErrors);
            });
        }