title: Is there a way to handle asp.net core odata errors
body: <p>Is there a way to handle asp.net core odata errors?</p>
  
  <p>I have a model class <code>DimDateAvailable</code> with one property, a primary key of <code>int DateId</code>, and I make a call like <code>/data/DimDateAvailable?$select=test</code>.</p>
  
  <p>Other calls work as expected and return what I'm after - this is a deliberate call to generate a fault, and it fails because there is no property named test on the model. The response comes back as expected, like so: <code>{"error":{"code":"","message":"The query specified in the URI is not valid. Could not find a property named 'test' on type 'DimDateAvailable'...</code> followed by a stack trace.</p>
  
  <p>This response is fine when <code>env.IsDevelopment()</code> is <code>true</code> but I don't want to expose the stack trace when not in development.</p>
  
  <p>I've looked at wrapping the code in the controllers' <code>get</code> method in a try-catch, but I think there's an action filter running over the results so it never gets called. On the other hand, I can't see where to inject any middleware and/or add any filters to catch errors. I suspect there might be a way to override an output formatter to achieve what I want but I can't see how.</p>
  
  <p>Here's what I have at the moment:</p>
  
  <p>In Startup.cs:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
    services.AddScoped&lt;TelemetryDbContext&gt;();
    services.AddOData();
    services.AddMvc();
  }
  
  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  {
    if (env.IsDevelopment())
    {
      app.UseDeveloperExceptionPage();
    }
  
    app.UseMvc(routeBuilder =&gt;
    {
      routeBuilder.MapODataServiceRoute("odata", "data", GetEdmModel());
      routeBuilder.Select().Expand().Filter().OrderBy().MaxTop(null).Count();
  
      // insert special bits for e.g. custom MLE here
      routeBuilder.EnableDependencyInjection();
    });
  }
  
  private static IEdmModel GetEdmModel()
  {
    var builder = new ODataConventionModelBuilder();
    builder.EntitySet&lt;DimDateAvailable&gt;("DimDateAvailable");
    return builder.GetEdmModel();
  }
  </code></pre>
  
  <p>In TelemetryDbContext.cs:</p>
  
  <pre><code>public virtual DbSet&lt;DimDateAvailable&gt; DimDateAvailable { get; set; }
  </code></pre>
  
  <p>In DimDateAvailable.cs</p>
  
  <pre><code>public class DimDateAvailable
  {
    [Key]
    public int DateId { get; set; }
  }
  </code></pre>
  
  <p>My controller:</p>
  
  <pre><code>public class DimDateAvailableController : ODataController
  {
    private readonly TelemetryDbContext data;
  
    public DimDateAvailableController(TelemetryDbContext data)
    {
      this.data = data;
    }
  
    [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.Supported, PageSize = 2000)]
    public IActionResult Get()
    {
      return Ok(this.data.DimDateAvailable.AsQueryable());
    }
  }
  </code></pre>
  
  <p>This is in an asp.net core 2 web app with the Microsoft.AspNetCoreOData v7.0.1 and EntityFramework 6.2.0 packages.</p>