Skip to content

Commit 2d29299

Browse files
SimonCroppAarononthewebArkatufus
authored
leverage exception when (#7614)
* leverage exception when * Post-merge fix --------- Co-authored-by: Aaron Stannard <aaron@petabridge.com> Co-authored-by: Gregorius Soedharmo <arkatufus@yahoo.com>
1 parent 0b91a04 commit 2d29299

File tree

11 files changed

+45
-69
lines changed

11 files changed

+45
-69
lines changed

src/contrib/testkits/Akka.TestKit.Xunit/Internals/Loggers.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,13 @@ private void HandleLogEvent(LogEvent e)
4646
_output.WriteLine(e.ToString());
4747
}
4848
catch (FormatException ex)
49+
when (e.Message is LogMessage msg)
4950
{
50-
if (e.Message is LogMessage msg)
51-
{
52-
var message =
53-
$"Received a malformed formatted message. Log level: [{e.LogLevel()}], Template: [{msg.Format}], args: [{string.Join(",", msg.Unformatted())}]";
54-
if (e.Cause != null)
55-
throw new AggregateException(message, ex, e.Cause);
56-
throw new FormatException(message, ex);
57-
}
58-
59-
throw;
51+
var message =
52+
$"Received a malformed formatted message. Log level: [{e.LogLevel()}], Template: [{msg.Format}], args: [{string.Join(",", msg.Unformatted())}]";
53+
if (e.Cause != null)
54+
throw new AggregateException(message, ex, e.Cause);
55+
throw new FormatException(message, ex);
6056
}
6157
catch (InvalidOperationException ie)
6258
{

src/contrib/testkits/Akka.TestKit.Xunit2/Internals/Loggers.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,13 @@ private void HandleLogEvent(LogEvent e)
4646
_output.WriteLine(e.ToString());
4747
}
4848
catch (FormatException ex)
49+
when (e.Message is LogMessage msg)
4950
{
50-
if (e.Message is LogMessage msg)
51-
{
52-
var message =
53-
$"Received a malformed formatted message. Log level: [{e.LogLevel()}], Template: [{msg.Format}], args: [{string.Join(",", msg.Unformatted())}]";
54-
if (e.Cause != null)
55-
throw new AggregateException(message, ex, e.Cause);
56-
throw new FormatException(message, ex);
57-
}
58-
59-
throw;
51+
var message =
52+
$"Received a malformed formatted message. Log level: [{e.LogLevel()}], Template: [{msg.Format}], args: [{string.Join(",", msg.Unformatted())}]";
53+
if (e.Cause != null)
54+
throw new AggregateException(message, ex, e.Cause);
55+
throw new FormatException(message, ex);
6056
}
6157
catch (InvalidOperationException ie)
6258
{

src/core/Akka.Discovery/Discovery.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,16 @@ private ServiceDiscovery CreateServiceDiscovery(string method)
7979
return Create(className);
8080
}
8181
catch (Exception ex)
82+
when (ex is TypeLoadException or MissingMethodException)
8283
{
83-
if (ex is TypeLoadException or MissingMethodException)
84-
throw new ArgumentException(
85-
message: $"Illegal akka.discovery.{method}.class value or incompatible class!\n" +
86-
"The implementation class MUST extend Akka.Discovery.ServiceDiscovery with:\n" +
87-
" * parameterless constructor, " +
88-
$" * constructor with a single {nameof(ExtendedActorSystem)} parameter, or\n" +
89-
$" * constructor with {nameof(ExtendedActorSystem)} and {nameof(Configuration.Config)} parameters.",
90-
paramName: nameof(method),
91-
innerException: ex);
92-
throw;
84+
throw new ArgumentException(
85+
message: $"Illegal akka.discovery.{method}.class value or incompatible class!\n" +
86+
"The implementation class MUST extend Akka.Discovery.ServiceDiscovery with:\n" +
87+
" * parameterless constructor, " +
88+
$" * constructor with a single {nameof(ExtendedActorSystem)} parameter, or\n" +
89+
$" * constructor with {nameof(ExtendedActorSystem)} and {nameof(Configuration.Config)} parameters.",
90+
paramName: nameof(method),
91+
innerException: ex);
9392
}
9493

9594
ServiceDiscovery Create(string typeName)

src/core/Akka.Persistence/Journal/ReplayFilter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,18 @@ protected override bool Receive(object message)
233233
throw new ArgumentException("Mode must not be Disabled");
234234
}
235235
}
236+
236237
node = next;
237238
}
239+
238240
_buffer.AddLast(value);
239241
}
240242

241243
}
242244
catch (IllegalStateException ex)
245+
when (Mode == ReplayFilterMode.Fail)
243246
{
244-
if (Mode == ReplayFilterMode.Fail)
245-
Fail(ex);
246-
else
247-
throw;
247+
Fail(ex);
248248
}
249249
}
250250
else if (message is RecoverySuccess or ReplayMessagesFailure)

