Reputation: 3032
I have a use case: There will be an error on getting property in the model:
public class Student
{
public Guid Id { get; set; }
public string Name { get; set; }
public int Score { get; set; }
public Student ErrorProperty { get { throw new Exception(); } set { } }
}
I am trying to expand ErrorProperty:https://localhost:44383/odata/Student?$expand=ErrorProperty In controller looks like everything is ok, it is not throwing any exception:
[HttpGet]
[EnableQuery()]
public IActionResult Get(ODataQueryOptions<TEntity> queryOptions, CancellationToken cancellationToken)
{
var list = new List<Student>
{
CreateNewStudent("Cody Allen", 130),
CreateNewStudent("Todd Ostermeier", 160),
CreateNewStudent("Viral Pandya", 140)
};
return Ok(list);
}
After executing this method ('Get' method), the model throws an exception. In Postman I am getting Could not get response
error:
Also, I created the error handler, but it is not catching this kind of exceptions:
public class ODataExceptionHandler
{
public static RequestDelegate HandleException()
{
return async context =>
{
await Handle(context);
};
}
public static Task Handle(HttpContext context)
{
return Task.Run(new Action(() =>
{
context.Response.ContentType = "application/problem+json";
var exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
var content = JsonConvert.SerializeObject(exceptionHandlerPathFeature);
byte[] byteArray = Encoding.ASCII.GetBytes(content);
context.Response.Body.Write(byteArray);
}));
}
}
Configuration:
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(ODataExceptionHandler.HandleException());
});
How can I catch this kind of errors?
Upvotes: 1
Views: 197
Reputation: 4022
public class Patient
{
public ulong PatientId { get; set; }
public string Name { get; set; }
public string City { get; set; }
public DateTime PurchaseDateTime { get; set; }
public ICollection<PatientForms> PatientForms { get; set; }
public Patient ErrorProperty { get { throw new Exception(); } set { } }
}
UPDATE 10/8/2020
Adding ExceptionHandler
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//if (env.IsDevelopment())
//{
// app.UseDeveloperExceptionPage();
//}
//else
//{
//app.UseExceptionHandler("/Home/Error");
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(ODataExceptionHandler.HandleException());
});
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
//}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "odata/{controller=Home}/{action=Index}/{id?}");
endpoints.EnableDependencyInjection();
endpoints.Select().Expand().Filter().OrderBy().Count().MaxTop(10);
});
}
Screenshot of test
Steps of using OData
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews().AddNewtonsoftJson();
services.AddOData();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "odata/{controller=Home}/{action=Index}/{id?}");
endpoints.EnableDependencyInjection();
endpoints.Select().Expand().Filter().OrderBy().Count().MaxTop(10);
});
}
Controller.cs
[ODataRoutePrefix("Student")]
public class StudentsController : ODataController
{
...
[ODataRoute]
[EnableQuery]
public IActionResult Get()
{
var list = new List<Student>
{
CreateNewStudent("Cody Allen", 130),
CreateNewStudent("Todd Ostermeier", 160),
CreateNewStudent("Viral Pandya", 140)
};
return Ok(list);
}
}
Upvotes: 1