Skip to content

Commit 0c2cfbb

Browse files
author
Stefán J. Sigurðarson
committed
Re-adding throw helpers to help with inlining.
1 parent 9c7fc8b commit 0c2cfbb

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

projects/RabbitMQ.Client/client/impl/WireFormatting.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ public static int ReadLongstr(ReadOnlySpan<byte> span, out byte[] value)
148148

149149
if (byteCount > int.MaxValue)
150150
{
151-
throw new SyntaxErrorException($"Long string too long; byte length={byteCount}, max={int.MaxValue}");
151+
value = null;
152+
return ThrowSyntaxErrorException(byteCount);
152153
}
153154

154155
value = span.Slice(4, (int)byteCount).ToArray();
@@ -183,7 +184,8 @@ public static int ReadShortstr(ReadOnlySpan<byte> span, out string value)
183184
#endif
184185
}
185186

186-
throw new ArgumentOutOfRangeException("span", $"Span has not enough space ({span.Length} instead of {byteCount + 1})");
187+
value = null;
188+
return ThrowArgumentOutOfRangeException(span.Length, byteCount + 1);
187189
}
188190

189191
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -737,9 +739,9 @@ public static int WriteShortstr(Span<byte> span, string val)
737739
span[0] = (byte)bytesWritten;
738740
return bytesWritten + 1;
739741
}
740-
catch (ArgumentException e)
742+
catch (ArgumentException)
741743
{
742-
throw new ArgumentOutOfRangeException($"Value exceeds the maximum allowed length of {maxLength} bytes.", e);
744+
return ThrowArgumentOutOfRangeException(nameof(val), val, maxLength);
743745
}
744746
#else
745747
unsafe
@@ -753,9 +755,9 @@ public static int WriteShortstr(Span<byte> span, string val)
753755
span[0] = (byte)bytesWritten;
754756
return bytesWritten + 1;
755757
}
756-
catch (ArgumentException e)
758+
catch (ArgumentException)
757759
{
758-
throw new ArgumentOutOfRangeException($"Value exceeds the maximum allowed length of {maxLength} bytes.", e);
760+
return ThrowArgumentOutOfRangeException(nameof(val), val, maxLength);
759761
}
760762
}
761763
}
@@ -894,5 +896,20 @@ public static int WriteTimestamp(Span<byte> span, AmqpTimestamp val)
894896
// See also MethodArgumentReader.ReadTimestamp and AmqpTimestamp itself
895897
return WriteLonglong(span, (ulong)val.UnixTime);
896898
}
899+
900+
public static int ThrowArgumentOutOfRangeException(int orig, int expected)
901+
{
902+
throw new ArgumentOutOfRangeException("span", $"Span has not enough space ({orig} instead of {expected})");
903+
}
904+
905+
public static int ThrowArgumentOutOfRangeException(string paramName, string val, int maxLength)
906+
{
907+
throw new ArgumentOutOfRangeException(paramName, val, $"Value exceeds the maximum allowed length of {maxLength} bytes.");
908+
}
909+
910+
public static int ThrowSyntaxErrorException(uint byteCount)
911+
{
912+
throw new SyntaxErrorException($"Long string too long; byte length={byteCount}, max={int.MaxValue}");
913+
}
897914
}
898915
}

0 commit comments

Comments
 (0)