diff --git a/Activout.RestClient.Test.Json/RestClientTests.cs b/Activout.RestClient.Test.Json/RestClientTests.cs index c642a41..0bf9324 100644 --- a/Activout.RestClient.Test.Json/RestClientTests.cs +++ b/Activout.RestClient.Test.Json/RestClientTests.cs @@ -112,6 +112,7 @@ public async Task TestErrorAsync(JsonImplementation jsonImplementation) Assert.Equal(HttpStatusCode.NotFound, exception.StatusCode); var error = exception.GetErrorResponse(); + Assert.NotNull(error); Assert.Equal(34, error.Errors[0].Code); Assert.Equal("Sorry, that page does not exist", error.Errors[0].Message); } @@ -165,6 +166,7 @@ public void TestErrorSync(JsonImplementation jsonImplementation) Assert.Equal("Sorry, that page does not exist", message); var error = exception.GetErrorResponse(); + Assert.NotNull(error); Assert.Equal(34, error.Errors[0].Code); Assert.Equal("Sorry, that page does not exist", error.Errors[0].Message); } diff --git a/Activout.RestClient.Xml.Test/ErrorResponseXmlTest.cs b/Activout.RestClient.Xml.Test/ErrorResponseXmlTest.cs index 8b84a26..74d0ff9 100644 --- a/Activout.RestClient.Xml.Test/ErrorResponseXmlTest.cs +++ b/Activout.RestClient.Xml.Test/ErrorResponseXmlTest.cs @@ -51,6 +51,7 @@ public async Task TestErrorResponse_Xml_BadRequest(string mediaType) Assert.NotNull(exception.ErrorResponse); Assert.IsType(exception.ErrorResponse); var errorResponse = exception.GetErrorResponse(); + Assert.NotNull(errorResponse); Assert.Equal(400, errorResponse.Code); Assert.Equal("Invalid request parameter", errorResponse.Message); } diff --git a/Activout.RestClient/RestClientException.cs b/Activout.RestClient/RestClientException.cs index 66084aa..a2711d1 100644 --- a/Activout.RestClient/RestClientException.cs +++ b/Activout.RestClient/RestClientException.cs @@ -1,39 +1,40 @@ -#nullable disable using System; using System.Net; -namespace Activout.RestClient +namespace Activout.RestClient; + +public class RestClientException : Exception { - public class RestClientException : Exception + public RestClientException(Uri? requestUri, HttpStatusCode statusCode, object? errorResponse) + : base(errorResponse?.ToString()) { - public RestClientException(Uri requestUri, HttpStatusCode statusCode, object errorResponse) : base(errorResponse?.ToString()) - { - RequestUri = requestUri; - StatusCode = statusCode; - ErrorResponse = errorResponse; - } + RequestUri = requestUri; + StatusCode = statusCode; + ErrorResponse = errorResponse; + } - public RestClientException(Uri requestUri, HttpStatusCode statusCode, string errorResponse, Exception innerException) : base( - errorResponse, innerException) - { - RequestUri = requestUri; - StatusCode = statusCode; - ErrorResponse = errorResponse; - } + public RestClientException(Uri? requestUri, HttpStatusCode statusCode, string? errorResponse, + Exception? innerException) + : base(errorResponse, innerException) + { + RequestUri = requestUri; + StatusCode = statusCode; + ErrorResponse = errorResponse; + } - public Uri RequestUri { get; } - public HttpStatusCode StatusCode { get; } + public Uri? RequestUri { get; } + public HttpStatusCode StatusCode { get; } - public object ErrorResponse { get; } + public object? ErrorResponse { get; } - public T GetErrorResponse() - { - return (T)ErrorResponse; - } + public T? GetErrorResponse() + { + return (T?)ErrorResponse; + } - public override string ToString() - { - return $"{base.ToString()}, {nameof(RequestUri)}: {RequestUri}, {nameof(StatusCode)}: {StatusCode}, {nameof(ErrorResponse)}: {ErrorResponse}"; - } + public override string ToString() + { + return + $"{base.ToString()}, {nameof(RequestUri)}: {RequestUri}, {nameof(StatusCode)}: {StatusCode}, {nameof(ErrorResponse)}: {ErrorResponse}"; } } \ No newline at end of file