Skip to content

Instantly share code, notes, and snippets.

@CoolGoose
Created May 1, 2018 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CoolGoose/0ea96db498d0c9933af476ff6dc5156a to your computer and use it in GitHub Desktop.
Save CoolGoose/0ea96db498d0c9933af476ff6dc5156a to your computer and use it in GitHub Desktop.
Simple JSON API ASP .NET Core Error handling class
using Newtonsoft.Json.Linq;
using System.Linq;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Craidd.Helpers
{
/// <summary>
/// Manage JSON Api response
/// </summary>
public class ApiResponseHelper : IApiResponseHelper
{
public JObject ErrorReponse { get; set; } = new JObject(new JProperty("errors", new JArray()));
/// <summary>
/// Create a JSON API Error Response
/// </summary>
/// <param name="title"></param>
/// <param name="detail"></param>
/// <param name="code"></param>
/// <param name="sourceParameter"></param>
/// <returns></returns>
public ApiResponseHelper AddErrorResponse(string title = null, string detail = null, string code = null, string sourceParameter = null)
{
var error = new JObject();
if (! (title is null) )
{
error.Add(new JProperty("title", title));
}
if (! (detail is null) )
{
error.Add(new JProperty("detail", detail));
}
if (! (code is null) )
{
error.Add(new JProperty("code", code));
}
if (! (sourceParameter is null) )
{
error.Add(new JProperty("source", new JObject( new JProperty("parameter", sourceParameter))));
}
ErrorReponse["errors"].Value<JArray>().Add(error);
return this;
}
/// <summary>
/// Parse a ModelState validation into a JSON API Error response
/// </summary>
/// <param name="modelState"></param>
/// <returns></returns>
public ApiResponseHelper ParseModelStateResponse(ModelStateDictionary modelState)
{
var errorData = modelState.Where(ms => ms.Value.Errors.Any())
.Select(x => new { x.Key, x.Value.Errors });
foreach (var stateError in errorData)
{
var errorMessages = stateError.Errors;
foreach (var errorMessage in errorMessages)
{
AddErrorResponse(sourceParameter: stateError.Key, detail: errorMessage.ErrorMessage);
}
}
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment