AutoQueryable
AutoQueryable copied to clipboard
Null reference exception if exception thrown in action method
The AutoQueryableFilter in ASPNETCORE.Filter throws a null reference exception if the action throws an exception. Filter should check context.Exception and context.ExceptionHandled before invoking filter behaviour?
hi @springyboy
Probably yes, in which case do you have an exception ? Could you make a PR with that feature ?
Thanks
Version: 2.0.5-beta
Encountered this issue as well and it's bad. We use exceptions and filters to easily return some specific errors to front-end.
For example, we might have this controller:
[HttpGet, AutoQueryable]
public IQueryable<object> Api(bool isError)
{
if (isError) throw new BusinessException("Is error is true!");
return new[] { new {} }.AsQueryable();
}
We have a global filter that turns BusinessException
into a 406
response with the error message in body, which is then handled by the front-end and displayed as a specific error.
The problem here is that throwing any exception is caught by AutoQueryable
filter, which then throws NullReferenceException
.
A better behavior, as suggested by @springyboy, would be to do nothing (passthrough) when context.Exception
is set.
Another minor problem is when the controller returns null
. The filter throws a very generic Exception
with message "Unable to retrieve value of IQueryable from context result".
You can say that the contract for AutoQueryable
is a non-null value, but it might be more useful to consider null
as empty and automatically return an empty array (or wrapper with count: 0).
Alternatively you could let null passthrough as well (which by default turns into a 204 No content
response).