Skip to content

Commit 6b6cce6

Browse files
authored
Refactor exception handling in NatsException (#677)
1 parent 7dd7525 commit 6b6cce6

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/NATS.Client.Core/Internal/HeaderWriter.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ internal long Write(IBufferWriter<byte> bufferWriter, NatsHeaders headers)
6767
var keySpan = bufferWriter.GetSpan(keyLength);
6868
_encoding.GetBytes(kv.Key, keySpan);
6969
if (!ValidateKey(keySpan.Slice(0, keyLength)))
70-
throw new NatsException($"Invalid header key '{kv.Key}': contains colon, space, or other non-printable ASCII characters");
70+
{
71+
NatsException.Throw($"Invalid header key '{kv.Key}': contains colon, space, or other non-printable ASCII characters");
72+
}
7173

7274
bufferWriter.Advance(keyLength);
7375
len += keyLength;
@@ -80,7 +82,9 @@ internal long Write(IBufferWriter<byte> bufferWriter, NatsHeaders headers)
8082
var valueSpan = bufferWriter.GetSpan(valueLength);
8183
_encoding.GetBytes(value, valueSpan);
8284
if (!ValidateValue(valueSpan.Slice(0, valueLength)))
83-
throw new NatsException($"Invalid header value for key '{kv.Key}': contains CRLF");
85+
{
86+
NatsException.Throw($"Invalid header value for key '{kv.Key}': contains CRLF");
87+
}
8488

8589
bufferWriter.Advance(valueLength);
8690
len += valueLength;

src/NATS.Client.Core/NatsException.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Runtime.CompilerServices;
2+
13
namespace NATS.Client.Core;
24

35
public class NatsException : Exception
@@ -11,6 +13,9 @@ public NatsException(string message, Exception exception)
1113
: base(message, exception)
1214
{
1315
}
16+
17+
[MethodImpl(MethodImplOptions.NoInlining)]
18+
public static void Throw(string message) => throw new NatsException(message);
1419
}
1520

1621
public sealed class NatsNoReplyException : NatsException

0 commit comments

Comments
 (0)