Path: redb-route/tests/redb.Route.Tests.Sql/Batch/BatchRouteErrorHandlingTests.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/tests/redb.Route.Tests.Sql/Batch/BatchRouteErrorHandlingTests.cs
----
using System.Data.Common;
using redb.Route.Core;
using redb.Route.Sql;
namespace redb.Route.Tests.Sql.Batch;
///
/// A failed batch inside a route: the route's OnException<DbException> must match it. Handlers are found by the
/// thrown exception's type and its base types, so this holds only if the provider's exception reaches the route unwrapped.
///
public sealed class BatchRouteErrorHandlingTests : IAsyncLifetime
{
private readonly SqliteTestHelper _db = new();
private readonly RouteContext _context = new();
public BatchRouteErrorHandlingTests() =>
_db.Execute("CREATE TABLE route_items (id INTEGER PRIMARY KEY, val TEXT NOT NULL)");
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
await _context.DisposeAsync();
_db.Dispose();
}
[Fact]
public async Task Break_OnExceptionDbException_HandlerRuns()
{
Exception? handled = null;
_context.AddComponent(new SqlComponent());
_context.AddToRegistry("main", _db.CreateFactory());
_context.AddRoutes(r =>
{
r.From("direct://sql-batch-errors")
.OnException()
.Handled()
.Process(e => handled = e.Exception)
.EndOnException()
.To("sql:INSERT INTO route_items (id, val) VALUES (:#id, :#val)?dataSource=main&batchSize=10&outputType=None");
});
await _context.Start();
var producer = _context.GetEndpoint("direct://sql-batch-errors").CreateProducer();
await producer.Start();
var items = new List>
{
new() { ["id"] = 1, ["val"] = "a" },
new() { ["id"] = 1, ["val"] = "dup" },
};
var escaped = await Record.ExceptionAsync(() => producer.Process(new Exchange(new Message(items))));
handled.Should().BeAssignableTo(
$"the route's OnException handles the failed batch (escaped the route: {escaped?.GetType().Name ?? "nothing"})");
Convert.ToInt64(_db.ExecuteScalar("SELECT COUNT(*) FROM route_items")).Should().Be(0);
}
}