Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throw or not throw the exception from the methods consumed by the ASP.NET Web API layer

Assume that I am building an ASP.NET Web API application and it has the following structure:

enter image description here

As you can see from the diagram, the ASP.NET Web API core will talk to domain service layer (e.g. MembershipService class which has methods such as GetUsers, CreateUser, etc.) and my service classes will talk to one or multiple repositories to handle the operations.

It's very obvious that a service operation (such as MembershipService.CreateUser method) would fail for several reasons (unmet conditions, an exception thrown by the repository, etc.) and this is the place where I have the doubts.

Do you think that service classes should handle exceptions and return some sort of result object such as the below one:

public class OperationResult {

    public OperationResult(bool isSuccess) : this(isSuccess) {
        IsSuccess = isSuccess;
    }

    public OperationResult(bool isSuccess, Exception exception) : this(isSuccess) {
        Exception = exception;
    }

    public bool IsSuccess { get; private set; }
    public Exception IsSuccess { get; private set; }
}

public class OperationResult<TEntity> : OperationResult {

    public OperationResult(bool isSuccess) 
        : base(isSuccess) { }

    public OperationResult(bool isSuccess, Exception exception) 
        : base(isSuccess, exception) { }

    public TEntity Entity { get; set; }
}

Or do you think that the service methods shouldn't abstract the exception like that and should throw the exception directly or indirectly (creating a new meaningful exception type specific to operation and put the thrown exception as its inner exception)?

like image 297
tugberk Avatar asked Jan 07 '13 16:01

tugberk


People also ask

How do you handle exception in Web API?

You can customize how Web API handles exceptions by writing an exception filter. An exception filter is executed when a controller method throws any unhandled exception that is not an HttpResponseException exception.

How do I handle exceptions in ASP.NET Core Web API?

To handle exceptions we can use the try-catch block in our code as well as finally keyword to clean up resources afterward. Even though there is nothing wrong with the try-catch blocks in our Actions in Web API project, we can extract all the exception handling logic into a single centralized place.

How many ways can you handle exceptions in Web API?

In ASP.NET MVC Web API, we usually have three ways to handle exceptions, HttpResponseException, associated with HttpStatusCode, --- Locally in Try-catch-final. Exception filter, --- in the level of Action, Controller, and Globally.

What is an exception how exceptions are handled in ASP.NET application?

Exception class. An exception is thrown from an area of code where a problem has occurred. The exception is passed up the call stack to a place where the application provides code to handle the exception. If the application does not handle the exception, the browser is forced to display the error details.


2 Answers

When you are in-process, use exceptions.

like image 191
Darrel Miller Avatar answered Oct 26 '22 21:10

Darrel Miller


I don't see ANY point in avoiding exceptions. Exceptions are there for good reasons, mainly to be used! Just try to look at the big picture: you are trying to change Exception mechanism with the old fashion way of error checking. This way you'll lose all the merits of Exceptions (like separation of the error-handling and regular code, CallStack, ...) and gain nothing in return.

What I usually do in this situation is to catch the exception in service layer and rewrap it into a custom exception (with the reference to the original exception in the InnerException field).

like image 20
Mahdi Avatar answered Oct 26 '22 19:10

Mahdi