-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#372: Throw a MollieApiException for all types of unsuccesfull API re…
…sponse codes (#374) * #372: Throw a MollieApiException for all type of unsuccesfull API response codes * #372: Move error parsing from MollieApiException to BaseMollieClient
- Loading branch information
1 parent
6e37edc
commit 52f2f65
Showing
4 changed files
with
109 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,12 @@ | ||
using System; | ||
using Mollie.Api.Models.Error; | ||
using Newtonsoft.Json; | ||
|
||
namespace Mollie.Api.Client { | ||
public class MollieApiException : Exception { | ||
public MollieErrorMessage Details { get; set; } | ||
|
||
public MollieApiException(string json) : base(ParseErrorMessage(json).ToString()){ | ||
Details = ParseErrorMessage(json); | ||
} | ||
|
||
private static MollieErrorMessage ParseErrorMessage(string json) { | ||
return JsonConvert.DeserializeObject<MollieErrorMessage>(json)!; | ||
public MollieApiException(MollieErrorMessage details) : base(details.ToString()) { | ||
Details = details; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Mime; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Mollie.Api.Client; | ||
using Mollie.Api.Models; | ||
using Mollie.Api.Models.Payment.Request; | ||
using Xunit; | ||
|
||
namespace Mollie.Tests.Unit.Client; | ||
|
||
public class BaseMollieClientTests : BaseClientTests { | ||
[Fact] | ||
public async Task HttpResponseStatusCodeIsNotSuccesfull_ResponseBodyContainsMollieErrorDetails_MollieApiExceptionIsThrown() { | ||
|
||
// Arrange | ||
const string errorMessage = "A validation error occured"; | ||
const int errorStatus = (int)HttpStatusCode.UnprocessableEntity; | ||
string responseBody = @$"{{ | ||
""_links"": {{ | ||
""documentation"": {{ | ||
""href"": ""https://docs.mollie.com/overview/handling-errors"", | ||
""type"": ""text/html"" | ||
}} | ||
}}, | ||
""detail"": ""{errorMessage}"", | ||
""status"": {errorStatus}, | ||
""title"": ""Error"" | ||
}}"; | ||
const string expectedUrl = $"{BaseMollieClient.ApiEndPoint}payments"; | ||
var mockHttp = CreateMockHttpMessageHandler( | ||
HttpMethod.Post, | ||
expectedUrl, | ||
responseBody, | ||
responseStatusCode: HttpStatusCode.UnprocessableEntity); | ||
HttpClient httpClient = mockHttp.ToHttpClient(); | ||
PaymentClient paymentClient = new("api-key", httpClient); | ||
PaymentRequest paymentRequest = new() { | ||
Amount = new Amount(Currency.EUR, 50m), | ||
Description = "description" | ||
}; | ||
|
||
// Act | ||
var exception = await Assert.ThrowsAsync<MollieApiException>(() => paymentClient.CreatePaymentAsync(paymentRequest)); | ||
|
||
// Assert | ||
exception.Details.Detail.Should().Be(errorMessage); | ||
exception.Details.Status.Should().Be(errorStatus); | ||
} | ||
|
||
[Fact] | ||
public async Task HttpResponseStatusCodeIsNotSuccesfull_ResponseBodyContainsHtml_MollieApiExceptionIsThrown() { | ||
// Arrange | ||
string responseBody = "<html><body>Whoops!</body></html>"; | ||
const string expectedUrl = $"{BaseMollieClient.ApiEndPoint}payments"; | ||
var mockHttp = CreateMockHttpMessageHandler( | ||
HttpMethod.Post, | ||
expectedUrl, | ||
responseBody, | ||
responseContentType: MediaTypeNames.Text.Html, | ||
responseStatusCode: HttpStatusCode.UnprocessableEntity); | ||
HttpClient httpClient = mockHttp.ToHttpClient(); | ||
PaymentClient paymentClient = new("api-key", httpClient); | ||
PaymentRequest paymentRequest = new() { | ||
Amount = new Amount(Currency.EUR, 50m), | ||
Description = "description" | ||
}; | ||
|
||
// Act | ||
var exception = await Assert.ThrowsAsync<MollieApiException>(() => paymentClient.CreatePaymentAsync(paymentRequest)); | ||
|
||
// Assert | ||
exception.Details.Detail.Should().Be(responseBody); | ||
exception.Details.Status.Should().Be((int)HttpStatusCode.UnprocessableEntity); | ||
} | ||
} |