Handle and log an unhandled action exception¶
Log an unhandled action exception with full diagnostic context and redirect non-AJAX requests. Trigger: Any unhandled exception in an action on a controller the filter is applied to.
| Field | Value |
|---|---|
| Operation ID | OP-08 |
| Trigger | Any unhandled exception in an action on a controller the filter is applied to |
| Inputs | ExceptionContext (request, controller, exception) |
| Outputs | Log entry via ILogService; redirect for non-AJAX requests |
| Primary components | HandleExceptionAttribute.OnException(ExceptionContext) |
| Entry point | src/Web.Mvc/ActionFilters/HandleExceptionAttribute.cs |
Steps¶
- Proceed only when both
LogServiceandfilterContextare non-null; otherwise do nothing at all — no logging and no redirect (BR-11). - Build state information (
BuildStateInformation): when the controller is aWebContextControllerBase, appendWebContextAsString(); then append every entry ofHttpContextExtensions.GetValuesAsDictionary(request)askey : valuelines — HttpMethod, IsAjaxRequest, RawUrl, Form, QueryString, ContentType, AnonymousID, browser version/type/gateway version, UserAgent, UserAddress, UserName, and UrlReferrer when present (BR-12). - Call
LogService.LogException(applicationName, controllerAndActionName, state, exception), the first two values deriving fromRequest.Url.ApplicationName()andRequest.Url.ControllerAndActionName(). - Swallow any exception raised while logging (
catch (System.Exception) { }) so error handling never throws (BR-13). - In the
finallyblock, when the request is not an AJAX request,Response.Redirectto{Constants.ErrorUrl}?MSG={UrlEncode(exception.Message)}. AJAX requests are logged but receive no redirect (BR-14).
Process flow¶
The following diagram visualizes this operation's control flow.
flowchart TD
Start([Unhandled exception])
Chk{LogService and filterContext present?}
Skip(["No logging, no redirect"])
Build[Build state information]
Log[LogService.LogException]
Catch[Swallow logging failures]
Ajax{Is AJAX request?}
NoRedir(["Logged, no redirect"])
Redir([Redirect to error page])
Start --> Chk
Chk -->|No| Skip
Chk -->|Yes| Build
Build --> Log
Log --> Catch
Catch --> Ajax
Ajax -->|Yes| NoRedir
Ajax -->|No| Redir
Failure and edge-case behavior¶
filterContext.ExceptionHandledis never set, so MVC's default error handling still applies afterwards.*
Related documentation¶
Note: Statements marked with
*were inferred from code structure, naming conventions, or contextual evidence where direct confirmation was not available in source artifacts. Statements marked with**could not be confirmed; treat as unknown until verified.
Sources¶
src/Web.Mvc/ActionFilters/HandleExceptionAttribute.cs