& Lt; ABP document & gt; Exception Handling, and abp Document Exception Handling

Source: Internet
Author: User

<ABP document> Exception Handling: An error occurred in the relevant document.


Document directory


Content of this section:

  • Introduction
  • Enable error handling
  • Non-AJAX requests
    • Display exception
    • UserFriendlyException
    • Error Model
  • AJAX request
  • Exception event



This document is intended for Asp.net Mvc and Web APIs. If you are interested in Asp.net Core, see the Asp.net Core document.

In a Web application, exceptions are usually handled in the Action of the Mvc controller or the Action of the Web Api controller. When an exception is surnamed, application Users may receive error messages and possible causes in some way.

If an error occurs in a common HTTP request, a page error is displayed. If an error occurs in an AJAX request, the server sends the error to the client, the client is responsible for processing and displaying data to users.

In all Web requests, exception handling is a tedious and repetitive task, and you do not need to explicitly handle any exceptions because the ABP automates this process, if you handle all exceptions, log them, and return the corresponding formatted response to the client, the client also processes the response and displays the error message to the user.


Enable error handling

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

<customErrors mode="On" />

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

Note: you only need to set this for the Asp.net Mvc controller. The Controller for Web Api and Asp.net Core does not need to be set.


Non-Ajax requests

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


Display exception

Here, the Action of an Mvc controller throws an arbitrary exception:

public ActionResult Index(){    throw new Exception("A sample exception message...");}

Of course, this exception will also be thrown by another method that calls this Action, and the ABP processes this exception, logs it, and displays "Error. cshtml "view. You can customize this view to display errors. An example of an Error view (default Error view in the ABP template ):

ABPS hides the exception details and displays standard (localized) error information. Unless you explicitly throw a UserFriendlyException.



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

public ActionResult Index(){    throw new UserFriendlyException("Ooppps! There is a problem!", "You are trying to see a product that is deleted...");}

You can record it in the ABP log, but do not hide the exception this time:

Therefore, if you want to display a special error message to the user, you only need to throw a UserFriendlyException (or an exception inherited from it ).



An ErrorViewModel object is passed as a model to the Error view through the "ABP" command:

public class ErrorViewModel{    public AbpErrorInfo ErrorInfo { get; set; }    public Exception Exception { get; set; }}

ErrorInfo contains the error details that can be displayed to the user. The Exception object is an Exception thrown. You can check it and display additional information. For example, if it is an AbpValidationException, you can display the verification error message:


AJAX Request

If the return type of the Action in Mvc is JsonResult (or asynchronous Action Task <JsonResult>), when an exception occurs, the following is returned to the client:

{  "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. The error object provides the error message and details ).

When you send an AJAX request on the client using the ABC infrastructure, it automatically processes the JSON object and displays the error message to the user using the message API. For more information, see the AJAX Api.


Exception event

When an ABC handles any exception, it triggers an AbpHandledExceptionData event. It can be registered and notified (view the event bus documentation 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 application (usually a Web application), The HandleEvent method will be called when the ABP handles exceptions, you can thoroughly check and handle this exception.


Kid1412 Appendix: http://www.aspnetboilerplate.com/Pages/Documents/Handling-Exceptions

Related Article

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.