From 8831e1b06dbdff7441680a48a53f5dfdf33eb76b Mon Sep 17 00:00:00 2001 From: Vincent Date: Sun, 30 Jun 2024 08:32:02 +0200 Subject: [PATCH] #372: Move error parsing from MollieApiException to BaseMollieClient --- src/Mollie.Api/Client/BaseMollieClient.cs | 18 +++++++++++++++++- src/Mollie.Api/Client/MollieApiException.cs | 20 ++------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Mollie.Api/Client/BaseMollieClient.cs b/src/Mollie.Api/Client/BaseMollieClient.cs index e52e6ec1..07755bdd 100644 --- a/src/Mollie.Api/Client/BaseMollieClient.cs +++ b/src/Mollie.Api/Client/BaseMollieClient.cs @@ -9,7 +9,9 @@ using Mollie.Api.Extensions; using Mollie.Api.Framework; using Mollie.Api.Framework.Idempotency; +using Mollie.Api.Models.Error; using Mollie.Api.Models.Url; +using Newtonsoft.Json; namespace Mollie.Api.Client { public abstract class BaseMollieClient : IDisposable { @@ -92,7 +94,8 @@ private async Task ProcessHttpResponseMessage(HttpResponseMessage response return _jsonConverterService.Deserialize(resultContent)!; } - throw new MollieApiException(response.StatusCode, resultContent); + MollieErrorMessage errorDetails = ParseMollieErrorMessage(response.StatusCode, resultContent); + throw new MollieApiException(errorDetails); } protected void ValidateApiKeyIsOauthAccesstoken(bool isConstructor = false) { @@ -150,6 +153,19 @@ private string GetUserAgent() { return $"{packageName}/{versionNumber}"; } + private MollieErrorMessage ParseMollieErrorMessage(HttpStatusCode responseStatusCode, string responseBody) { + try { + return _jsonConverterService.Deserialize(responseBody)!; + } + catch (JsonReaderException) { + return new MollieErrorMessage { + Title = "Unknown error", + Status = (int)responseStatusCode, + Detail = responseBody + }; + } + } + protected void ValidateRequiredUrlParameter(string parameterName, string parameterValue) { if (string.IsNullOrWhiteSpace(parameterValue)) { throw new ArgumentException($"Required URL argument '{parameterName}' is null or empty"); diff --git a/src/Mollie.Api/Client/MollieApiException.cs b/src/Mollie.Api/Client/MollieApiException.cs index 09e3aeb0..d169a4e8 100644 --- a/src/Mollie.Api/Client/MollieApiException.cs +++ b/src/Mollie.Api/Client/MollieApiException.cs @@ -1,28 +1,12 @@ using System; -using System.Net; using Mollie.Api.Models.Error; -using Newtonsoft.Json; namespace Mollie.Api.Client { public class MollieApiException : Exception { public MollieErrorMessage Details { get; set; } - public MollieApiException(HttpStatusCode httpStatusCode, string responseBody) - : base(ParseErrorMessage(httpStatusCode, responseBody).ToString()){ - Details = ParseErrorMessage(httpStatusCode, responseBody); - } - - private static MollieErrorMessage ParseErrorMessage(HttpStatusCode httpStatusCode, string responseBody) { - try { - return JsonConvert.DeserializeObject(responseBody)!; - } - catch (JsonReaderException) { - return new MollieErrorMessage { - Title = "Unknown error", - Status = (int)httpStatusCode, - Detail = responseBody - }; - } + public MollieApiException(MollieErrorMessage details) : base(details.ToString()) { + Details = details; } } }