Skip to content
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

Null reference exception if exception thrown in action method #75

Open
springyboy opened this issue Oct 3, 2018 · 2 comments
Open

Null reference exception if exception thrown in action method #75

springyboy opened this issue Oct 3, 2018 · 2 comments

Comments

@springyboy
Copy link

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?

@trenoncourt
Copy link
Owner

hi @springyboy

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

Thanks

@jods4
Copy link

jods4 commented Jul 5, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants