Path: redb-route/redb.Route/Definitions/ThrowDefinition.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/ThrowDefinition.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Leaf definition that rethrows the exception stored on the exchange.
///
public sealed class RethrowExceptionDefinition : ProcessorDefinition
{
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new DelegateProcessor(exchange =>
throw exchange.Exception ?? new InvalidOperationException("No exception on exchange to rethrow."));
}
///
/// Leaf definition that throws a new with a static message.
///
public sealed class ThrowMessageDefinition : ProcessorDefinition
{
private readonly string _message;
/// Creates a throw definition with a static message.
public ThrowMessageDefinition(string message)
{
ArgumentException.ThrowIfNullOrWhiteSpace(message);
_message = message;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new DelegateProcessor(_ => throw new Exception(_message));
}
///
/// Leaf definition that throws a specific pre-built exception instance.
///
public sealed class ThrowExceptionDefinition : ProcessorDefinition
{
private readonly Exception _exception;
/// Creates a throw definition from an exception instance.
public ThrowExceptionDefinition(Exception exception)
{
ArgumentNullException.ThrowIfNull(exception);
_exception = exception;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new DelegateProcessor(_ => throw _exception);
}
///
/// Leaf definition that instantiates and throws an exception by type.
///
public sealed class ThrowExceptionTypeDefinition : ProcessorDefinition
{
private readonly Type _exceptionType;
private readonly string? _message;
/// Creates a throw definition for a specific exception type.
/// Exception type to instantiate and throw.
/// Optional message to pass to the exception constructor.
public ThrowExceptionTypeDefinition(Type exceptionType, string? message = null)
{
ArgumentNullException.ThrowIfNull(exceptionType);
if (!typeof(Exception).IsAssignableFrom(exceptionType))
throw new ArgumentException($"Type '{exceptionType.Name}' does not inherit from Exception.", nameof(exceptionType));
_exceptionType = exceptionType;
_message = message;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new DelegateProcessor(_ =>
throw (Exception)(_message is not null
? Activator.CreateInstance(_exceptionType, _message)
: Activator.CreateInstance(_exceptionType))!);
}
///
/// Leaf definition that marks the exchange exception as handled.
///
public sealed class ExceptionHandledDefinition : ProcessorDefinition
{
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new DelegateProcessor(exchange =>
{
exchange.ExceptionHandled = true;
exchange.Exception = null;
});
}