Skip to content

Commit

Permalink
clean up exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Dec 23, 2024
1 parent 5803b75 commit eec8d82
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 42 deletions.
36 changes: 20 additions & 16 deletions src/Libplanet.Net/Protocols/KademliaProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ await PingAsync(peer, dialTimeout, cancellationToken)
dialTimeout,
cancellationToken));
}
catch (PingTimeoutException)
catch (PingFailedException)
{
_logger.Warning("A timeout exception occurred connecting to seed peer");
RemovePeer(peer);
Expand Down Expand Up @@ -140,19 +140,25 @@ public async Task AddPeersAsync(
var tasks = new List<Task>();
foreach (BoundPeer peer in peers)
{
tasks.Add(PingAsync(
peer,
timeout: timeout,
cancellationToken: cancellationToken));
tasks.Add(
PingAsync(
peer,
timeout: timeout,
cancellationToken: cancellationToken));
}

_logger.Verbose("Trying to ping {PeerCount} peers", tasks.Count);
await Task.WhenAll(tasks).ConfigureAwait(false);
_logger.Verbose("Update complete");
}
catch (PingTimeoutException e)
catch (PingFailedException pfe)
{
_logger.Debug(e, "Ping timed out");
if (pfe.InnerException is { } e)
{
throw e;
}

throw;
}
catch (TaskCanceledException e)
{
Expand Down Expand Up @@ -284,7 +290,7 @@ public async Task CheckReplacementCacheAsync(CancellationToken cancellationToken
await PingAsync(replacement, _requestTimeout, cancellationToken)
.ConfigureAwait(false);
}
catch (PingTimeoutException)
catch (PingFailedException)
{
_logger.Verbose(
"Removed stale peer {Peer} from replacement cache",
Expand Down Expand Up @@ -327,7 +333,7 @@ await PingAsync(replacement, _requestTimeout, cancellationToken)
await PingAsync(boundPeer, _requestTimeout, cancellationToken)
.ConfigureAwait(false);
}
catch (PingTimeoutException)
catch (PingFailedException)
{
var msg =
"{BoundPeer}, a target peer, is in the routing table does not respond";
Expand Down Expand Up @@ -394,7 +400,7 @@ await PingAsync(found, _requestTimeout, cancellationToken)
throw new TaskCanceledException(
$"Task is cancelled during {nameof(FindSpecificPeerAsync)}()");
}
catch (PingTimeoutException)
catch (PingFailedException)
{
// Ignore peer not responding
}
Expand Down Expand Up @@ -439,11 +445,9 @@ internal async Task PingAsync(

AddPeer(peer);
}
catch (CommunicationFailException)
catch (Exception e)
{
throw new PingTimeoutException(
$"Failed to send Ping to {peer}.",
peer);
throw new PingFailedException(peer, e);
}
}

Expand Down Expand Up @@ -497,7 +501,7 @@ private async Task ValidateAsync(
await PingAsync(peer, timeout, cancellationToken).ConfigureAwait(false);
_table.Check(peer, check, DateTimeOffset.UtcNow);
}
catch (PingTimeoutException)
catch (PingFailedException)
{
_logger.Verbose("Removing invalid peer {Peer}...", peer);
RemovePeer(peer);
Expand Down Expand Up @@ -711,7 +715,7 @@ private async Task ProcessFoundAsync(
AggregateException aggregateException = aggregateTask.Exception!;
foreach (Exception e in aggregateException.InnerExceptions)
{
if (e is PingTimeoutException pte)
if (e is PingFailedException pte)
{
peers.Remove(pte.Target);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,21 @@
namespace Libplanet.Net.Protocols
{
[Serializable]
public class PingTimeoutException : TimeoutException
public class PingFailedException : Exception
{
public PingTimeoutException(BoundPeer target)
: base()
public PingFailedException(BoundPeer target, Exception innerException)
: base($"Failed to send ping to target peer {target}", innerException)
{
Target = target;
}

public PingTimeoutException(string message, BoundPeer target)
: base(message)
{
Target = target;
}

public PingTimeoutException(string message, BoundPeer target, Exception innerException)
public PingFailedException(string message, BoundPeer target, Exception innerException)
: base(message, innerException)
{
Target = target;
}

protected PingTimeoutException(SerializationInfo info, StreamingContext context)
protected PingFailedException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
Target = info.GetValue(nameof(Target), typeof(BoundPeer)) is BoundPeer target
Expand Down
2 changes: 1 addition & 1 deletion test/Libplanet.Net.Tests/Protocols/TestTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ async Task DoAddPeersAsync()
"Different version encountered during {MethodName}()",
nameof(AddPeersAsync));
}
catch (PingTimeoutException)
catch (PingFailedException)
{
var msg =
$"Timeout occurred during {nameof(AddPeersAsync)}() after {timeout}";
Expand Down
33 changes: 19 additions & 14 deletions test/Libplanet.Net.Tests/SwarmTest.AppProtocolVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ public async Task DetectAppProtocolVersion()
await StartAsync(c);
await StartAsync(d);

var peers = new[] { c.AsPeer, d.AsPeer };

foreach (var peer in peers)
{
await a.AddPeersAsync(new[] { peer }, null);
await b.AddPeersAsync(new[] { peer }, null);
}
await a.AddPeersAsync(new[] { c.AsPeer }, null);
await Assert.ThrowsAsync<InvalidMessageContentException>(
() => a.AddPeersAsync(new[] { d.AsPeer }, null));
await Assert.ThrowsAsync<InvalidMessageContentException>(
() => b.AddPeersAsync(new[] { c.AsPeer }, null));
await b.AddPeersAsync(new[] { d.AsPeer }, null);

Assert.Equal(new[] { c.AsPeer }, a.Peers.ToArray());
Assert.Equal(new[] { d.AsPeer }, b.Peers.ToArray());
Expand Down Expand Up @@ -163,14 +162,20 @@ AppProtocolVersion localVersion
await StartAsync(f);

await a.AddPeersAsync(new[] { c.AsPeer }, TimeSpan.FromSeconds(1));
await a.AddPeersAsync(new[] { d.AsPeer }, TimeSpan.FromSeconds(1));
await a.AddPeersAsync(new[] { e.AsPeer }, TimeSpan.FromSeconds(1));
await a.AddPeersAsync(new[] { f.AsPeer }, TimeSpan.FromSeconds(1));

await b.AddPeersAsync(new[] { c.AsPeer }, TimeSpan.FromSeconds(1));
await Assert.ThrowsAsync<InvalidMessageContentException>(
() => a.AddPeersAsync(new[] { d.AsPeer }, TimeSpan.FromSeconds(1)));
await Assert.ThrowsAsync<InvalidMessageContentException>(
() => a.AddPeersAsync(new[] { e.AsPeer }, TimeSpan.FromSeconds(1)));
await Assert.ThrowsAsync<InvalidMessageContentException>(
() => a.AddPeersAsync(new[] { f.AsPeer }, TimeSpan.FromSeconds(1)));

await Assert.ThrowsAsync<InvalidMessageContentException>(
() => b.AddPeersAsync(new[] { c.AsPeer }, TimeSpan.FromSeconds(1)));
await b.AddPeersAsync(new[] { d.AsPeer }, TimeSpan.FromSeconds(1));
await b.AddPeersAsync(new[] { e.AsPeer }, TimeSpan.FromSeconds(1));
await b.AddPeersAsync(new[] { f.AsPeer }, TimeSpan.FromSeconds(1));
await Assert.ThrowsAsync<InvalidMessageContentException>(
() => b.AddPeersAsync(new[] { e.AsPeer }, TimeSpan.FromSeconds(1)));
await Assert.ThrowsAsync<InvalidMessageContentException>(
() => b.AddPeersAsync(new[] { f.AsPeer }, TimeSpan.FromSeconds(1)));

Assert.Equal(new[] { c.AsPeer }, a.Peers.ToArray());
Assert.Equal(new[] { d.AsPeer }, b.Peers.ToArray());
Expand Down

0 comments on commit eec8d82

Please sign in to comment.