I have constructed a Web API controller with an IQueryable action method as follows.
public class ValuesController : ApiController
{
[Queryable]
IQueryable<int> GetValues()
{
Task<int>[] tasks =
{
Task.Factory.StartNew<int>(() =>
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
})
};
return tasks.Select(t => t.Result).AsQueryable();
}
}
Note that the GetValues() method is designed to raise an HttpResponseException when the Queryable action filter is run. It does not matter that the query values are computed asynchronously; I have also reproduced this by returning an IQueryable<int> implementation that raises the same exception for each property/method on the interface.
When this query is evaluated, a new exception with status code 500 (internal server error) is raised. Since I am explicitly raising and HttpResponseException via my query evaluation, I am expecting that the framework propagate my exception instead.
Comments: I investigated this issue, and I'm relabeling it as a runtime bug. The problem isn't specific to OData by any means. It's just that currently HttpResponseExceptions are only handled when they are thrown by your action, not when your action returns an object that throws an HttpResponseException lazily. I think we can look into fixing this in a future release of WebAPI.
public class ValuesController : ApiController
{
[Queryable]
IQueryable<int> GetValues()
{
Task<int>[] tasks =
{
Task.Factory.StartNew<int>(() =>
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
})
};
return tasks.Select(t => t.Result).AsQueryable();
}
}
Note that the GetValues() method is designed to raise an HttpResponseException when the Queryable action filter is run. It does not matter that the query values are computed asynchronously; I have also reproduced this by returning an IQueryable<int> implementation that raises the same exception for each property/method on the interface.
When this query is evaluated, a new exception with status code 500 (internal server error) is raised. Since I am explicitly raising and HttpResponseException via my query evaluation, I am expecting that the framework propagate my exception instead.
Comments: I investigated this issue, and I'm relabeling it as a runtime bug. The problem isn't specific to OData by any means. It's just that currently HttpResponseExceptions are only handled when they are thrown by your action, not when your action returns an object that throws an HttpResponseException lazily. I think we can look into fixing this in a future release of WebAPI.