Last active
August 18, 2021 21:49
-
-
Save dust63/37ca5011f8d299d95ca716d9868780b4 to your computer and use it in GitHub Desktop.
RefitExtensions: Parse validation api exception to Dictionary<string, IEnumerable<string>>
This file contains hidden or 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
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text.Json; | |
using System.Threading.Tasks; | |
using Refit; | |
namespace Refit.Extensions | |
{ | |
public static class RefitExtensions | |
{ | |
public static async Task<Dictionary<string, IEnumerable<string>>> GetValidationErrorsAsync(this ApiException refitException) | |
{ | |
if (refitException is ValidationApiException == false) | |
return null; | |
var jsonException = await refitException.GetContentAsAsync<Dictionary<string, JsonElement>>(); | |
if (!jsonException.ContainsKey("errors")) | |
return null; | |
var validationErrors = jsonException["errors"].EnumerateObject().Select(x => x); | |
return validationErrors.ToDictionary(x => x.Name, x => x.Value.EnumerateArray().Select(j => j.GetString())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment