Path: redb-route/redb.Route/Core/ExchangeFailureExtensions.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/ExchangeFailureExtensions.cs ---- using System.Runtime.ExceptionServices; using redb.Route.Abstractions; namespace redb.Route.Core; /// /// Consumer-side view of an exchange's terminal state. A route whose OnException has no /// Handled(true) returns normally with the exception still on the exchange; for a /// consumer that decides ack/nack by "did Process throw", that failure would be acknowledged. /// public static class ExchangeFailureExtensions { /// /// Exchange property set by .RollbackAll(), Camel's markRollbackOnly(): the unit of work is rolled /// back without an exception. /// public const string RollbackOnlyPropertyKey = "RollbackOnly"; /// Marks the unit of work for rollback: the enclosing transaction rolls back and the consumer does not acknowledge. public static void MarkRollbackOnly(this IExchange exchange) => exchange.Properties[RollbackOnlyPropertyKey] = true; /// Whether the route marked the unit of work for rollback (.RollbackAll()). public static bool IsRollbackOnly(this IExchange exchange) => exchange.Properties.TryGetValue(RollbackOnlyPropertyKey, out var value) && value is true; /// /// Exchange property set by an OnException handler that ran with Handled(true) — Apache Camel's /// ExchangeHelper.setFailureHandled. The exchange completes successfully (the consumer acknowledges), but the /// failure WAS handled by an error handler, so a dedup key guarding the failed work (the idempotent consumer) must /// not be confirmed. Cleared on continued and before every redelivery attempt. /// public const string FailureHandledPropertyKey = "FailureHandled"; /// Marks the failure an error handler handled (Camel's setFailureHandled(true)). public static void MarkFailureHandled(this IExchange exchange) => exchange.Properties[FailureHandledPropertyKey] = true; /// Clears the failure-handled marker: a redelivery, or a continued handler (Camel's setFailureHandled(false)). public static void ClearFailureHandled(this IExchange exchange) => exchange.Properties.Remove(FailureHandledPropertyKey); /// Whether an error handler handled the failure on this exchange (Camel's ExchangeHelper.isFailureHandled). public static bool IsFailureHandled(this IExchange exchange) => exchange.Properties.TryGetValue(FailureHandledPropertyKey, out var value) && value is true; /// /// Whether the unit of work did not succeed: an unhandled failure is left on the exchange, or the route marked it /// rollback-only. A consumer does not acknowledge such an exchange. /// public static bool EndedInFailure(this IExchange exchange) => (exchange.Exception is not null && !exchange.ExceptionHandled) || exchange.IsRollbackOnly(); /// /// Rethrows the exchange's unhandled failure (stack preserved) so the consumer's existing /// failure path — nack / rollback / no-ack — handles it exactly like a thrown failure. /// A handled failure () is a completed exchange and is left alone. /// A rollback-only exchange throws too: its work was rolled back, so the message must be delivered again. /// public static void ThrowIfUnhandledFailure(this IExchange exchange) { if (exchange.Exception is { } failure && !exchange.ExceptionHandled) ExceptionDispatchInfo.Capture(failure).Throw(); if (exchange.IsRollbackOnly()) throw RollbackOnlyFailure(); } /// /// The failure an exchange that returned normally ended in, as sees it: the unhandled /// exception left on it, or, for a rollback-only exchange, an exception saying so; when it /// succeeded. /// internal static Exception? FailureOf(IExchange exchange) { if (exchange.Exception is { } failure && !exchange.ExceptionHandled) return failure; return exchange.IsRollbackOnly() ? RollbackOnlyFailure() : null; } private static InvalidOperationException RollbackOnlyFailure() => new( "The route marked the exchange rollback-only (.RollbackAll()): its work was rolled back, so the message " + "is not acknowledged and the broker will deliver it again."); }