AutoQueryable icon indicating copy to clipboard operation
AutoQueryable copied to clipboard

Null reference exception if exception thrown in action method

Open springyboy opened this issue 6 years ago • 2 comments

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?

springyboy avatar Oct 03 '18 13:10 springyboy

hi @springyboy

Probably yes, in which case do you have an exception ? Could you make a PR with that feature ?

Thanks

trenoncourt avatar Oct 03 '18 17:10 trenoncourt

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).

jods4 avatar Jul 05 '19 16:07 jods4