Skip to content

Instantly share code, notes, and snippets.

@dust63
Last active August 18, 2021 21:49
Show Gist options
  • Save dust63/37ca5011f8d299d95ca716d9868780b4 to your computer and use it in GitHub Desktop.
Save dust63/37ca5011f8d299d95ca716d9868780b4 to your computer and use it in GitHub Desktop.
RefitExtensions: Parse validation api exception to Dictionary<string, IEnumerable<string>>
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