Path: redb-route/redb.Route.Controllers/ControllerErrorReporting.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Controllers/ControllerErrorReporting.cs ---- using Microsoft.Extensions.Logging; using redb.Route.Abstractions; namespace redb.Route.Controllers; /// /// What a caller is told when an action fails for a reason the framework does not model, and where the /// real reason goes instead. One place for all five dispatchers: each of them used to carry its own /// copy of "put the exception's Message in the response", and a copy is exactly how one of them /// stays wrong after the others are fixed. /// /// The exception's text is written by whoever threw it and routinely carries internal detail — a file /// path, a connection string, the name of an inner service. Handing that to a caller is a disclosure. /// The caller gets a generic sentence /// and the exchange id to quote; the exception goes to the log under the same id, so an operator can /// still find out what actually happened. Errors the framework itself produces (400 for a missing /// header, 404 for no matching action) keep their text: it is ours and says nothing private. /// /// internal static class ControllerErrorReporting { /// Error code reported for any unhandled action failure. public const string ErrorCode = "InternalError"; /// Error code reported when the request could not be bound to the action's parameters. public const string BadRequestCode = "BadRequest"; /// /// Logs a request-binding failure and returns the message the caller should see. /// /// /// A body that does not bind is the CALLER's error: an expected event class, logged as a /// Warning without a stack — junk POSTed from outside must not fill the operator's log with /// error stacks (that is also a nudge-the-server-with-garbage log-flooding vector). And unlike /// action failures, the exception text here describes the caller's own bytes (the missing /// member, the unparsable token), so returning it is the BR-4 rule, not a violation of it. /// public static string ReportBadRequest(ILogger? logger, Exception exception, IExchange exchange, string? operation) { logger?.LogWarning( "Malformed request for {Operation} (exchange {ExchangeId}): {Reason}", operation, exchange.ExchangeId, exception.Message); return exception.Message; } /// /// The dispatcher's logger. Looks in the context's own service table first and then in the DI /// container: IRouteContext.GetService<T>() reads only the table, and a host that builds /// the context as new RouteContext(serviceProvider) after AddLogging() never puts the /// factory there. Missing that second step is how withholding the exception from the caller turns /// into losing it altogether — worse for the operator than the disclosure it replaced. /// public static ILogger? CreateLogger(IRouteContext context) { var factory = context.GetService() ?? context.GetServiceProvider()?.GetService(typeof(ILoggerFactory)) as ILoggerFactory; return factory?.CreateLogger(); } /// /// Logs against the exchange and returns the message the caller may /// see. Without a logger the detail is lost — that is the host's choice of configuration, and it is /// still better than disclosing it. /// /// Dispatcher logger, or null when the context has no logger factory. /// The exception as thrown by the action (already unwrapped from any TargetInvocationException). /// The exchange being dispatched; its id is the reference given to the caller. /// What was being dispatched, for the log line (e.g. "GET modules/42"). public static string Report(ILogger? logger, Exception exception, IExchange exchange, string? operation) { logger?.LogError(exception, "Unhandled exception in controller action for {Operation} (exchange {ExchangeId})", operation, exchange.ExchangeId); return $"An unexpected error occurred while processing the request (ref: {exchange.ExchangeId})."; } }