Skip to content

Commit

Permalink
Merge pull request #13 from viagogo/pretty-exceptions
Browse files Browse the repository at this point in the history
Make ApiException.Message contain error information from the API
  • Loading branch information
akilburge committed Dec 15, 2015
2 parents a68c253 + a3cb1af commit ee6368c
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 20 deletions.
6 changes: 5 additions & 1 deletion ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
### New in 0.5.0 (Releases 2015/11/23)
### New in 0.6.0 (Released 2015/12/15)
* ApiException.Message now returns actual error information from the API
* `purchasepreview:confirm` link added to PurchasePreview resource

### New in 0.5.0 (Released 2015/11/23)
* BearerAuthenticationHandler gets a `client_credentials` token automatically if
the token store doesn't have a token yet

Expand Down
8 changes: 4 additions & 4 deletions SolutionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
[assembly: AssemblyProductAttribute("GogoKit")]
[assembly: AssemblyCompanyAttribute("viagogo")]
[assembly: AssemblyCopyrightAttribute("Copyright viagogo 2015")]
[assembly: AssemblyVersionAttribute("0.5.0")]
[assembly: AssemblyFileVersionAttribute("0.5.0")]
[assembly: AssemblyInformationalVersionAttribute("0.5.0")]
[assembly: AssemblyVersionAttribute("0.6.0")]
[assembly: AssemblyFileVersionAttribute("0.6.0")]
[assembly: AssemblyInformationalVersionAttribute("0.6.0")]
[assembly: ComVisibleAttribute(false)]
namespace System {
internal static class AssemblyVersionInformation {
internal const string Version = "0.5.0";
internal const string Version = "0.6.0";
}
}
11 changes: 11 additions & 0 deletions src/GogoKit/Exceptions/ApiAuthorizationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,16 @@ public ApiAuthorizationException(IApiResponse<AuthorizationError> response) : ba
}

public AuthorizationError AuthorizationError { get; }

public override string Message
{
get
{
var authorizationErrorResponse = Response as IApiResponse<AuthorizationError>;
return authorizationErrorResponse?.BodyAsObject != null
? authorizationErrorResponse.BodyAsObject.ErrorDescription
: base.Message;
}
}
}
}
16 changes: 1 addition & 15 deletions src/GogoKit/Exceptions/ApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,7 @@ public ApiException(IApiResponse response)
Response = response;
}

public override string Message
{
get
{
string message = null;
var authorizationErrorResponse = Response as IApiResponse<AuthorizationError>;
if (authorizationErrorResponse != null &&
authorizationErrorResponse.BodyAsObject != null)
{
message = authorizationErrorResponse.BodyAsObject.ErrorDescription;
}

return message ?? "An error occurred with this API request";
}
}
public override string Message => $"An error occurred with this API request: {Response.Body}";

public IApiResponse Response { get; }
}
Expand Down
6 changes: 6 additions & 0 deletions src/GogoKit/Models/Request/NewPurchasePreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,11 @@ public class NewPurchasePreview

[DataMember(Name = "coupons")]
public string[] Coupons { get; set; }

[DataMember(Name = "confirm_complete_callback")]
public string ConfirmCompleteCallbackUrl { get; set; }

[DataMember(Name = "confirm_cancelled_callback")]
public string ConfirmCancelledCallbackUrl { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/GogoKit/Models/Response/PurchasePreview.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class PurchasePreview : Resource
[DataMember(Name = "estimated_total_charge")]
public Money EstimatedTotalCharge { get; set; }

[Rel("purchasepreview:confirm")]
public Link ConfirmLink { get; set; }

[Rel("purchasepreview:createpurchase")]
public Link CreatePurchaseLink { get; set; }

Expand Down
38 changes: 38 additions & 0 deletions tests/GogoKit.Tests/Exceptions/ApiErrorExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GogoKit.Exceptions;
using GogoKit.Models.Response;
using HalKit.Http;
using NUnit.Framework;

namespace GogoKit.Tests.Exceptions
{
[TestFixture]
public class ApiErrorExceptionTests
{
public static IEnumerable<Type> ApiErrorExceptionTypes
{
get
{
return typeof(ApiErrorException)
.Assembly
.GetTypes()
.Where(t => typeof(ApiErrorException).IsAssignableFrom(t) && t != typeof(ResourceNotFoundException))
.OrderBy(t => t.Name);
}
}

[Test, TestCaseSource(nameof(ApiErrorExceptionTypes))]
public void ApiErrorExceptions_ShouldHaveAPrettyErrorMessage(Type apiErrorExceptionType)
{
var apiResponse = new ApiResponse<ApiError> {Body = "{\"message\":\"pretty message\"}"};
var expectedMessage = $"An error occurred with this API request: {apiResponse.Body}";
var ctor = apiErrorExceptionType.GetConstructor(new[] {typeof(IApiResponse<ApiError>)});

var actualException = ctor.Invoke(new object[] {apiResponse}) as ApiErrorException;

Assert.AreEqual(expectedMessage, actualException.Message);
}
}
}
1 change: 1 addition & 0 deletions tests/GogoKit.Tests/GogoKit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Clients\OAuth2ClientTests.cs" />
<Compile Include="Exceptions\ApiErrorExceptionTests.cs" />
<Compile Include="Fakes\FakeApiResponseFactory.cs" />
<Compile Include="Fakes\FakeLocalizationProvider.cs" />
<Compile Include="Fakes\FakeDelegatingHandler.cs" />
Expand Down

0 comments on commit ee6368c

Please sign in to comment.