src/core/Akka.Remote.Tests/Transport/DotNettyTransportDnsResolutionSpec.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,11 @@ public Property HeliosTransport_Should_Resolve_DNS(EndPoint inbound, EndPoint ou
155155
useIpv6Dns: dnsIpv6,
156156
enforceIpFamily: enforceIpFamily);
157157
}
158-
catch
158+
catch when (enforceIpFamily && IsExpectedFailure(inbound, outbound, dnsIpv6))
159159
{
160160
//if ip family is enforced, there are some special cases when it is normal to unable
161161
//to create actor system
162-
if (enforceIpFamily && IsExpectedFailure(inbound, outbound, dnsIpv6))
163-
return true.ToProperty();
164-
throw;
162+
return true.ToProperty();
165163
}
166164

167165
var outboundReceivedAck = true;
@@ -246,14 +244,13 @@ public Property HeliosTransport_Should_Resolve_DNS_with_PublicHostname(IPEndPoin
246244
dnsUseIpv6,
247245
enforceIpFamily);
248246
}
249-
catch
247+
catch when (enforceIpFamily && IsExpectedFailure(inbound, outbound, dnsUseIpv6))
250248
{
251249
//if ip family is enforced, there are some special cases when it is normal to unable
252250
//to create actor system
253-
if (enforceIpFamily && IsExpectedFailure(inbound, outbound, dnsUseIpv6))
254-
return true.ToProperty();
255-
throw;
251+
return true.ToProperty();
256252
}
253+
257254
var outboundReceivedAck = true;
258255
var inboundReceivedAck = true;
259256
_outbound.ActorSelection(_inboundAck).Tell("ping", _outboundProbe.Ref);

src/core/Akka.Remote/Transport/DotNetty/DotNettyTransportSettings.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,10 @@ public static SslSettings CreateOrDefault(Config config, SslSettings? @default =
257257
{
258258
return Create(config);
259259
}
260-
catch(Exception)
260+
catch (Exception)
261+
when (@default != null)
261262
{
262-
if (@default != null)
263-
return @default;
264-
throw;
263+
return @default;
265264
}
266265
}
267266

src/core/Akka.Streams/Implementation/ActorPublisher.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,8 @@ private void ReportSubscribeFailure(ISubscriber<TOut> subscriber)
267267
}
268268
}
269269
catch (Exception exception)
270+
when (exception is ISpecViolation)
270271
{
271-
if (!(exception is ISpecViolation))
272-
throw;
273272
}
274273
}
275274
}

src/core/Akka.Streams/Implementation/CompletedPublishers.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ public void Subscribe(ISubscriber<T> subscriber)
3838
ReactiveStreamsCompliance.TryOnComplete(subscriber);
3939
}
4040
catch (Exception e)
41+
when (e is ISpecViolation)
4142
{
42-
if (!(e is ISpecViolation))
43-
throw;
4443
}
4544
}
4645

@@ -91,9 +90,8 @@ public void Subscribe(ISubscriber<T> subscriber)
9190
ReactiveStreamsCompliance.TryOnError(subscriber, Cause);
9291
}
9392
catch (Exception e)
93+
when (e is ISpecViolation)
9494
{
95-
if (!(e is ISpecViolation))
96-
throw;
9795
}
9896
}
9997

@@ -270,9 +268,8 @@ public void Subscribe(ISubscriber<T> subscriber)
270268
ReactiveStreamsCompliance.RejectAdditionalSubscriber(subscriber, "Publisher");
271269
}
272270
catch (Exception e)
271+
when (e is ISpecViolation)
273272
{
274-
if (!(e is ISpecViolation))
275-
throw;
276273
}
277274
}
278275

src/core/Akka.Streams/Implementation/SubscriberManagement.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,10 @@ private long DispatchFromBufferAndReturnRemainingRequested(long requested, ISubs
265265
goOn = true;
266266
}
267267
catch (Exception e)
268+
when (e is ISpecViolation)
268269
{
269-
if (e is ISpecViolation)
270-
{
271-
UnregisterSubscriptionInternal(subscription);
272-
goOn = false;
273-
}
274-
else
275-
throw;
270+
UnregisterSubscriptionInternal(subscription);
271+
goOn = false;
276272
}
277273

278274
if (!goOn)

src/core/Akka.TestKit/Internal/InternalTestActor.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ protected override void OnReceive(object message)
4545
System.Diagnostics.Debug.WriteLine("TestActor received " + message);
4646
}
4747
catch (FormatException)
48+
when (message is LogEvent { Message: LogMessage msg })
4849
{
49-
if (message is LogEvent { Message: LogMessage msg })
50-
System.Diagnostics.Debug.WriteLine(
51-
$"TestActor received a malformed formatted message. Template:[{msg.Format}], args:[{string.Join(",", msg.Unformatted())}]");
52-
else
53-
throw;
50+
System.Diagnostics.Debug.WriteLine(
51+
$"TestActor received a malformed formatted message. Template:[{msg.Format}], args:[{string.Join(",", msg.Unformatted())}]");
5452
}
5553

5654
switch (message)

0 commit comments

Comments
 (0)