gog
gog

Reputation: 12968

Exception and log handlers in WebApi

Im developing a ASP.NET WebApi using a specialized implementation of the ExceptionLogger and ExceptionHandler but i have some doubts about those two classes. By using the ExceptionHandler class im returning some custom response message to the client, so in every exception that might occours on the controller or service layer it is handled in this class. My first question is, can it replace all the try/catch blocks on the controller? The documentation says that the ExceptionHandler is a global filter, and it always be fired.

My other doubt is if its possible to catch the 404(not found) errors within this class. I tried but i couldnt catch it.

Thanks.

Upvotes: 0

Views: 216

Answers (3)

Big Daddy
Big Daddy

Reputation: 5224

I don't put try/catch blocks in controllers. You don't show any code, but as an alternative you may want to try this. Use a custom exception attribute that inherits from ExceptionFilterAttribute. Basically, it handles the exception and returns a HttpResponseException to the client.

Here's the attribute:

public class HandleExceptionAttribute : ExceptionFilterAttribute
    {
        private HttpStatusCode _status;
        private Type _type;

        public Type Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public HttpStatusCode Status
        {
            get { return _status; }
            set { _status = value; }
        }

        public override void OnException(HttpActionExecutedContext context)
        {
            var exception = context.Exception;

            if (exception == null || exception.GetType() != Type) return;

            var response = context.Request.CreateErrorResponse(Status, exception.Message, exception);

            throw new HttpResponseException(response);
        }
    }

Here's the usage:

[HandleException(Type = typeof (ArgumentNullException), Status = ttpStatusCode.InternalServerError)]
public class MyController : ApiController
{
   //...
}

Upvotes: 0

Kien Chu
Kien Chu

Reputation: 4895

can it replace all the try/catch blocks on the controller

Yes, you can. But I prefer to add more custom information to the exception and rethrow it so that it has more useful information.

404 not found

For global unhandled exception, I'm using ELMAH to do the logging. It also can catch 404 not found and log it.

It's easy to config and easy to use

Link: https://code.google.com/p/elmah/

If you use IIS to host, you can also customize 404 error page there.

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

I suggest you to use Global.asaxand Application_Error in order to catch all errors. You can't use handler class on this topic, because handler occurs always before enter in your application, you can use handler for formatting output.

link : https://msdn.microsoft.com/en-us/library/ms227675(v=vs.100).aspx

But it's recommended to delete your try catch in others classes, except specific cases. (it's proper)

For 404 i suggest you to use web.config and customerror node in configuration , or use filter exception in your c#.

Upvotes: 1

Related Questions