Skip to content

Commit

Permalink
Factor out catching and transforming execution exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcweber committed Nov 16, 2023
1 parent 54d06bc commit 6919631
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
35 changes: 5 additions & 30 deletions src/Core/Execution/GremlinQueryExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,36 +128,11 @@ public TransformExecutionExceptionGremlinQueryExecutor(IGremlinQueryExecutor bas
_exceptionTransformation = exceptionTransformation;
}

public IAsyncEnumerable<T> Execute<T>(GremlinQueryExecutionContext context)
{
return Core(this, context);

static async IAsyncEnumerable<T> Core(TransformExecutionExceptionGremlinQueryExecutor @this, GremlinQueryExecutionContext context, [EnumeratorCancellation] CancellationToken ct = default)
{
var enumerator = @this._baseExecutor
.Execute<T>(context)
.WithCancellation(ct)
.GetAsyncEnumerator();

await using (enumerator)
{
while (true)
{
try
{
if (!await enumerator.MoveNextAsync())
yield break;
}
catch (GremlinQueryExecutionException ex)
{
throw @this._exceptionTransformation(ex);
}

yield return enumerator.Current;
}
}
}
}
public IAsyncEnumerable<T> Execute<T>(GremlinQueryExecutionContext context) => _baseExecutor
.Execute<T>(context)
.Catch(ex => ex is GremlinQueryExecutionException executionException
? _exceptionTransformation(executionException)
: ex);
}

private sealed class SerializingGremlinQueryExecutor : IGremlinQueryExecutor
Expand Down
28 changes: 28 additions & 0 deletions src/Core/Extensions/AsyncEnumerable.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Runtime.CompilerServices;

using ExRam.Gremlinq.Core.Execution;

namespace ExRam.Gremlinq.Core
{
internal static class AsyncEnumerable
Expand Down Expand Up @@ -114,5 +116,31 @@ public static async ValueTask<TSource[]> ToArrayAsync<TSource>(this IAsyncEnumer

return list.ToArray();
}

public static IAsyncEnumerable<T> Catch<T>(this IAsyncEnumerable<T> source, Func<Exception, Exception> exceptionTransformation)
{
return Core(source, exceptionTransformation);

static async IAsyncEnumerable<T> Core(IAsyncEnumerable<T> source, Func<Exception, Exception> exceptionTransformation, [EnumeratorCancellation] CancellationToken ct = default)
{
await using (var enumerator = source.WithCancellation(ct).GetAsyncEnumerator())
{
while (true)
{
try
{
if (!await enumerator.MoveNextAsync())
yield break;
}
catch (Exception ex)
{
throw exceptionTransformation(ex);
}

yield return enumerator.Current;
}
}
}
}
}
}

0 comments on commit 6919631

Please sign in to comment.