Back to Catalog
For the business layer of the program's fatal error, we have been the practice is to directly throw the specified exception, let the program go to the end, this is true, because if a business has a fatal blocking problem, there is no need to go up a layer of return, but then there is a problem, the direct throw exception, means that the server directly 500, the front-end how to display, or if you are the API service, if the front-end return, if it is 500, then directly hung up, haha!
The following is an optimized global exception capture code (non-API) in an MVC environment
/// <summary> ///Global Exception Capture/// </summary> Public classGlobalexceptionfilterattribute:handleerrorattribute { Public Override voidonexception (Exceptioncontext context) {Jsonresult response=NewJsonresult () {Jsonrequestbehavior=Jsonrequestbehavior.allowget}; if(Context. Exception isArgumentException) { varException =(ArgumentException) context. Exception; Response. Data=New{StatusCode=System.Net.HttpStatusCode.InternalServerError, ErrorCode=" Geneva", Message=exception. Message,}; } Else if(Context. Exception isException) { varException =(Exception) context. Exception; Response. Data=New{StatusCode=System.Net.HttpStatusCode.InternalServerError, ErrorCode=" on", Message=exception. Message,}; } Else{Response. Data=New{StatusCode=System.Net.HttpStatusCode.InternalServerError, ErrorCode="Geneva", Message="Other Exceptions", }; } context. Result=response; Context. Exceptionhandled=true; Context. HttpContext.Response.Clear (); Context. HttpContext.Response.TrySkipIisCustomErrors=true; } }
If there is a problem with the business layer, throw it directly.
if 0 ) thrownew ArgumentException ("ID cannot be 0"); if 0 ) thrownew ArgumentException ("ID cannot be negative ");
Then after the page, deliberately let it throw an exception, we look at the results of the page response
In fact, for the server, it is 200, normal return, and for the non-business module, its state is a 500, hehe, this should be clear.
Thank you for reading!
Back to Catalog
Falls in love with the mvc~ business layer deliberately throws an exception, and the global exception captures it and returns it as a format