Skip to content

Commit a48ad7a

Browse files
author
Zhen
committed
Plugged in OnErrorAsync
1 parent f0fbf34 commit a48ad7a

File tree

5 files changed

+16
-40
lines changed

5 files changed

+16
-40
lines changed

Neo4j.Driver/Neo4j.Driver.Tests/Connector/DelegatedConnectionTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ public TestDelegatedConnection(IConnection connection) : base(connection)
3636
}
3737

3838
public override void OnError(Exception error)
39+
{
40+
throw new NotImplementedException();
41+
}
42+
43+
public override Task OnErrorAsync(Exception error)
3944
{
4045
ErrorList.Add(error);
41-
throw error;
46+
return Task.FromException(error);
4247
}
4348
}
4449

Neo4j.Driver/Neo4j.Driver.Tests/Routing/ClusterConnectionPoolTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public void ShouldDeactivateServerPoolIfNotPresentInNewServersButHasInUseConnect
206206
public class AddMethod
207207
{
208208
[Fact]
209-
public void ShouldActivateIfExist()
209+
public void ShouldDeactivateIfExist()
210210
{
211211
// Given
212212
var mockedConnectionPool = new Mock<IConnectionPool>();

Neo4j.Driver/Neo4j.Driver/Internal/Connector/DelegatedConnection.cs

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ protected DelegatedConnection(IConnection connection)
3333

3434
public abstract void OnError(Exception error);
3535

36-
private void OnError(AggregateException error)
36+
public virtual Task OnErrorAsync(Exception error)
3737
{
38-
OnError(error.GetBaseException());
38+
OnError(error);
39+
return TaskExtensions.GetCompletedTask();
3940
}
4041

4142
public void Sync()
@@ -167,45 +168,16 @@ public virtual Task CloseAsync()
167168
return Delegate.CloseAsync();
168169
}
169170

170-
internal Task TaskWithErrorHandling(Func<Task> task)
171+
internal async Task TaskWithErrorHandling(Func<Task> task)
171172
{
172-
var tcs = new TaskCompletionSource<bool>();
173-
174173
try
175174
{
176-
task().ContinueWith(t =>
177-
{
178-
if (t.IsFaulted)
179-
{
180-
try
181-
{
182-
OnError(t.Exception);
183-
}
184-
catch (AggregateException exc)
185-
{
186-
tcs.SetException(exc.GetBaseException());
187-
}
188-
catch (Exception exc)
189-
{
190-
tcs.SetException(exc);
191-
}
192-
}
193-
else if (t.IsCanceled)
194-
{
195-
tcs.SetCanceled();
196-
}
197-
else
198-
{
199-
tcs.SetResult(true);
200-
}
201-
}, TaskContinuationOptions.ExecuteSynchronously);
202-
}
203-
catch (Exception e) // this is to catch whatever direct error in `task()` before returning a task
175+
await task().ConfigureAwait(false);
176+
}
177+
catch (Exception e)
204178
{
205-
OnError(e);
179+
await OnErrorAsync(e).ConfigureAwait(false);
206180
}
207-
208-
return tcs.Task;
209181
}
210182
}
211183
}

Neo4j.Driver/Neo4j.Driver/Internal/Routing/ClusterConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public override void OnError(Exception error)
5555
throw error;
5656
}
5757

58-
public async Task OnErrorAsync(Exception error)
58+
public override async Task OnErrorAsync(Exception error)
5959
{
6060
if (error is ServiceUnavailableException)
6161
{

Neo4j.Driver/Neo4j.Driver/Internal/Routing/ClusterConnectionPool.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ public void Update(IEnumerable<Uri> added, IEnumerable<Uri> removed)
142142
public async Task UpdateAsync(IEnumerable<Uri> added, IEnumerable<Uri> removed)
143143
{
144144
await AddAsync(added).ConfigureAwait(false);
145-
// TODO chain this part and use task.waitAll
146145
foreach (var uri in removed)
147146
{
148147
if (_pools.TryGetValue(uri, out var pool))

0 commit comments

Comments
 (0)