Click or drag to resize
HandleExceptionsAttribute Class
Handles exceptions thrown when processing controller's actions, ensures that internal exceptions are properly logged to the Event log.
Inheritance Hierarchy
SystemObject
  SystemAttribute
    FilterAttribute
      ExceptionFilterAttribute
        CMS.WebApiHandleExceptionsAttribute

Namespace: CMS.WebApi
Assembly: CMS.WebApi (in CMS.WebApi.dll) Version: 11.0.0
Syntax
C#
public class HandleExceptionsAttribute : ExceptionFilterAttribute

The HandleExceptionsAttribute type exposes the following members.

Constructors
  NameDescription
Public methodHandleExceptionsAttribute
Creates new instance of HandleExceptionsAttribute.
Public methodHandleExceptionsAttribute(IEventLogService)
Creates new instance of HandleExceptionsAttribute
Top
Methods
  NameDescription
Public methodOnException
Raises the exception event.
(Overrides ExceptionFilterAttribute.OnException(HttpActionExecutedContext).)
Top
Remarks

This API supports the framework infrastructure and is not intended to be used directly from your code.

If exception is of type UnauthorizedAccessException, response is returned with the status code Unauthorized, so the browser can invoke login dialog. All other exceptions are logged to the Event log and empty response with status code InternalServerError is returned, except for the HttpResponseException. This exception is considered as valid result and therefore is returned to the caller, including the error message.

Examples
This example shows how to handle unauthorized request.
[HandleExceptions]
public class MyController : ApiController
{
    public HttpResponseMessage GetValue()
    {
        ...
        if(!IsAuthorized(MembershipContext.AuthenticatedUser))
        {
            // 401 status code Unauthorized is returned to the caller, so browser can handle the response properly
            throw new UnauthorizedAccessException();
        }
        ...
    }
}
This example shows how HttpResponseException behaves.
[HandleExceptions]
public class MyController : ApiController
{
    public HttpResponseMessage GetValue()
    {
        ...
        // 400 status code Bad request is returned to the caller together with the error message
        throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Given property is in invalid format"))
    }
}
This example shows how other types of exceptions behave.
[HandleExceptions]
public class MyController : ApiController
{
    public HttpResponseMessage GetValue()
    {
        ...
        // 500 status code Internal server error is returned to the caller. All sensitive data like error message or stack trace are omitted
        // Exception is logged to the Event log
        throw new Exception()
    }
}
See Also