I have a simple scenario where I have a class with the following method:
public async IAsyncEnumerable<Entity> GetEntities(IQueryOptions options){
if(!validator.ValidateQuery(options)) { throw new ArgumentException(nameof(options));}
var data = dataSource.ReadEntitiesAsync(options);
await foreach (var entity in data) { yield return await converter.ConvertAsync(entity);}
}
Is it possible to have the ArgumentException
thrown exactly at the GetEntities()
method call, and not after first step of iteration like here:
await foreach(var e in GetEntities(options)) { // some code here }
I'm asking because when I want to return IAsyncEnumerable
up to my API controller the exception actually is thrown in the framework code. I have no chance to catch it, and return a HTTP 404 BAD REQUEST code. Surely I can intercept exceptions in the request pipeline, but sometimes I want to wrap them in other exceptions depending on the abstraction layer they come from.