SAIF.NetCore.ErrorHandler architecture¶
SAIF.NetCore.ErrorHandler is an implementation-layer library that centralizes exception translation and formatting for two hosting models: ASP.NET Core HTTP pipelines, through ErrorHandler/IErrorHandler, and Windows Service hosts, through WindowsServiceErrorHandler/IWindowsServiceErrorHandler; it codes only against SAIF.NetCore.ServiceContracts interfaces and relies on the host application to supply the logging and environment implementations and to wire it into the pipeline.
Responsibilities¶
- Translate exceptions into HTTP responses: log the exception through
ISaifLogService, capture the returned log ID asErrorID, clear the response, addError-ID/Server-Nameheaders, set the status code and content type, and write a handled or unhandled body (ErrorHandler.LogAndReturnException). - Provide a legacy,
[Obsolete]-marked conventional middleware entry point (ErrorHandler.Invoke) that classifies exceptions asHttpStatusCodeException,SAIFException, or any otherException, in that precedence order. - Build and log a formatted, multi-line Windows Service error (
WindowsServiceErrorHandler.HandleError), raisingBeginErrorHandlingandErrorHandledevents so a host can veto logging, add context objects, or suppress special actions. - Execute pluggable, fire-conditional special error actions (
SpecialErrorAction/SpecialErrorAction<T>) that return aSpecialErrorActionResult; only the wrapped delegate call is contained in a try/catch — the fire-condition predicate, a non-conformingISpecialErrorActionoverride, and the Error-level failure-logging call can all still throw uncaught, stopping the remaining actions and the subsequentErrorHandledevent from running (ExceptionExtensions). - Flatten exception stack traces across the inner-exception chain and extract
SAIFException.LogDetailsas strings (ExceptionExtensions).
Component boundaries¶
ErrorHandler/IErrorHandler: ASP.NET Core-facing exception-to-response translator; implements theIErrorHandleablemarker from ServiceContracts.WindowsServiceErrorHandler/IWindowsServiceErrorHandler(ServiceContracts): Windows Service-facing exception handler; ownsBeginErrorHandling/ErrorHandledevents, theSpecialErrorActionslist, and theShutdownExemptExceptionTypeslist.SpecialErrorAction/SpecialErrorAction<T>: delegate wrappers with an optional fire-condition predicate, invoked byWindowsServiceErrorHandler.ContextContainer(IErrorContextContainer): string-keyed object bag passed to special actions during error handling.WindowsServiceError(IWindowsServiceError/IStandardErrorLogInfo): formats the identity, timestamp, exception, and log details into the logged error text.ExceptionExtensions: static helpers for stack-trace flattening andSAIFException.LogDetailsextraction.- This library has no DI registration extensions, no middleware
Use*extension, and no hosted service of its own; every wiring point lives inSAIF.NetCore.ServiceExtensionsand, for Windows Service hosts,SAIF.NetCore.WindowsService.
Interaction model¶
- For HTTP hosts,
SAIF.NetCore.ServiceExtensionsregistersIErrorHandleable/ErrorHandlerand wraps the pipeline with its ownDynamicErrorHandlermiddleware, which catchesHttpStatusCodeException,SAIFException, or any other exception, classifies status/content-type/handled accordingly, and callsIErrorHandler.LogAndReturnExceptionin this library to produce the response. - For Windows Service hosts,
SAIF.NetCore.ServiceExtensionsregistersWindowsServiceErrorHandleras a singletonIWindowsServiceErrorHandler, andSAIF.NetCore.WindowsService'sServiceWorker.HandleTaskErrorfirst computesIsExceptionExemptFromShutdownand builds the error message, then callsHandleError(message, ex), and only afterward conditionally stops the host using the previously computed exemption result. - Both handlers resolve their logging dependency (
ISaifLogService/ISaifWindowsServiceLogService) andIEnvironmentClientpurely as ServiceContracts interfaces supplied at construction time; outbound calls to the SAIF Logging API happen insideSAIF.NetCore.Logging's own implementation, not inside this library.
Architecture diagram¶
The following diagram shows the two hosting paths this library serves and the ServiceContracts interfaces each depends on; all concrete implementations are supplied by the consuming host.
flowchart TD
HTTP["ASP.NET Core host<br>(DynamicErrorHandler middleware,<br>SAIF.NetCore.ServiceExtensions)"] -->|IErrorHandler.LogAndReturnException| EH["ErrorHandler<br>(SAIF.NetCore.ErrorHandler)"]
WS["Windows Service host<br>(ServiceWorker, SAIF.NetCore.WindowsService)"] -->|HandleError| WSEH["WindowsServiceErrorHandler<br>(SAIF.NetCore.ErrorHandler)"]
EH -->|Error / Warning| LOG["ISaifLogService<br>(ServiceContracts contract)"]
WSEH -->|Error| WSLOG["ISaifWindowsServiceLogService<br>(ServiceContracts contract)"]
WSEH --> ACTIONS["SpecialErrorAction / SpecialErrorAction<T>"]
WSEH -->|ApplicationName / ApplicationHost / TrackName| ENV["IEnvironmentClient<br>(ServiceContracts contract)"]
Sequence diagram¶
The following diagram shows the Windows Service error-handling sequence, including the event hooks and special actions.
sequenceDiagram
participant ServiceWorker
participant WindowsServiceErrorHandler
participant ISaifWindowsServiceLogService
participant SpecialErrorAction
ServiceWorker->>ServiceWorker: IsExceptionExemptFromShutdown? (computed first)
ServiceWorker->>ServiceWorker: BuildErrorMessage(ex, exemptFromShutdown)
ServiceWorker->>WindowsServiceErrorHandler: HandleError(message, ex)
WindowsServiceErrorHandler->>WindowsServiceErrorHandler: Raise BeginErrorHandling
alt not Handled by subscriber
WindowsServiceErrorHandler->>ISaifWindowsServiceLogService: Error(ex, formattedText)
end
alt FireSpecialActions
WindowsServiceErrorHandler->>SpecialErrorAction: Invoke(ex, context)
SpecialErrorAction-->>WindowsServiceErrorHandler: SpecialErrorActionResult
end
WindowsServiceErrorHandler->>WindowsServiceErrorHandler: Raise ErrorHandled
WindowsServiceErrorHandler-->>ServiceWorker: return
ServiceWorker->>ServiceWorker: if not exemptFromShutdown, stop host (using value computed before HandleError)
Design constraints¶
- No project references between libraries in this repository; the only cross-library dependency is the
SAIF.NetCore.ServiceContractsNuGet package, consistent with the repository-wide layering inArchitecture.md. BuildHandledResponseMessagecallscontentType.ToLower()without a null check; anHttpStatusCodeExceptionwith anullContentTypewould throw after the response headers are already written, leaving the client with the status code and an empty body*.WindowsServiceErrorHandler.HandleErrorthrows before logging when aSAIFException'sLogDetailsisnull, becauseGetLogDetailsForSAIfExceptioncalls.Selectonnull; the exception then escapes unlogged intoServiceWorker.HandleTaskError*.- A local
IStandardErrorContentinterface duplicates the ServiceContractsIStandardErrorLogInfocontract minusID, with no in-repository consumer found*.
Related documentation¶
- Index
- Overview
- Dependencies
- Operations and processes
- Business rules
- API reference
- SAIF.NetCore.ServiceContracts overview
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/SAIF.NetCore.ErrorHandler/SAIF.NetCore.ErrorHandler/SAIF.NetCore.ErrorHandler/ErrorHandler.cssrc/SAIF.NetCore.ErrorHandler/SAIF.NetCore.ErrorHandler/SAIF.NetCore.ErrorHandler/ExceptionExtensions.cssrc/SAIF.NetCore.ErrorHandler/SAIF.NetCore.ErrorHandler/SAIF.NetCore.ErrorHandler/WindowsService/WindowsServiceErrorHandler.cssrc/SAIF.NetCore.ErrorHandler/SAIF.NetCore.ErrorHandler/SAIF.NetCore.ErrorHandler/WindowsService/SpecialErrorAction.cssrc/SAIF.NetCore.ErrorHandler/SAIF.NetCore.ErrorHandler/SAIF.NetCore.ErrorHandler/WindowsService/ContextContainer.cssrc/SAIF.NetCore.ErrorHandler/SAIF.NetCore.ErrorHandler/SAIF.NetCore.ErrorHandler/WindowsService/WindowsServiceError.cssrc/SAIF.NetCore.ServiceContracts/SAIF.NetCore.ServiceContracts/SAIF.NetCore.ServiceContracts/IErrorHandleable.cs,ISaifLogService.cs,ISaifWindowsServiceLogService.cs,IEnvironmentClient.cs,IWindowsServiceErrorHandler.cssrc/SAIF.NetCore.WindowsService/SAIF.NetCore.WindowsService/SAIF.NetCore.WindowsService/ServiceWorker.csArchitecture.md