Almost all exceptions are getting caught except the exception from EF core . The exception filter is catching any error . but not errors from entity framework. If you need any other info like efcore model etc i can provide it , but it will become a long one .
This is my Exception filter
public class CustomExceptionFilter : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
Exception ex = context.Exception;
if (context.Exception is IndexOutOfRangeException)
context.Result = new OkObjectResult(new { error = true, errorMessage = ex.Message });
else if (context.Exception is ArgumentOutOfRangeException)
context.Result = new OkObjectResult(new { error = true, errorMessage = ex.Message });
else if(context.Exception is AggregateException)
context.Result = new OkObjectResult(new { error = true, errorMessage = ex.Message });
}
}
This the configure Services method
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers(options=> {
options.Filters.Add<ProductTypeSeedFilter>();
options.Filters.Add<CustomExceptionFilter>();
options.Filters.Add<VehicleSeedFilter>();
});
services.AddDbContext<InventoryContext>(options =>
options.UseSqlServer (Configuration.GetConnectionString( "DefaultConnection")),ServiceLifetime.Transient
);
services.AddSingleton<IProductService, ProductService>();
services.AddSingleton<IVehicleService, VehicleService>();
services.AddTransient<IVehicleRepository, VehicleRepository>();
services.AddTransient<IProductRepository, ProductRepository>();
services.AddMvc(o => { o.Filters.Add<CustomExceptionFilter>(); });
services.AddControllers().AddJsonOptions(options =>
options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.Preserve
);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "InventoryAPI2", Version = "v1" });
});
}
this is the repository method.I am trying to generate an error by inserting existing EAN for Product ean .
public async Task<int> InsertProduct(ProductDTO productDTO)
{
Product p = new Product()
{
EAN = productDTO.EAN,
Description = productDTO.Description,
MinimumRetail = productDTO.MinimumRetail,
MultipackUnits = productDTO.MultipackUnits,
Packsize = productDTO.PackSize,
QtyOnHand = productDTO.QtyOnHand,
RetailPrice = productDTO.RetailPrice,
CreatedDate = DateTime.Now,
UpdateDate = DateTime.Now,
HubNo = productDTO.Store,
tableKey = productDTO.datakey,
Store = _dbcontext.Stores.Where(x => x.HubNo == productDTO.Store).FirstOrDefault()
};
_dbcontext.Products.Add(p);
//if (_dbcontext.Products.Where(x => x.EAN == p.EAN).Count() < 1)
// _dbcontext.Products.Add(p);
await _dbcontext.SaveChangesAsync();
return _dbcontext.Products.Count();
}
this is the controller Action Method .
[HttpPost]
[CustomExceptionFilter]
public IActionResult Post([FromBody] ProductDTO productDTO)
{
return Ok( new { cnt =_productRepository.InsertProduct(productDTO) });
}
Can any one help me here ?
EANis your primary key forProduct?CustomExceptionFilterdo you have built-in logging in ASP.NET Core to Log the exception to get more information about what kind of exception is being thrown. Another importnant point is that, ensure that the order of execution of your exception filter is correct. Filters are executed in the order they are added to the filter pipeline. Make sure that your CustomExceptionFilter is registered early in the pipeline.