Beispiel #1
0
        public static FulcrumException ToFulcrumException(FulcrumError error)
        {
            if (error == null)
            {
                return(null);
            }
            FulcrumException exception;

            switch (error.TypeId)
            {
            case BusinessRuleException.ExceptionTypeId:
                exception = new BusinessRuleException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case ConflictException.ExceptionTypeId:
                exception = new ConflictException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case ServerContractException.ExceptionTypeId:
                exception = new ServerContractException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case NotFoundException.ExceptionTypeId:
                exception = new NotFoundException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case UnauthorizedException.ExceptionTypeId:
                exception = new UnauthorizedException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case AssertionFailedException.ExceptionTypeId:
                exception = new AssertionFailedException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case NotImplementedException.ExceptionTypeId:
                exception = new NotImplementedException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            case TryAgainException.ExceptionTypeId:
                exception = new TryAgainException(error.TechnicalMessage, ToFulcrumException(error.InnerError));
                break;

            default:
                exception = null;
                break;
            }
            if (exception == null)
            {
                var message = $"The TypeId ({error.TypeId}) was not recognized: {error.ToJsonString(Formatting.Indented)}";
                return(new AssertionFailedException(message, ToFulcrumException(error.InnerError)));
            }
            exception.CopyFrom(error);
            return(exception);
        }
Beispiel #2
0
        public static FulcrumError ToFulcrumError(Exception e)
        {
            var fulcrumException = e as FulcrumException;

            if (fulcrumException == null)
            {
                return(null);
            }
            var error = new FulcrumError();

            error.CopyFrom(fulcrumException);
            error.InnerError = ToFulcrumError(fulcrumException.InnerException);
            return(error);
        }
Beispiel #3
0
        private static void ValidateStatusCode(HttpStatusCode statusCode, FulcrumError error)
        {
            var expectedStatusCode = ToHttpStatusCode(error);

            if (expectedStatusCode == null)
            {
                throw new AssertionFailedException(
                          $"The TypeId of the content could not be converted to an HTTP status code: {error.ToJsonString(Formatting.Indented)}.");
            }
            if (expectedStatusCode != statusCode)
            {
                throw new AssertionFailedException(
                          $"The HTTP error response had status code {statusCode}, but was expected to have {expectedStatusCode.Value}, due to the TypeId in the content: \"{error.ToJsonString(Formatting.Indented)}");
            }
        }
Beispiel #4
0
        public static async Task <FulcrumException> ToFulcrumExceptionAsync(HttpResponseMessage response)
        {
            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }
            if (response.IsSuccessStatusCode)
            {
                return(null);
            }
            if (response.Content == null)
            {
                return(null);
            }
            var contentAsString = await response.Content?.ReadAsStringAsync();

            if (string.IsNullOrWhiteSpace(contentAsString))
            {
                throw new AssertionFailedException(
                          $"Received an HTTP response with status code {response.StatusCode}. Expected a JSON formatted error as content, but the content was empty.");
            }
            var error = FulcrumError.Parse(contentAsString);

            if (error == null)
            {
                throw new AssertionFailedException(
                          $"Received an HTTP response with status code {response.StatusCode}. Expected a JSON formatted error as content, but the content was \"{contentAsString}\".");
            }
            ValidateStatusCode(response.StatusCode, error);
            var fulcrumException = ToFulcrumException(error);

            if (fulcrumException != null)
            {
                return(fulcrumException);
            }
            var message = $"The TypeId ({error.TypeId}) was not recognized: {error.ToJsonString(Formatting.Indented)}";

            return(new AssertionFailedException(message, ToFulcrumException(error.InnerError)));
        }