Skip to content

Commit

Permalink
Refactor exception handling in NatsException (#677)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtmk authored Nov 13, 2024
1 parent 7dd7525 commit 6b6cce6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/NATS.Client.Core/Internal/HeaderWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ internal long Write(IBufferWriter<byte> bufferWriter, NatsHeaders headers)
var keySpan = bufferWriter.GetSpan(keyLength);
_encoding.GetBytes(kv.Key, keySpan);
if (!ValidateKey(keySpan.Slice(0, keyLength)))
throw new NatsException($"Invalid header key '{kv.Key}': contains colon, space, or other non-printable ASCII characters");
{
NatsException.Throw($"Invalid header key '{kv.Key}': contains colon, space, or other non-printable ASCII characters");
}

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

bufferWriter.Advance(valueLength);
len += valueLength;
Expand Down
5 changes: 5 additions & 0 deletions src/NATS.Client.Core/NatsException.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Runtime.CompilerServices;

namespace NATS.Client.Core;

public class NatsException : Exception
Expand All @@ -11,6 +13,9 @@ public NatsException(string message, Exception exception)
: base(message, exception)
{
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void Throw(string message) => throw new NatsException(message);
}

public sealed class NatsNoReplyException : NatsException
Expand Down

0 comments on commit 6b6cce6

Please sign in to comment.