NJsonApiCore icon indicating copy to clipboard operation
NJsonApiCore copied to clipboard

Multi errors

Open knownasilya opened this issue 9 years ago • 2 comments

The json api spec allows an array of errors, but currently this isn't supported here. This would be a useful addition.

knownasilya avatar Nov 08 '16 23:11 knownasilya

Agreed! Thanks for commenting. Most errors coming out of .NET are single exceptions but there does need to be a way for a user to add more error messages onto the output.

brainwipe avatar Nov 09 '16 10:11 brainwipe

The way I dealt with this was to create up a new CustomJsonApiActionFilter that inherits JsonApiActionFilter. I then override OnActionExecuted to return MultiErrorResult immediately if it's found:

public override void OnActionExecuted(ActionExecutedContext context)
{
    if (context.Result == null || context.Result is MultiErrorResult)
    {
        return;
    }
    base.OnActionExecuted(context);
}

MultiErrorResult is simply an ObjectResult that was created to have the same format as the JsonApi calls for:

public class MultiErrorResult : ObjectResult
{
    public MultiErrorResult(List<Error> errors) : base(null)
    {
        StatusCode = (int) HttpStatusCode.BadRequest;
        Value = new {errors = errors};
    }
}

I now can use MultiErrorResult to return validation errors as needed. I get that CustomJsonApiActionFilter is kinda a hack, but I have not yet had time to put together a pull request.

AdamLJohnson avatar Nov 10 '16 02:11 AdamLJohnson