A.R
A.R

Reputation: 407

How to handle business logic errors and application exceptions from business logic layer in MVC ASP.NET

I'm working on an ASP.NET MVC application in which I need to trap errors in business logic classes and redirect to error controller. I need to trap both errors, ones which are related to my business logic and others which are application exceptions. If I throw an exception from my business logic, how do I catch that in the current controller and how do I redirect to ErrorController?

Upvotes: 2

Views: 3644

Answers (2)

haim770
haim770

Reputation: 49095

You can decorate your Controller/Action with the [HandleErrorAttribute] to do just that.

For example:

    [HandleError]
    public ActionResult PlaceOrder(OrderDetails orderDetails)
    {
        orderService.PlaceOrder(orderDetails);
        return View("Success");
    }

You can set the appropriate View to load to depend on the Exception Type:

    [HandleError(ExceptionType=typeof(PlaceOrderException),View="OrdersError"]
    [HandleError(ExceptionType=typeof(Exception),View="GeneralError"]
    public ActionResult PlaceOrder(OrderDetails orderDetails)
    {
        orderService.PlaceOrder(orderDetails);
        return View("Success");
    }

Alternatively, you can register it globally on your global.asax:

    GlobalFilters.Filters.Add(new HandleErrorAttribute
    {
        View = "Error"
    }); 

P.S: The above example assumes your 'Error/GeneralError/OrdersError' Views are in the Shared folder. if they're not, you're gonna need to specify the full path.

Edit (as per your comment):

If you want to return Json instead of View, create the following ActionFilter:

public class HandleErrorJsonAttribute : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.StatusCode = 500;
        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;

        filterContext.Result = new JsonResult
        {
            JsonRequestBehavior = JsonRequestBehavior.AllowGet,
            ContentType = "application/json",
            Data = new 
            {
                Msg = "An Error Occured",
                ExceptionMsg = filterContext.Exception.ToString()
            }
        };
    }
}

Then use the new [HandleErrorJson] attribute (as outlined above), or register it as a Global Filter in your global.asax.

Upvotes: 2

Babak Fakhriloo
Babak Fakhriloo

Reputation: 2126

I think it is a good idea to look at this sample. When ever an error occurs in Business logic layer you can return false or say -1 for a method of Business class, then show proper error message to user, and in catch part of the business method you can use one of popular error logging libs like Log4NET or elmah.

Edited: To redirect user when error occurs you can specify the error controller in custom error section of web config

Upvotes: -1

Related Questions