Path: redb-route/redb.Route/Core/ExchangeProcessing.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/ExchangeProcessing.cs
----
using redb.Route.Abstractions;
using redb.Route.Telemetry;
namespace redb.Route.Core;
///
/// How a consumer base runs one exchange through its route, shared by and
/// : what escapes the pipeline goes on the exchange, the consumer's own stop passes through,
/// and a failure marks the receive span.
///
internal static class ExchangeProcessing
{
///
/// Runs through . The route's own error handling
/// (OnException, DeadLetterChannel) runs inside the pipeline, and a failure it did not handle stays on the exchange;
/// what escapes is put on the exchange too, so the consumer decides on a failed route rather than a clean one, and is
/// returned for the consumer's log line. A cancellation of (the consumer's stop) is
/// rethrown.
///
/// The exception that escaped the pipeline, or null.
internal static async Task RunAsync(IProcessor processor, IExchange exchange, CancellationToken processingCt)
{
try
{
await processor.Process(exchange, processingCt).ConfigureAwait(false);
return null;
}
catch (OperationCanceledException) when (processingCt.IsCancellationRequested)
{
throw; // shutdown: the consumer ends this delivery
}
catch (Exception ex)
{
exchange.Exception ??= ex;
return ex;
}
}
///
/// Whether the route failed: a failure is on the exchange, not handled, and it is not the cancellation of the
/// consumer's own stop (any other cancellation, an HttpClient timeout inside the route, is a failure).
///
internal static bool Failed(IExchange exchange, CancellationToken processingCt)
=> exchange.Exception is { } failure && !exchange.ExceptionHandled
&& (failure is not OperationCanceledException || !processingCt.IsCancellationRequested);
/// Marks failed when says the route failed.
internal static void RecordFailure(IExchange exchange, TransportSpan span, CancellationToken processingCt)
{
if (Failed(exchange, processingCt))
RouteTelemetryExtensions.RecordFailure(span.Activity, exchange.Exception!);
}
}