Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an appropriate way to handle or throw exception from service layer of n-tier ASP.Net MVC application?

I have a web application with three layers: Web > Services > Core. Services has a bunch of business logic that helps Web construct and interpret viewModels. Sometimes there might be a problem in the Services layer though, and the user should be pushed to an error page.

How should error handling be implemented in the service layer of an MVC application? For example:

public void DeleteOrder(int orderId)
{
    var order = _db.Order.FirstOrDefault(c => c.OrderId == orderId);
    if (order == null)
    {
        // error handling
    }

    _db.Orders.Remove(order);
    _db.SaveChanges();
}

What would go in that isNull block?

like image 218
RobVious Avatar asked Feb 20 '13 02:02

RobVious


People also ask

What is the best way to handle exceptions globally in asp net core?

The middleware UseExceptionHandler can be used to handle exceptions globally. You can get all the details of the exception object (Stack Trace, Inner exception, message etc..) and display them on-screen. You can implement like this.

Where do you handle exceptions controller or service?

You should make your services transactional and handle the exceptions in the Controller layer: you may choose Controller Based Exception Handling (Using @ExceptionHandler ) or Global Exception Handling (Using @ControllerAdvice Classes).

What is an exception in net How An exception is handled explain with an example?

In . NET, an exception is an object that inherits from the System. Exception class. An exception is thrown from an area of code where a problem has occurred. The exception is passed up the stack until the application handles it or the program terminates.


2 Answers

Generally you would put your exception handling code in your controllers. In your terminology, I am assuming the the MVC controllers live in your "Web" layer, and that these controllers call methods in your "service" layer, such as the "DeleteOrder" method you've shown. If this is the case, in your error handling code in DeleteOrder, you should simply throw an exception:

if (order == null)
{
    throw new InvalidOperationException("Specified OrderId does not exist");
}

This way the unhandled exception will be passed to your controller, where your exception handling code lives, and there you can log the exception and redirect the user to the appropriate error page.

As far as how to handle the exception in your controller, you have a number of options:

  1. Use a try-catch block for each action method
  2. Implement the IExceptionFilter interface on your controller class by implementing the OnException method
  3. Use the built-in HandleErrorAttribute exception filter
  4. Create your own custom exception handling filter

The fourth method (create your own exception filter) is probably the most robust way to go. In here, you can add exception logging, as well as code to redirect the user to an appropriate error page based on the type of exception that is thrown.

You can find a good overview of MVC controller exception handling here.

like image 63
Joe Alfano Avatar answered Sep 20 '22 06:09

Joe Alfano


Throwing a user-friendly Exception is the way to go. Your controller should try/catch the code that calls your service layer and act accordingly by redirecting the user to an appropriate error page.

In the specific example you show, an OrderNotFoundException would be fine.

like image 45
Icarus Avatar answered Sep 21 '22 06:09

Icarus