Skip to content

Commit

Permalink
Clearer error messages (#72)
Browse files Browse the repository at this point in the history
* Clearer error messages

* Version bump

* Fixed test
  • Loading branch information
mroloux authored Jan 20, 2023
1 parent 65a9dbf commit bee95ac
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
20 changes: 16 additions & 4 deletions SeatsioDotNet.Test/ErrorHandlingTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Xunit;
using RestSharp;
using SeatsioDotNet.Util;
using Xunit;

namespace SeatsioDotNet.Test
{
Expand All @@ -10,18 +12,28 @@ public void Test4xx()
var e = Assert.Throws<SeatsioException>(() =>
Client.Events.RetrieveObjectInfo("unexistingEvent", "unexistingObject"));

Assert.StartsWith("Get " + BaseUrl + "/events/unexistingEvent/objects?label=unexistingObject resulted in a 404 Not Found response. Reason: Event not found: unexistingEvent. Request ID:", e.Message);
Assert.Contains(new SeatsioApiError("EVENT_NOT_FOUND", "Event not found: unexistingEvent"), e.Errors);
Assert.Equal("Event not found: unexistingEvent.", e.Message);
Assert.Contains(new SeatsioApiError("EVENT_NOT_FOUND", "Event not found: unexistingEvent"), e.Errors);
Assert.NotNull(e.RequestId);
}

[Fact]
public void Test5xx()
{
var client = new SeatsioRestClient("https://httpbin.seatsio.net");

var e = Assert.Throws<SeatsioException>(() => RestUtil.AssertOk(client.Execute<object>(new RestRequest("/status/500"))));

Assert.Equal("Get https://httpbin.seatsio.net/status/500 resulted in a 500 Internal Server Error response. Body: ", e.Message);
}

[Fact]
public void WeirdError()
{
var e = Assert.Throws<SeatsioException>(() =>
new SeatsioClient("", null, "unknownProtocol://").Events.RetrieveObjectInfo("unexistingEvent", "unexistingObject"));

Assert.Equal("Get resulted in a 0 response.", e.Message);
Assert.Equal("Get resulted in a 0 response. Body: ", e.Message);
Assert.Null(e.Errors);
Assert.Null(e.RequestId);
}
Expand Down
2 changes: 1 addition & 1 deletion SeatsioDotNet.Test/Events/DeleteEventTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Delete()

Exception ex = Assert.Throws<SeatsioException>(() => Client.Events.Retrieve(evnt.Key));

Assert.Contains("404", ex.Message);
Assert.Equal("Event not found: " + evnt.Key + ".", ex.Message);
}
}
}
4 changes: 2 additions & 2 deletions SeatsioDotNet/RateLimitExceededException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace SeatsioDotNet
{
public class RateLimitExceededException : SeatsioException
{
public RateLimitExceededException(List<SeatsioApiError> errors, string requestId, RestResponse response) :
base(errors, requestId, response)
public RateLimitExceededException(List<SeatsioApiError> errors, string requestId) :
base(errors, requestId)
{
}
}
Expand Down
2 changes: 1 addition & 1 deletion SeatsioDotNet/SeatsioDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<IsPackable>true</IsPackable>
<Version>93.2.0</Version>
<Version>93.3.0</Version>
<Authors>mroloux;bverbeken</Authors>
<Title>Official Seats.io .NET API client</Title>
<Description>Official Seats.io .NET API client</Description>
Expand Down
22 changes: 11 additions & 11 deletions SeatsioDotNet/SeatsioException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class SeatsioException : Exception
public readonly List<SeatsioApiError> Errors;
public readonly string RequestId;

protected SeatsioException(List<SeatsioApiError> errors, string requestId, RestResponse response) : base(ExceptionMessage(errors, requestId, response))
protected SeatsioException(List<SeatsioApiError> errors, string requestId) : base(
ExceptionMessage(errors))
{
Errors = errors;
RequestId = requestId;
Expand All @@ -21,30 +22,29 @@ private SeatsioException(RestResponse response) : base(ExceptionMessage(response
{
}

private static string ExceptionMessage(List<SeatsioApiError> errors, string requestId, RestResponse response)
private static string ExceptionMessage(List<SeatsioApiError> errors)
{
string exceptionMessage = ExceptionMessage(response);
exceptionMessage += " Reason: " + String.Join(", ", errors.Select(x => x.Message)) + ".";
exceptionMessage += " Request ID: " + requestId;
return exceptionMessage;
return String.Join(", ", errors.Select(x => x.Message)) + ".";
}

private static string ExceptionMessage(RestResponse response)
{
var request = response.Request;
return request.Method + " " + response.ResponseUri + " resulted in a " + (int) response.StatusCode + " " + response.StatusDescription + " response.";
return request.Method + " " + response.ResponseUri + " resulted in a " + (int) response.StatusCode + " " + response.StatusDescription + " response. Body: " + response.Content;
}

public static SeatsioException From(RestResponse response)
{
if (response.ContentType != null && response.ContentType.Contains("application/json"))
{
var parsedException = JsonSerializer.Deserialize<SeatsioExceptionTo>(response.Content, SeatsioRestClient.SeatsioJsonSerializerOptions());
var parsedException = JsonSerializer.Deserialize<SeatsioExceptionTo>(response.Content,
SeatsioRestClient.SeatsioJsonSerializerOptions());
if ((int) response.StatusCode == 429)
{
return new RateLimitExceededException(parsedException.Errors, parsedException.RequestId, response);
return new RateLimitExceededException(parsedException.Errors, parsedException.RequestId);
}
return new SeatsioException(parsedException.Errors, parsedException.RequestId, response);

return new SeatsioException(parsedException.Errors, parsedException.RequestId);
}

return new SeatsioException(response);
Expand Down

0 comments on commit bee95ac

Please sign in to comment.