From dfc8beded919956612a78be816c62cd124871e67 Mon Sep 17 00:00:00 2001 From: Artem Levin Date: Thu, 13 Jul 2023 09:46:35 +0500 Subject: [PATCH] Add ApiEndpoint to NetworkException --- src/Arbus.Network/ApiEndpoint.cs | 8 ++++---- src/Arbus.Network/Exceptions/NetworkException.cs | 13 ++++++++++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Arbus.Network/ApiEndpoint.cs b/src/Arbus.Network/ApiEndpoint.cs index d3a0505..43bc7b1 100644 --- a/src/Arbus.Network/ApiEndpoint.cs +++ b/src/Arbus.Network/ApiEndpoint.cs @@ -80,11 +80,11 @@ private async Task EnsureSuccessResponse(HttpResponseMessage responseMessage) public virtual Task HandleNotSuccessStatusCode(HttpResponseMessage responseMessage) { if (responseMessage.Content.Headers.ContentType?.MediaType == HttpContentType.Application.ProblemJson) - return HandleProblemDetailsResponse(responseMessage); + return HandleProblemDetailsResponse(responseMessage, this); return HandleAnyResponse(responseMessage); } - public static async Task HandleProblemDetailsResponse(HttpResponseMessage responseMessage) + public static async Task HandleProblemDetailsResponse(HttpResponseMessage responseMessage, ApiEndpoint endpoint) { var responseStream = await responseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false); @@ -92,13 +92,13 @@ public static async Task HandleProblemDetailsResponse(HttpResponseMessage respon responseStream, GlobalJsonSerializerOptions.Options).ConfigureAwait(false) ?? throw new Exception("Failed to deserialize ProblemDetails."); - throw new NetworkException(responseMessage.StatusCode, problemDetails); + throw new NetworkException(responseMessage.StatusCode, problemDetails, endpoint); } public virtual async Task HandleAnyResponse(HttpResponseMessage responseMessage) { var responseString = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); - throw new NetworkException(responseMessage.StatusCode, responseString); + throw new NetworkException(responseMessage.StatusCode, responseString, this); } } diff --git a/src/Arbus.Network/Exceptions/NetworkException.cs b/src/Arbus.Network/Exceptions/NetworkException.cs index d4e4816..8059155 100644 --- a/src/Arbus.Network/Exceptions/NetworkException.cs +++ b/src/Arbus.Network/Exceptions/NetworkException.cs @@ -7,18 +7,25 @@ public class NetworkException : Exception public HttpStatusCode? StatusCode { get; } public ProblemDetails? ProblemDetails { get; } + + public ApiEndpoint? Endpoint { get; } - public NetworkException(HttpStatusCode httpStatusCode, ProblemDetails problemDetails) - : this(httpStatusCode, problemDetails.Detail ?? problemDetails.Title ?? string.Empty) + public NetworkException(HttpStatusCode httpStatusCode, ProblemDetails problemDetails, ApiEndpoint endpoint) + : this(httpStatusCode, problemDetails.Detail ?? problemDetails.Title ?? string.Empty, endpoint) { ProblemDetails = problemDetails; } - public NetworkException(HttpStatusCode httpStatusCode, string message) : this(message) + public NetworkException(HttpStatusCode httpStatusCode, string message, ApiEndpoint endpoint) : this(message, endpoint) { StatusCode = httpStatusCode; } + public NetworkException(string message, ApiEndpoint endpoint) : base(message) + { + Endpoint = endpoint; + } + public NetworkException(string message) : base(message) { }