Skip to content

Commit

Permalink
#372: Move error parsing from MollieApiException to BaseMollieClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Viincenttt committed Jun 30, 2024
1 parent 183ec7a commit 8831e1b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
18 changes: 17 additions & 1 deletion src/Mollie.Api/Client/BaseMollieClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -92,7 +94,8 @@ private async Task<T> ProcessHttpResponseMessage<T>(HttpResponseMessage response
return _jsonConverterService.Deserialize<T>(resultContent)!;
}

throw new MollieApiException(response.StatusCode, resultContent);
MollieErrorMessage errorDetails = ParseMollieErrorMessage(response.StatusCode, resultContent);
throw new MollieApiException(errorDetails);
}

protected void ValidateApiKeyIsOauthAccesstoken(bool isConstructor = false) {
Expand Down Expand Up @@ -150,6 +153,19 @@ private string GetUserAgent() {
return $"{packageName}/{versionNumber}";
}

private MollieErrorMessage ParseMollieErrorMessage(HttpStatusCode responseStatusCode, string responseBody) {
try {
return _jsonConverterService.Deserialize<MollieErrorMessage>(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");
Expand Down
20 changes: 2 additions & 18 deletions src/Mollie.Api/Client/MollieApiException.cs
Original file line number Diff line number Diff line change
@@ -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<MollieErrorMessage>(responseBody)!;
}
catch (JsonReaderException) {
return new MollieErrorMessage {
Title = "Unknown error",
Status = (int)httpStatusCode,
Detail = responseBody
};
}
public MollieApiException(MollieErrorMessage details) : base(details.ToString()) {
Details = details;
}
}
}

0 comments on commit 8831e1b

Please sign in to comment.