Path: redb-route/redb.Route/Definitions/RouteDefinition.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/Definitions/RouteDefinition.cs
----
#pragma warning disable CS0619
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Concrete root route definition. Inherits the entire fluent leaf-DSL from
/// with = ;
/// adds root-only behavior — namely with declarative
/// hoisting (Apache Camel parity).
///
public class RouteDefinition : RouteDefinitionBase, IBranchingDefinition
{
/// Route-level intercepts (declared anywhere inside the route; applied to the whole route at compile).
internal readonly List Intercepts = [];
/// Route-level OnCompletion blocks.
internal readonly List OnCompletions = [];
///
/// Intercept and OnCompletion bodies: logical children kept outside , exposed so a
/// generic tree walk (the route validator, AdviceWith's MockEndpoints / Weave*) reaches
/// the steps nested in them.
///
public IEnumerable Branches => Intercepts.Concat(OnCompletions);
/// Intercepts declared on this route (read by the context at compile and by tooling).
public IReadOnlyList GetIntercepts() => Intercepts;
/// OnCompletion blocks declared on this route.
public IReadOnlyList GetOnCompletions() => OnCompletions;
///
public override IProcessor CreateProcessor(IRouteContext context)
{
if (Outputs.Count == 0)
return new DelegateProcessor(_ => { });
// Detect inline OnException blocks declared inside the route body. In Apache Camel
// these are route-scoped — they wrap the entire route pipeline regardless of where
// they appear textually, INCLUDING inside nested scopes (Transacted, Traced,
// Metered, Throttle, ...). We collect them recursively, mark them hoisted (so their
// CreateProcessor compiles to a no-op at the declaration site), then wrap the
// remaining body with the handler envelopes.
var onExceptions = new List();
CollectAndMarkOnExceptions(Outputs, onExceptions);
// Under a context's route compiler the handlers belong to the route's error scope, which a detached body of
// this route (.Threads()) is wrapped by too; known before the body compiles.
var errorScope = (context as RouteContext)?.DetachedErrorScope;
errorScope?.AddRouteHandlers(onExceptions);
if (onExceptions.Count == 0)
{
if (Outputs.Count == 1)
return NodePipeline.Node(context, Outputs[0]);
var pipeline = new PipelineProcessor();
foreach (var output in Outputs)
{
var p = NodePipeline.Node(context, output);
if (p != null)
pipeline.Add(p);
}
return pipeline;
}
var bodySteps = new List();
foreach (var output in Outputs)
{
if (output is OnExceptionDefinition)
continue;
var p = NodePipeline.Node(context, output);
if (p != null) bodySteps.Add(p);
}
IProcessor body;
if (bodySteps.Count == 0)
{
body = new DelegateProcessor(_ => { });
}
else if (bodySteps.Count == 1)
{
body = bodySteps[0];
}
else
{
var pl = new PipelineProcessor();
foreach (var s in bodySteps) pl.Add(s);
body = pl;
}
// One scope for all of the route's handlers, so the nearest exception type wins whatever the declaration
// order (Camel's exception policy). Outside a context's route compiler there is no shared scope around it.
if (errorScope is not null)
return errorScope.WrapRoute(body);
return OnExceptionDefinition.WrapBody(body, onExceptions, context);
}
private static void CollectAndMarkOnExceptions(
IList outputs, List acc)
{
foreach (var output in outputs)
{
if (output is OnExceptionDefinition oe)
{
// Do not descend into the handler steps — a nested OnException inside a
// handler pipeline is unsupported and fails fast at compilation.
oe.MarkHoisted();
acc.Add(oe);
continue;
}
if (output.Outputs.Count > 0)
CollectAndMarkOnExceptions(output.Outputs, acc);
}
}
}