'Best practices regarding passing CRUD operation results to the controller
I am trying to write an application that performs CRUD operations using Web API. It is using ADO.Net as SQL repository to do all the CRUD operations. While performing the CRUDs, any exceptions can occur, and I want to pass these exceptions back to the Web API controller.
I am thinking of creating a class called OperationResult which will have boolean field for success or failure and a message field for holding exception error message.
I am just wondering if there is already some best practices for passing success/failure CRUD operations and error messages to the controller. If it's success I pass Ok(), if data not found I pass Notfound(), etc.
Thanks
Solution 1:[1]
You can create a customer Middleware to capture the error logs.
And if you want your error to be more specific, all you have to do is to add another specific catch block (FileNotFoundException or SqlException…etc) before the global catch block.
So in your case, I think we can judge the error number of SqlException and return the error message.
Refer Blog: Global Error Handling in ASP.NET Core Web API
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
