HandleExceptionsAttribute Class |
Namespace: CMS.WebApi
public class HandleExceptionsAttribute : ExceptionFilterAttribute
The HandleExceptionsAttribute type exposes the following members.
Name | Description | |
---|---|---|
HandleExceptionsAttribute |
Creates new instance of HandleExceptionsAttribute.
| |
HandleExceptionsAttribute(IEventLogService) |
Creates new instance of HandleExceptionsAttribute |
Name | Description | |
---|---|---|
OnException |
Raises the exception event.
(Overrides ExceptionFilterAttribute.OnException(HttpActionExecutedContext).) |
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.
[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(); } ... } }
[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")) } }
[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() } }