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: You can also force the HttpResponseException to be thrown from your action by buffering the collection like this with the ToArray: public IQueryable<int> Get() { Task<int>[] tasks = new Task<int>[] { Task.Factory.StartNew<int>(() => { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); }) }; return tasks.Select(t => t.Result).ToArray().AsQueryable(); }
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: You can also force the HttpResponseException to be thrown from your action by buffering the collection like this with the ToArray: public IQueryable<int> Get() { Task<int>[] tasks = new Task<int>[] { Task.Factory.StartNew<int>(() => { throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)); }) }; return tasks.Select(t => t.Result).ToArray().AsQueryable(); }