Skip to content

Commit

Permalink
feat: add details to api exception (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashish-s committed Apr 28, 2020
1 parent 3444137 commit ff7e17c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/Twilio/Clients/TwilioRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ private static Response ProcessResponse(Response response)
restException.Code,
(int)response.StatusCode,
restException.Message ?? "Unable to make request, " + response.StatusCode,
restException.MoreInfo
restException.MoreInfo,
restException.Details
);
}

Expand Down
10 changes: 9 additions & 1 deletion src/Twilio/Exceptions/ApiException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace Twilio.Exceptions
{
Expand All @@ -22,6 +23,11 @@ public class ApiException : TwilioException
/// </summary>
public string MoreInfo { get; }

/// <summary>
/// Any more details about the error, if any were provided
/// </summary>
public Dictionary<string, object> Details { get; }

/// <summary>
/// Create a ApiException with message
/// </summary>
Expand All @@ -42,19 +48,21 @@ public class ApiException : TwilioException
/// <param name="status">HTTP status code</param>
/// <param name="message">Error message</param>
/// <param name="moreInfo">More info if provided</param>
/// <param name="details">Details if provided</param>
/// <param name="exception">Original exception</param>
public ApiException(
int code,
int status,
string message,
string moreInfo,
Dictionary<string, object> details = null,
Exception exception = null
) : base(message, exception)
{
Code = code;
Status = status;
MoreInfo = moreInfo;
Details = details;
}
}
}

28 changes: 19 additions & 9 deletions src/Twilio/Exceptions/RestException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Twilio.Exceptions
{
Expand All @@ -23,25 +24,31 @@ public class RestException : TwilioException
/// <summary>
/// Error message
/// </summary>
public override string Message {
public override string Message {
get
{
return _message;
}
{
return _message;
}
}

[JsonProperty("message")]
private string _message
{
get; set;
private string _message
{
get; set;
}

/// <summary>
/// More info if provided
/// </summary>
[JsonProperty("more_info")]
public string MoreInfo { get; private set; }


/// <summary>
/// Details if provided
/// </summary>
[JsonProperty("details")]
public Dictionary<string, object> Details { get; private set; }

/// <summary>
/// Create an empty RestException
/// </summary>
Expand All @@ -54,12 +61,15 @@ private string _message
[JsonProperty("code")]
int code,
[JsonProperty("more_info")]
string moreInfo
string moreInfo,
[JsonProperty("details")]
Dictionary<string, object> details
) {
Status = status;
Code = code;
_message = message;
MoreInfo = moreInfo;
Details = details;
}

/// <summary>
Expand Down
29 changes: 29 additions & 0 deletions test/Twilio.Test/Clients/TwilioRestClientTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Net;
using System.Collections.Generic;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using NUnit.Framework;
Expand Down Expand Up @@ -62,5 +63,33 @@ public void TestNotOkResponse()
Assert.Fail("Threw an unknown exception");
}
}

[Test]
public void TestBadResponseWithDetails()
{
string jsonResponse = @"{
""code"": 20001,
""message"": ""Bad request"",
""more_info"": ""https://www.twilio.com/docs/errors/20001"",
""status"": 400,
""details"": {
""foo"": ""bar""
}}";
client.MakeRequest(Arg.Any<Request>()).Returns(new Response(HttpStatusCode.BadRequest, jsonResponse));
try {
Request request = new Request(HttpMethod.Get,"https://www.contoso.com");
TwilioRestClient twilioClient = new TwilioRestClient("foo", "bar", null, null, client);
twilioClient.Request(request);
Assert.Fail("Should have failed");
} catch (ApiException e) {
Assert.AreEqual("Bad request", e.Message);
Assert.AreEqual(20001, e.Code);
Assert.AreEqual("https://www.twilio.com/docs/errors/20001", e.MoreInfo);
Assert.AreEqual(400, e.Status);
var expectedDetails = new Dictionary<string, object>();
expectedDetails.Add("foo", "bar");
Assert.AreEqual(expectedDetails, e.Details);
}
}
}
}

0 comments on commit ff7e17c

Please sign in to comment.