ASP.NET MVC application Controller methods are different:
- Return a JSON response, for example {status: 0 , message: "Data saved successfully"}
- Return JSON in javascript datatables.net library format
There can be two types of errors:
- Error in business logic: incorrect data came from the user and he needs to return the following JSON: {status: 1 , message: "Date not specified ..."}
- System error (incorrect SQL query, loss of communication with the database, other runtime error) - in this case, the user needs to return JSON {status: 1 , message: "Something went wrong, try later"}, and save details of the exception to log file (possibly with sending a message to the administrator)
It would not be desirable in the code of controllers to scatter try-catch, there is the idea of data validation errors throwing exceptions, catching them and system errors in one place.
Is it possible to implement this with filters or something else?