Skip to content

Commit

Permalink
get error response as dynamic with Newtonsoft
Browse files Browse the repository at this point in the history
missed in earlier back-ports from 3.x
  • Loading branch information
Todd committed Dec 8, 2023
1 parent 4361a2d commit d0a43cf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Flurl.Http.Newtonsoft/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Flurl.Http.Newtonsoft
public static class ExtensionMethods
{
/// <summary>
/// Deserializes JSON-formatted HTTP response body to a dynamic object.
/// Deserializes a JSON-formatted HTTP response body to a dynamic object.
/// </summary>
/// <returns>A Task whose result is a dynamic object containing data in the response body.</returns>
public static async Task<dynamic> GetJsonAsync(this IFlurlResponse resp) {
Expand All @@ -22,7 +22,7 @@ public static async Task<dynamic> GetJsonAsync(this IFlurlResponse resp) {
}

/// <summary>
/// Deserializes JSON-formatted HTTP response body to a list of dynamic objects.
/// Deserializes a JSON-formatted HTTP response body to a list of dynamic objects.
/// </summary>
/// <returns>A Task whose result is a list of dynamic objects containing data in the response body.</returns>
public static async Task<IList<dynamic>> GetJsonListAsync(this IFlurlResponse resp) {
Expand All @@ -31,7 +31,7 @@ public static async Task<IList<dynamic>> GetJsonListAsync(this IFlurlResponse re
}

/// <summary>
/// Deserializes JSON-formatted HTTP response body to a dynamic object. Intended to chain off an async call.
/// Deserializes a JSON-formatted HTTP response body to a dynamic object. Intended to chain off an async call.
/// </summary>
/// <returns>A Task whose result is a dynamic object containing data in the response body.</returns>
public static async Task<dynamic> ReceiveJson(this Task<IFlurlResponse> response) {
Expand All @@ -41,7 +41,7 @@ public static async Task<dynamic> ReceiveJson(this Task<IFlurlResponse> response
}

/// <summary>
/// Deserializes JSON-formatted HTTP response body to a list of dynamic objects. Intended to chain off an async call.
/// Deserializes a JSON-formatted HTTP response body to a list of dynamic objects. Intended to chain off an async call.
/// </summary>
/// <returns>A Task whose result is a list of dynamic objects containing data in the response body.</returns>
public static async Task<IList<dynamic>> ReceiveJsonList(this Task<IFlurlResponse> response) {
Expand All @@ -50,6 +50,13 @@ public static async Task<IList<dynamic>> ReceiveJsonList(this Task<IFlurlRespons
return await resp.GetJsonListAsync().ConfigureAwait(false);
}

/// <summary>
/// Deserializes a JSON-formatted error response body to a dynamic object.
/// </summary>
/// <returns>A Task whose result is a dynamic object containing data in the response body.</returns>
public static async Task<dynamic> GetResponseJsonAsync(this FlurlHttpException flurlException) =>
(flurlException.Call?.Response == null) ? null : await flurlException.Call.Response.GetJsonAsync().ConfigureAwait(false);

/// <summary>
/// Shortcut to use NewtonsoftJsonSerializer with this IFlurlClientBuilder.
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions test/Flurl.Test/Http/NewtonsoftTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ public async Task null_response_returns_null_dynamic() {
var list = await resp.ReceiveJsonList();
Assert.IsNull(list);
}

[TestCase(false)]
[TestCase(true)]
public async Task can_get_error_json_untyped(bool useShortcut) {
HttpTest.RespondWithJson(new { code = 999, message = "our server crashed" }, 500);

try {
await "http://api.com".GetStringAsync();
}
catch (FlurlHttpException ex) {
var error = useShortcut ? // error is a dynamic this time
await ex.GetResponseJsonAsync() :
await ex.Call.Response.GetJsonAsync();
Assert.IsNotNull(error);
Assert.AreEqual(999, error.code);
Assert.AreEqual("our server crashed", error.message);
}
}
}

[TestFixture, Parallelizable]
Expand Down

0 comments on commit d0a43cf

Please sign in to comment.