<<ABP Documentation >> Exception handling

Source: Internet
Author: User

Document Directory

The content of this section:

    • Brief introduction
    • Enable error handling
    • Non-AJAX requests
      • Show exceptions
      • Userfriendlyexception
      • Error model
    • AJAX Requests
    • Exception events

Brief introduction

This approach is for ASP. NET MVC and Web APIs, if you are interested in ASP. ASP. NET core documentation.

In a Web application, exceptions are usually handled in the action of the MVC controller or the Web API controller, and when an outlier occurs, the user of the app receives an error message and the possible cause of the error in some way.

If an error occurs in a normal HTTP request, a page error is displayed, and if an error occurs in an AJAX request, the server sends the error to the client and the client is responsible for processing and displaying it to the user.

In all Web requests, handling exceptions is a tedious and repetitive matter, and the ABP automates the process, and you hardly have to explicitly handle any exceptions, the ABP handles all exceptions, logs them and returns the corresponding formatted response to the client, processing the response on the client and displaying the error message to the user.

Enable error handling

To enable error handling for the ABP controller, the customerrors mode must be enabled:

<customerrors mode="on" />

If you do not want to handle errors on local computations, you can set it to "RemoteOnly".

Note: Only set this for the ASP. NET MVC controller, the Web API and the Controller for ASP. NET Core do not need to be set.

Non-AJAX requests

If it is not an AJAX request, an error page is displayed.

Show exceptions

Here, an action of an MVC controller throws an arbitrary exception:

 Public ActionResult Index () {    thrownew Exception ("A sample Exception message ... ");}

Of course, this exception will also be thrown by another method calling this action, the ABP handles the exception, logs it and displays the "error.cshtml" view, you can customize this view to display errors, an example of an error view (the default error view in the ABP template):

The ABP hides the exception's details from the user and displays a standard (and localized) error message. Unless you explicitly throw a userfriendlyexception.

Userfriendlyexception

Userfriendlyexception is a special type of exception that is displayed directly to the user, as shown in the following example:

 Public ActionResult Index () {    thrownew userfriendlyexception ("ooppps! There is a problem! " " You were trying to see a product that's deleted ... " );}

ABP Logging It but this time does not hide the exception:

So, if you want to display a special error message to the user, just throw a userfriendlyexception (or inherit an exception from it).

Error Model

The ABP passes a Errorviewmodel object as a model to the error view:

 Public class errorviewmodel{    publicgetset;}      Public Get Set ; }}

ErrorInfo contains error details that can be displayed to the user, exception object is thrown exception, you can verify it and display additional information, for example, if it is a abpvalidationexception, you can display the validation error message:

AJAX Request

If the action return type of MVC is jsonresult (or task<jsonresult> of an asynchronous action), the ABP returns a JSON object to the client when an exception is encountered, as an example of an incorrect return object:

{  "TargetUrl":NULL,  "result":NULL,  "Success":false,  "Error": {    "message":"An internal error occured during your request!",    "Details":"..."  },  "unauthorizedrequest":false}

Success:false indicates that it is an error, and the Error object provides the wrong message (information) and details (details).

When you use the ABP infrastructure to send AJAX requests on the client, it automatically processes the JSON object and uses the message API to display the error message to the user, and more information to view the Ajax Api.

Exception events

When the ABP handles any exception, it triggers a abphandledexceptiondata event that can be registered to notify and notify (see the event Bus document for more information), for example:

 Public class Myexceptionhandler:ieventhandler<abphandledexceptiondata>, itransientdependency{    public  void  handleevent (abphandledexceptiondata eventData)    {        //Todo:check eventdata.exception!     }}

If you add this example class to your app (usually a web app), the Handleevent method will be called when the ABP handles the exception, so you can drill down into this exception handling.

kid1412 attached: English Original: Http://www.aspnetboilerplate.com/Pages/Documents/Handling-Exceptions

<<ABP Documentation >> Exception handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.