From fdd7366627c4640366dd44c2f381f72fb7a17bf8 Mon Sep 17 00:00:00 2001 From: David Eriksson Date: Tue, 28 Oct 2025 09:43:26 +0100 Subject: [PATCH 1/3] Handle nullable in RestClientException --- Activout.RestClient/RestClientException.cs | 55 +++++++++++----------- 1 file changed, 28 insertions(+), 27 deletions(-) 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 From 8b8356b77f1527507d660b43568d8e91be92303c Mon Sep 17 00:00:00 2001 From: David Eriksson Date: Tue, 28 Oct 2025 09:55:33 +0100 Subject: [PATCH 2/3] Fix test cases --- .../RestClientTests.cs | 2 ++ .../ErrorResponseXmlTest.cs | 1 + Activout.RestClient/Part.cs | 20 ++----------------- 3 files changed, 5 insertions(+), 18 deletions(-) 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/Part.cs b/Activout.RestClient/Part.cs index 8a8db93..2beaccc 100644 --- a/Activout.RestClient/Part.cs +++ b/Activout.RestClient/Part.cs @@ -1,19 +1,3 @@ -#nullable disable -namespace Activout.RestClient -{ - public class Part - { - internal object InternalContent { get; set; } - public string Name { get; set; } - public string FileName { get; set; } - } +namespace Activout.RestClient; - public class Part : Part - { - public T Content - { - get => (T)InternalContent; - set => InternalContent = value; - } - } -} \ No newline at end of file +public record Part(object Content, string? Name, string? FileName = null); From a9395c3e488ecab19f562ac0f64165d4f6de1a6a Mon Sep 17 00:00:00 2001 From: David Eriksson Date: Tue, 28 Oct 2025 10:03:23 +0100 Subject: [PATCH 3/3] Revert Part for this PR --- Activout.RestClient/Part.cs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Activout.RestClient/Part.cs b/Activout.RestClient/Part.cs index 2beaccc..8a8db93 100644 --- a/Activout.RestClient/Part.cs +++ b/Activout.RestClient/Part.cs @@ -1,3 +1,19 @@ -namespace Activout.RestClient; +#nullable disable +namespace Activout.RestClient +{ + public class Part + { + internal object InternalContent { get; set; } + public string Name { get; set; } + public string FileName { get; set; } + } -public record Part(object Content, string? Name, string? FileName = null); + public class Part : Part + { + public T Content + { + get => (T)InternalContent; + set => InternalContent = value; + } + } +} \ No newline at end of file