Path: redb-route/redb.Route/Definitions/TryCatchDefinition.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/TryCatchDefinition.cs
----
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Scope-opener definition for the TryCatch EIP.
/// Inherits the leaf DSL from ; child steps
/// build the try body. Open catch branches with ,
/// optionally add a finally block with , close the scope with
/// .
///
public class TryCatchDefinition : RouteDefinitionBase, IRouteScope, IBranchingDefinition
{
internal readonly List Catches = [];
internal FinallyDefinition? FinallyBlock;
///
/// The catch/finally handler bodies — logical children outside (which
/// holds the try body), so a generic tree-walk (validation) reaches steps nested in a handler.
///
public IEnumerable Branches
{
get
{
foreach (var c in Catches) yield return c;
if (FinallyBlock is not null) yield return FinallyBlock;
}
}
internal TryCatchDefinition() { }
// ── Navigation ─────────────────────────────────────────────────────────────
/// Opens a typed catch branch.
public CatchDefinition Catch() where TException : Exception
=> Catch(typeof(TException));
/// Opens a catch branch for the given exception type.
public CatchDefinition Catch(Type exceptionType)
{
ArgumentNullException.ThrowIfNull(exceptionType);
if (!typeof(Exception).IsAssignableFrom(exceptionType))
throw new ArgumentException($"Type must be an Exception, got {exceptionType.Name}", nameof(exceptionType));
var def = new CatchDefinition(exceptionType, this);
Catches.Add(def);
return def;
}
/// Opens the Finally block.
/// Thrown if Finally was already set.
public FinallyDefinition Finally()
{
if (FinallyBlock != null)
throw new InvalidOperationException("Finally() can only be called once per TryCatch scope.");
FinallyBlock = new FinallyDefinition(this);
return FinallyBlock;
}
/// Closes this TryCatch scope and returns the parent route definition.
public IRouteDefinition EndTryCatch()
=> (IRouteDefinition)(Parent ?? throw new InvalidOperationException(
"EndTryCatch() called without a parent route."));
///
public IRouteDefinition End() => EndTryCatch();
// ── IProcessorDefinition ───────────────────────────────────────────────────
///
public override IProcessor CreateProcessor(IRouteContext context)
{
IProcessor body = BuildPipeline(Outputs, context);
var loggerFactory = context.GetService();
var logger = loggerFactory?.CreateLogger();
var proc = new TryCatchProcessor(body, logger);
foreach (var catchDef in Catches)
{
IProcessor handler = BuildPipeline(catchDef.Outputs, context);
proc.Catch(new CatchClause(catchDef.ExceptionType, handler, catchDef.WhenPredicate));
}
if (FinallyBlock != null)
proc.SetFinally(BuildPipeline(FinallyBlock.Outputs, context));
return proc;
}
internal static IProcessor BuildPipeline(IList outputs, IRouteContext context)
=> NodePipeline.Body(context, outputs);
}
///
/// A catch branch inside a .
/// Inherits the leaf DSL from ; child steps
/// build the catch handler pipeline. Navigate back with ,
/// open another catch with , open finally with
/// , or close the entire scope with .
///
public class CatchDefinition : RouteDefinitionBase, IRouteScope
{
internal readonly Type ExceptionType;
internal Func? WhenPredicate;
private readonly TryCatchDefinition _tryCatch;
internal CatchDefinition(Type exceptionType, TryCatchDefinition tryCatch)
{
ExceptionType = exceptionType;
_tryCatch = tryCatch;
Parent = tryCatch;
}
// ── Navigation ─────────────────────────────────────────────────────────────
/// Adds an additional guard predicate to this catch clause.
public CatchDefinition When(Func predicate) { WhenPredicate = predicate; return this; }
/// Closes this catch branch and returns to the parent TryCatch.
public TryCatchDefinition EndCatch() => _tryCatch;
/// Opens another catch branch.
public CatchDefinition Catch() where TException : Exception => _tryCatch.Catch();
/// Opens the Finally block.
public FinallyDefinition Finally() => _tryCatch.Finally();
/// Closes the entire TryCatch scope.
public IRouteDefinition EndTryCatch() => _tryCatch.EndTryCatch();
///
public IRouteDefinition End() => EndTryCatch();
// ── IProcessorDefinition ───────────────────────────────────────────────────
///
public override IProcessor CreateProcessor(IRouteContext context)
=> throw new InvalidOperationException(
"CatchDefinition is compiled via its parent TryCatchDefinition.CreateProcessor.");
}
///
/// The Finally block inside a .
/// Inherits the leaf DSL from ; child steps
/// build the finally pipeline. Close with or
/// .
///
public class FinallyDefinition : RouteDefinitionBase, IRouteScope
{
private readonly TryCatchDefinition _tryCatch;
internal FinallyDefinition(TryCatchDefinition tryCatch)
{
_tryCatch = tryCatch;
Parent = tryCatch;
}
// ── Navigation ─────────────────────────────────────────────────────────────
/// Closes the Finally block and returns to the parent TryCatch.
public TryCatchDefinition EndFinally() => _tryCatch;
/// Closes the entire TryCatch scope.
public IRouteDefinition EndTryCatch() => _tryCatch.EndTryCatch();
///
public IRouteDefinition End() => EndTryCatch();
// ── IProcessorDefinition ───────────────────────────────────────────────────
///
public override IProcessor CreateProcessor(IRouteContext context)
=> throw new InvalidOperationException(
"FinallyDefinition is compiled via its parent TryCatchDefinition.CreateProcessor.");
}