From 57b91242c34ebde46f1928214bc5b5a7fecc3d12 Mon Sep 17 00:00:00 2001 From: MrSmoke <709976+MrSmoke@users.noreply.github.com> Date: Fri, 17 Nov 2023 17:29:57 +1100 Subject: [PATCH] Add content of response to Error --- src/RestClient/RestClient/src/Error.cs | 1 + .../src/Requests/BaseRestClientRequest.cs | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/RestClient/RestClient/src/Error.cs b/src/RestClient/RestClient/src/Error.cs index efd461f..0317a7a 100644 --- a/src/RestClient/RestClient/src/Error.cs +++ b/src/RestClient/RestClient/src/Error.cs @@ -11,6 +11,7 @@ public Error(HttpStatusCode httpStatusCode, ErrorBody body) } public HttpStatusCode HttpStatusCode { get; } + public string Content { get; set; } public ErrorBody Body { get; } } } diff --git a/src/RestClient/RestClient/src/Requests/BaseRestClientRequest.cs b/src/RestClient/RestClient/src/Requests/BaseRestClientRequest.cs index d97d686..a0f036d 100644 --- a/src/RestClient/RestClient/src/Requests/BaseRestClientRequest.cs +++ b/src/RestClient/RestClient/src/Requests/BaseRestClientRequest.cs @@ -151,15 +151,15 @@ protected async Task GetErrorAsync(HttpResponseMessage message) if (message.IsSuccessStatusCode) return null; - if (message.Content != null) - { - var content = await message.Content.ReadAsStringAsync().ConfigureAwait(false); + var contentString = await message.Content.ReadAsStringAsync().ConfigureAwait(false); - if (TryParseErrorBody(content, out var errorBody)) - return new Error(message.StatusCode, errorBody); - } + if (!TryParseErrorBody(contentString, out var errorBody)) + errorBody = GetDefaultErrorBody(message.StatusCode, message.ReasonPhrase); - return new Error(message.StatusCode, GetDefaultErrorBody(message.StatusCode, message.ReasonPhrase)); + return new Error(message.StatusCode, errorBody) + { + Content = contentString + }; } protected virtual void HandleError(Error error)