-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customize the exception handling? #91
Comments
Thanks for making this issue. Can you explain what you currently get and how this is different from what you expect? What specifically would you like to customize? Please provide some examples if you can. |
I tried a couple things: My own error rendering: [HttpPost]
[ReturnsResource(typeof (EngineResource))]
[Route("engines/")]
public object Create([FromBody] JObject engineData)
{
try
{
var engine = CurrentUser.Engines.Create(engineData);
return engine;
}
catch (System.Data.Entity.Validation.DbEntityValidationException ex)
{
var errorHandler = new DbEntityValidationExceptionHandler(ex);
var obj = errorHandler.AsErrors();
// TODO: no 422?
var response = Request.CreateResponse(HttpStatusCode.BadRequest, obj);
return response;
}
} but when I passed invalid data I got this as a response:
Which makes sense, I guess, cause Saule is handelling more of the response serializing than I thought. Here is my using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;
namespace API.ExceptionHandlers
{
public class DbEntityValidationExceptionHandler : ExceptionHandler
{
private DbEntityValidationException _exception;
public DbEntityValidationExceptionHandler(DbEntityValidationException e)
: base(e)
{
_exception = e;
}
public object AsErrors()
{
var errors = new List<object>();
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var dbError in _exception.EntityValidationErrors)
{
foreach (var dbErrorEntry in dbError.ValidationErrors)
{
var error = AsError(dbError, dbErrorEntry);
errors.Add(error);
}
}
var obj = new {errors = errors.ToArray()};
return obj;
}
private object AsError(DbEntityValidationResult dbError, DbValidationError dbErrorEntry)
{
var status = "422";
var detail = dbErrorEntry.ErrorMessage;
var source = new {pointer = "/data/attributes/" + dbErrorEntry.PropertyName};
return new {status = status, detail = detail, source = source};
}
}
} Now if I remove the try/catch: [HttpPost]
[ReturnsResource(typeof (EngineResource))]
[Route("engines/")]
public object Create([FromBody] JObject engineData)
{
var engine = CurrentUser.Engines.Create(engineData);
return engine;
} And still post an incomplete object, I get this response:
What I expect an entity errors object to look like is this:
This data is retrieved from
|
Thanks for the detailed response. Indeed, Saule currently only supports single exceptions and doesn't do any advanced handling of them. I will see if this support can be extended somehow. |
Maybe different error renderers could be added to be used in the ApiError class? so instead of doing var json = JObject.FromObject(
error,
new JsonSerializer
{
NullValueHandling = NullValueHandling.Ignore
}); in ErrorSerializer, You'd just do public JObject Serialize(ApiError error)
{
var result = error.ToJObject();
return new JObject { ["errors"] = new JArray { result } };
} and Thoughts? I can try this out in a PR, if you're interested. |
ref: #93 |
More advanced error handling is a must.... particularly for validation errors (as mentioned above). Has any progress been made on this? or workarounds? |
Does #172 address this issue (at least partially)? |
Lets say we get a Db validation error
We get an error message like:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
So, EntityValidationErrors should be rendered as an error object:
http://jsonapi.org/format/#error-objects
The text was updated successfully, but these errors were encountered: