SAIF.NetCore.ErrorHandler overview¶
Documentation context: Packaged Component (NuGet)
SAIF.NetCore.ErrorHandler is an implementation-layer NuGet package 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.
Purpose¶
- Translate any exception caught by a host's pipeline or task runner into a consistent, loggable, and (for HTTP hosts) client-facing representation, without the host reimplementing exception classification or response formatting.
- Provide two parallel, independently usable entry points:
ErrorHandler.LogAndReturnExceptionfor ASP.NET Core responses, andWindowsServiceErrorHandler.HandleErrorfor Windows Service task failures. - Offer extensibility points — the
BeginErrorHandling/ErrorHandledevents and the pluggableSpecialErrorAction/SpecialErrorAction<T>list — so a Windows Service host can veto logging, inspect or add context, or run custom recovery/notification logic without modifying this library.
Responsibilities¶
- Log every handled HTTP exception at Error level, capture the logger's returned ID as the response's
Error-IDcorrelation header, and also return aServer-Nameheader identifying the host machine. - Classify exceptions for HTTP hosts in precedence order —
HttpStatusCodeException, thenSAIFException, then any otherException— each producing a different status code, content type, and handled/unhandled body (the legacy[Obsolete]ErrorHandler.Invokepath; the currentDynamicErrorHandlerinSAIF.NetCore.ServiceExtensionsre-implements the same precedence). - Build and log a formatted, multi-line Windows Service error, raising
BeginErrorHandlingandErrorHandledevents so a host can veto logging, add context objects, or suppress special actions. - Execute pluggable, fire-conditional special error actions that return a
SpecialErrorActionResult; only the wrapped action delegate is caught, so a throwing fire-condition predicate, a non-conformingISpecialErrorActionoverride, or a throwing failure-logger call can still escape and stop the remaining actions (and the subsequentErrorHandledevent) from running. - Flatten exception stack traces across the inner-exception chain and extract
SAIFException.LogDetailsas strings for inclusion in logged output.
Key components¶
ErrorHandler/IErrorHandler: the ASP.NET Core-facing exception-to-response translator.WindowsServiceErrorHandler/IWindowsServiceErrorHandler(ServiceContracts): the Windows Service-facing exception handler, owning theBeginErrorHandling/ErrorHandledevents, theSpecialErrorActionslist, and theShutdownExemptExceptionTypeslist.SpecialErrorAction/SpecialErrorAction<T>: delegate wrappers with an optional fire-condition predicate.ContextContainer(IErrorContextContainer): the 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.
Related documentation¶
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.cs