-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve error information with tailored format
- Loading branch information
Showing
33 changed files
with
389 additions
and
229 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#region | ||
using System.Net; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using Vonage.Common; | ||
using Vonage.Common.Client; | ||
using Vonage.Common.Failures; | ||
using Vonage.Common.Monads; | ||
using Vonage.Serialization; | ||
using Vonage.Test.Common.Extensions; | ||
using Vonage.Test.Common.TestHelpers; | ||
using Xunit; | ||
#endregion | ||
|
||
namespace Vonage.Test.Common.Client; | ||
|
||
[Trait("Category", "Unit")] | ||
public class ApiErrorsTest | ||
{ | ||
private readonly SerializationTestHelper helper = new SerializationTestHelper(typeof(ApiErrorsTest).Namespace, | ||
JsonSerializerBuilder.BuildWithSnakeCase()); | ||
|
||
private readonly JsonSerializer serializer = new JsonSerializer(); | ||
|
||
[Fact] | ||
public async Task ShouldReturnErrorContent_GivenVideoApiError() | ||
{ | ||
var expectedJson = this.helper.GetResponseJson("ApiErrorVideo"); | ||
var sut = this.BuildClient<VideoApiError>(expectedJson); | ||
var result = await sut.SendAsync(BuildFakeRequest()); | ||
result.Should().BeFailure(HttpFailure.From(HttpStatusCode.BadRequest, | ||
"You do not pass in a session ID or you pass in an invalid session ID.", expectedJson)); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldReturnErrorContent_GivenNetworkApiError() | ||
{ | ||
var expectedJson = this.helper.GetResponseJson("ApiErrorNetwork"); | ||
var sut = this.BuildClient<NetworkApiError>(expectedJson); | ||
var result = await sut.SendAsync(BuildFakeRequest()); | ||
result.Should().BeFailure(HttpFailure.From(HttpStatusCode.BadRequest, | ||
"Client specified an invalid argument, request body or query param", expectedJson)); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldReturnErrorContent_GivenStandardApiError() | ||
{ | ||
var expectedJson = this.helper.GetResponseJson("ApiErrorStandard"); | ||
var sut = this.BuildClient<StandardApiError>(expectedJson); | ||
var result = await sut.SendAsync(BuildFakeRequest()); | ||
result.Should().BeFailure(HttpFailure.From(HttpStatusCode.BadRequest, | ||
"You did not provide correct credentials.", expectedJson)); | ||
} | ||
|
||
private static Result<VonageHttpClientTest.FakeRequest> BuildFakeRequest() => | ||
Result<VonageHttpClientTest.FakeRequest>.FromSuccess(new VonageHttpClientTest.FakeRequest()); | ||
|
||
private VonageHttpClient<T> BuildClient<T>(string responseContent) where T : IApiError | ||
{ | ||
var messageHandler = FakeHttpRequestHandler.Build(HttpStatusCode.BadRequest) | ||
.WithResponseContent(responseContent); | ||
var configuration = new VonageHttpClientConfiguration(messageHandler.ToHttpClient(), | ||
new AuthenticationHeaderValue("Anonymous"), "userAgent"); | ||
return new VonageHttpClient<T>(configuration, this.serializer); | ||
} | ||
} |
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,5 @@ | ||
{ | ||
"status": 400, | ||
"code": "INVALID_ARGUMENT", | ||
"message": "Client specified an invalid argument, request body or query param" | ||
} |
6 changes: 6 additions & 0 deletions
6
Vonage.Test/Common/Client/Data/ApiErrorStandard-response.json
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,6 @@ | ||
{ | ||
"type": "https://developer.vonage.com/api-errors/#unathorized", | ||
"title": "You did not provide correct credentials.", | ||
"detail": "Check that you're using the correct credentials, and that your account has this feature enabled", | ||
"instance": "bf0ca0bf927b3b52e3cb03217e1a1ddf" | ||
} |
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,4 @@ | ||
{ | ||
"code": 400, | ||
"message": "You do not pass in a session ID or you pass in an invalid session ID." | ||
} |
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
Oops, something went wrong.