Path: redb-route/redb.Route/Core/RouteErrorScope.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/Core/RouteErrorScope.cs ---- using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Definitions; using redb.Route.Processors; namespace redb.Route.Core; /// /// The error handling of one route, as in Apache Camel, from the inside out: /// /// the route's own OnException blocks (hoisted from its body); /// the OnException blocks the route shares with other routes: those of its builder — that builder's only /// (Camel's RouteBuilder scope) — then those of its route configurations, which are added after the builder's and so /// are used instead of a builder's handler of the same exception type; /// the context's error handling (): AddLocalExceptionHandler of the /// route, AddGlobalExceptionHandler, — for a failure no OnException /// took, as Camel's context error handler. /// /// /// Layers 1 and 2 choose together, as Camel's exception policy does: the handler whose exception type is nearest to /// the failure wins wherever it is declared, and the route's own wins a tie (). /// So a route's OnException<Exception> does not hide a shared OnException<IOException>. /// /// /// An ordinary route is wrapped in two parts, because the route policy and the processing timeout sit between them: /// (layer 1, inside the route body) and (layers 2 and 3, around the /// timeout). A body that runs after its caller has returned — .Threads() for an in-only exchange — is no longer /// inside those wrappers, so it is wrapped by the whole scope (): a failure there gets the same /// handlers with their whole configuration (OnWhen, redeliveries, Handled, UseOriginalBody...). /// /// /// Each handler body is compiled once, where it always was: the route's inside the route's compile frame, the shared /// ones after it. The wrappers are therefore completed only in , by which time every handler /// exists; a detached wrapper refuses to run before that (the route has not finished compiling). /// /// internal sealed class RouteErrorScope { private readonly RouteContext _context; private readonly IReadOnlyList _sharedHandlers; private readonly List _routeHandlers = []; private readonly Dictionary _bodies = new(ReferenceEqualityComparer.Instance); private readonly List _detached = []; private readonly ILogger? _logger; private OnExceptionProcessor? _routeLayer; private bool _sealed; /// The context the route compiles in. /// The route this scope belongs to. /// The OnException blocks of the route's builder. /// The OnException blocks of the route configurations of the route. /// Logger of the handlers. internal RouteErrorScope(RouteContext context, string routeId, IReadOnlyList builderHandlers, IReadOnlyList configurationHandlers, ILogger? logger) { _context = context; RouteId = routeId; _sharedHandlers = [.. builderHandlers, .. configurationHandlers]; _logger = logger; } /// The route this scope belongs to. internal string RouteId { get; } /// Takes the route's own OnException blocks, in declaration order. internal void AddRouteHandlers(IEnumerable handlers) => _routeHandlers.AddRange(handlers); /// Layer 1: the route's own OnException blocks around . internal IProcessor WrapRoute(IProcessor body) { _routeLayer = Layer(body, _routeHandlers); return _routeLayer ?? body; } /// /// Layers 2 and 3 around : the shared OnException blocks, then the context's error /// handling. Links the route's own layer to the shared one and completes the detached wrappers of this route. /// internal IProcessor WrapOuter(IProcessor body) { var shared = Layer(body, _sharedHandlers); if (_routeLayer is not null) _routeLayer.Outer = shared; foreach (var detached in _detached) { var route = Layer(detached.Body, _routeHandlers); var outer = Layer(route ?? detached.Body, _sharedHandlers); if (route is not null) route.Outer = outer; detached.Target = Context(outer ?? route ?? detached.Body); } _sealed = true; return Context(shared ?? body); } /// /// The whole scope around a body that runs after its caller has returned. The wrapper is completed by /// , when every handler body of the route is compiled. /// internal IProcessor WrapDetached(IProcessor body) { if (_sealed) throw new InvalidOperationException($"The error scope of route '{RouteId}' is already complete."); var detached = new DetachedBody(body, RouteId); _detached.Add(detached); return detached; } /// /// Whether an OnException of this route, of its builder or of its route configurations covers /// . /// internal bool Handles(Type exceptionType) => _routeHandlers.Concat(_sharedHandlers) .Any(h => h.ExceptionTypes.Any(t => t.IsAssignableFrom(exceptionType))); private IProcessor Context(IProcessor body) => new ContextErrorLayerProcessor(body, _context, RouteId); /// One processor for all of around ; none when there are no handlers. private OnExceptionProcessor? Layer(IProcessor body, IReadOnlyList handlers) { if (handlers.Count == 0) return null; var layer = new OnExceptionProcessor(body, _logger); foreach (var handler in handlers) handler.AddTo(layer, BodyOf(handler)); return layer; } private IProcessor BodyOf(OnExceptionDefinition handler) { if (!_bodies.TryGetValue(handler, out var body)) { body = _context.CompileHandlerBody(handler.Outputs); _bodies[handler] = body; } return body; } /// A detached body whose error scope is completed when the route's compilation ends. private sealed class DetachedBody(IProcessor body, string routeId) : IProcessor { internal IProcessor Body { get; } = body; internal IProcessor? Target { get; set; } public Task Process(IExchange exchange, CancellationToken ct = default) => (Target ?? throw new InvalidOperationException( $"Route '{routeId}' has not finished compiling: its error scope is not complete.")) .Process(exchange, ct); } }