Click here to go to the ABP series articles General Catalogue
DDD-based Modern ASP.--ABP Series 23, ABP presentation Layer-Exception handling
The ABP is "ASP. Boilerplate Project (ASP. NET Template project) "for short.
ABP's official website :http://www.aspnetboilerplate.com
ABP's Open source project on GitHub : https://github.com/aspnetboilerplate
In a Web application, exceptions are usually handled in the MVC controller actions and the Web API controller actions. When an exception occurs, the application user is notified in some way of the error and the optional reason for the error (that is, enumerating the various causes of the exception, the cause of the error may be one or more of the enumeration.) )
If a regular HTTP request generates an error, an error page is displayed. If an AJAX request generates an error, the server sends an error message to the client and then the client processes and displays the error to the user.
Handling Exceptions in all Web Requests is a tedious and repetitive task. In the ABP, however, you rarely need to specify explicit exception handling for any exceptions, and the ABP automatically logs these exceptions and responds in the appropriate format to the client. That is, the response is processed on the client and the error is displayed in detail to the customer.
Turn On error handling
In order to turn on error handling, the customErrors must be set as follows:
<customerrors mode="on" />
You can also set ' RemoteOnly ' If you do not want to handle these errors locally.
Non-AJAX requests
If it is not an AJAX request, an error page will be displayed.
Show exception (Showing exceptions)
The MVC Controller action throws an exception, as follows:
Public ActionResult Index () { thrownew Exception ("A sample Exception message ... ");}
Of course, this exception can be thrown by another method in the specified action of the call. The ABP handles this exception, logs the exception information, and displays the ' error.cshtml ' view. You can customize this view to display the error. Example Error view (this view is the default error view template in the ABP).
The ABP hides the details of the exception, but instead presents a standard (localized, friendly) error message to the user. Unless you explicitly specify to throw a Userfriendlyexception exception.
User exception Friendly (userfriendlyexception)
Userfriendlyexception is a special type of exception that is used to display directly to the user. Take a look at the following example:
Public ActionResult Index () { thrownew userfriendlyexception ("ooppps! There is a problem! " " You were trying to see a product that's deleted ... " );}
The ABP logs this exception and does not hide this exception message:
So, if you want to display a special error message to the user, you only need to throw a userfriendlyexception (or a type derived from the exception class, that is, to inherit the subclass of the exception class).
Error model
The ABP passes a Errorviewmodel object to the error view:
Public class errorviewmodel{ publicgetset;} Public Get Set ; }}
The errorinfo contains detailed exception information that can be displayed to the customer. The Exception object is the exception that was thrown. You can check for exceptions and attach custom information to show if you want to do so. For example: We can verify whether the exception is a abpvalidationexception.
Ajax requests
If the request is an AJAX request, the ABP returns a JSON object to the client. The same is true of the ASP. Controllers and ASP. Net-Web API Controllers. To return an exception message in JSON, see the following example:
{ "TargetUrl":NULL, "result":NULL, "Success":false, "Error": { "message":"An internal error occured during your request!", "Details":"..." }, "unauthorizedrequest":false}
Success:false indicates that an error has occurred. The Error object provides a detailed description of the fault message and the error.
When you make an AJAX request at the client with an ABP infrastructure, it automatically processes the JSON object with the message API and displays the error message to the user. For more information, refer to the Ajax API and the Dynamic Web API layer.
Exception events
When the ABP handles any Web request exception, it triggers the Abphandledexceptiondata event, and of course you must register the event and write the appropriate processing code. Please refer to Eventbus documentation for more information.
I hope that more domestic architects will be able to focus on the ABP project, and perhaps it will help you, perhaps with your participation, this project can develop better.
Welcome to add ABP Architecture Design Exchange QQ Group: 134710707
Click here to go to the ABP series articles General Catalogue
ABP (modern ASP. NET template Development Framework) series 23, ABP presentation Layer-Exception handling