Skip to content

Commit cc014bd

Browse files
committed
refactor(extensions): 统一异常消息本地化处理
将 ByteExtensions、SpanExtensions、ObjectExtensions 中的硬编码异常消息 替换为 LocalizationService 调用,实现异常消息的国际化支持。 新增本地化键: - BufferTooSmall: 缓冲区空间不足 - OffsetOutsideBufferBounds: 偏移量超出边界(带参数) - OffsetOutsideBufferBoundsSimple: 偏移量超出边界(简单版) - StringTooLong: 字符串过长 - ValueMustBeBetween: 值必须在范围内
1 parent f336745 commit cc014bd

6 files changed

Lines changed: 179 additions & 58 deletions

File tree

GameFrameX.Foundation.Extensions/ByteExtensions.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,8 @@ public static void WriteFloatValue(this byte[] buffer, float value, ref int offs
492492

493493
if (offset + ConstBaseTypeSize.FloatSize > buffer.Length)
494494
{
495-
throw new ArgumentOutOfRangeException(nameof(offset), $"Buffer too small: need {offset + ConstBaseTypeSize.FloatSize} bytes, have {buffer.Length}.");
495+
throw new ArgumentOutOfRangeException(nameof(offset),
496+
LocalizationService.GetString(LocalizationKeys.Exceptions.BufferTooSmall, offset + ConstBaseTypeSize.FloatSize, buffer.Length));
496497
}
497498

498499
BinaryPrimitives.WriteSingleBigEndian(buffer.AsSpan()[offset..], value);
@@ -514,7 +515,8 @@ public static void WriteDoubleValue(this byte[] buffer, double value, ref int of
514515

515516
if (offset + ConstBaseTypeSize.DoubleSize > buffer.Length)
516517
{
517-
throw new ArgumentOutOfRangeException(nameof(offset), $"Buffer too small: need {offset + ConstBaseTypeSize.DoubleSize} bytes, have {buffer.Length}.");
518+
throw new ArgumentOutOfRangeException(nameof(offset),
519+
LocalizationService.GetString(LocalizationKeys.Exceptions.BufferTooSmall, offset + ConstBaseTypeSize.DoubleSize, buffer.Length));
518520
}
519521

520522
BinaryPrimitives.WriteDoubleBigEndian(buffer.AsSpan()[offset..], value);
@@ -538,7 +540,8 @@ public static void WriteBytesValue(this byte[] buffer, byte[] value, ref int off
538540

539541
if (offset + value.Length + ConstBaseTypeSize.IntSize > buffer.Length)
540542
{
541-
throw new ArgumentOutOfRangeException(nameof(offset), $"Buffer too small: need {offset + value.Length + ConstBaseTypeSize.IntSize} bytes, have {buffer.Length}.");
543+
throw new ArgumentOutOfRangeException(nameof(offset),
544+
LocalizationService.GetString(LocalizationKeys.Exceptions.BufferTooSmall, offset + value.Length + ConstBaseTypeSize.IntSize, buffer.Length));
542545
}
543546

544547
buffer.WriteIntValue(value.Length, ref offset);
@@ -584,7 +587,8 @@ public static void WriteSByteValue(this byte[] buffer, sbyte value, ref int offs
584587

585588
if (offset + ConstBaseTypeSize.SbyteSize > buffer.Length)
586589
{
587-
throw new ArgumentOutOfRangeException(nameof(offset), $"Buffer too small: need {offset + ConstBaseTypeSize.SbyteSize} bytes, have {buffer.Length}.");
590+
throw new ArgumentOutOfRangeException(nameof(offset),
591+
LocalizationService.GetString(LocalizationKeys.Exceptions.BufferTooSmall, offset + ConstBaseTypeSize.SbyteSize, buffer.Length));
588592
}
589593

590594
buffer[offset] = (byte)value;
@@ -619,7 +623,8 @@ public static void WriteStringValue(this byte[] buffer, string value, ref int of
619623

620624
if (offset + len + ConstBaseTypeSize.ShortSize > buffer.Length)
621625
{
622-
throw new ArgumentOutOfRangeException(nameof(offset), $"Buffer too small: need {offset + len + ConstBaseTypeSize.ShortSize} bytes, have {buffer.Length}.");
626+
throw new ArgumentOutOfRangeException(nameof(offset),
627+
LocalizationService.GetString(LocalizationKeys.Exceptions.BufferTooSmall, offset + len + ConstBaseTypeSize.ShortSize, buffer.Length));
623628
}
624629

625630
Encoding.UTF8.GetBytes(value, 0, value.Length, buffer, offset + ConstBaseTypeSize.ShortSize);
@@ -642,7 +647,8 @@ public static void WriteBoolValue(this byte[] buffer, bool value, ref int offset
642647

643648
if (offset + ConstBaseTypeSize.BoolSize > buffer.Length)
644649
{
645-
throw new ArgumentOutOfRangeException(nameof(offset), $"Buffer too small: need {offset + ConstBaseTypeSize.BoolSize} bytes, have {buffer.Length}.");
650+
throw new ArgumentOutOfRangeException(nameof(offset),
651+
LocalizationService.GetString(LocalizationKeys.Exceptions.BufferTooSmall, offset + ConstBaseTypeSize.BoolSize, buffer.Length));
646652
}
647653

648654
buffer[offset] = value ? (byte)1 : (byte)0;

GameFrameX.Foundation.Extensions/Localization/Keys.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,55 @@ public static class Exceptions
108108
/// 参数: {0} - 对象类型, {1} - 目标类型
109109
/// </remarks>
110110
public const string ObjectTypeMismatch = "Extensions.Exceptions.ObjectTypeMismatch";
111+
112+
/// <summary>
113+
/// 缓冲区空间不足的错误消息
114+
/// </summary>
115+
/// <remarks>
116+
/// 键名: Extensions.Exceptions.BufferTooSmall
117+
/// 用途: 当缓冲区空间不足以执行写入操作时使用
118+
/// 参数: {0} - 需要的字节数, {1} - 缓冲区长度
119+
/// </remarks>
120+
public const string BufferTooSmall = "Extensions.Exceptions.BufferTooSmall";
121+
122+
/// <summary>
123+
/// 偏移量超出缓冲区边界的错误消息
124+
/// </summary>
125+
/// <remarks>
126+
/// 键名: Extensions.Exceptions.OffsetOutsideBufferBounds
127+
/// 用途: 当读取操作的偏移量超出缓冲区边界时使用
128+
/// 参数: {0} - 偏移量, {1} - 需要的字节数, {2} - 缓冲区长度
129+
/// </remarks>
130+
public const string OffsetOutsideBufferBounds = "Extensions.Exceptions.OffsetOutsideBufferBounds";
131+
132+
/// <summary>
133+
/// 偏移量超出缓冲区边界的简单错误消息(无参数)
134+
/// </summary>
135+
/// <remarks>
136+
/// 键名: Extensions.Exceptions.OffsetOutsideBufferBoundsSimple
137+
/// 用途: 当读取操作的偏移量超出缓冲区边界时使用(无详细参数)
138+
/// </remarks>
139+
public const string OffsetOutsideBufferBoundsSimple = "Extensions.Exceptions.OffsetOutsideBufferBoundsSimple";
140+
141+
/// <summary>
142+
/// 字符串过长的错误消息
143+
/// </summary>
144+
/// <remarks>
145+
/// 键名: Extensions.Exceptions.StringTooLong
146+
/// 用途: 当字符串长度超过最大允许值时使用
147+
/// 参数: {0} - 最大允许长度
148+
/// </remarks>
149+
public const string StringTooLong = "Extensions.Exceptions.StringTooLong";
150+
151+
/// <summary>
152+
/// 值超出范围的错误消息
153+
/// </summary>
154+
/// <remarks>
155+
/// 键名: Extensions.Exceptions.ValueMustBeBetween
156+
/// 用途: 当值不在指定的最小值和最大值之间时使用
157+
/// 参数: {0} - 实际值, {1} - 最小值, {2} - 最大值
158+
/// </remarks>
159+
public const string ValueMustBeBetween = "Extensions.Exceptions.ValueMustBeBetween";
111160
}
112161

113162
/// <summary>

GameFrameX.Foundation.Extensions/Localization/Messages/Resources.resx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@
3333
<value>Object type mismatch. Object type: {0}, target type: {1}.</value>
3434
</data>
3535

36+
<data name="Extensions.Exceptions.BufferTooSmall" xml:space="preserve">
37+
<value>Buffer too small: need {0} bytes, have {1}.</value>
38+
</data>
39+
40+
<data name="Extensions.Exceptions.OffsetOutsideBufferBounds" xml:space="preserve">
41+
<value>Offset is outside the bounds of the buffer. Offset: {0}, Required: {1}, Available: {2}.</value>
42+
</data>
43+
44+
<data name="Extensions.Exceptions.OffsetOutsideBufferBoundsSimple" xml:space="preserve">
45+
<value>Offset is outside the bounds of the buffer.</value>
46+
</data>
47+
48+
<data name="Extensions.Exceptions.StringTooLong" xml:space="preserve">
49+
<value>String is too long. Maximum length is {0} bytes.</value>
50+
</data>
51+
52+
<data name="Extensions.Exceptions.ValueMustBeBetween" xml:space="preserve">
53+
<value>Value {0} must be between {1} and {2} (inclusive).</value>
54+
</data>
55+
3656
<!-- Log messages -->
3757
<data name="Extensions.Logs.CollectionProcessed" xml:space="preserve">
3858
<value>Collection processing completed. Collection type: {0}, element count: {1}.</value>

GameFrameX.Foundation.Extensions/Localization/Messages/Resources.zh-CN.resx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@
3333
<value>对象类型不匹配。对象类型:{0},目标类型:{1}。</value>
3434
</data>
3535

36+
<data name="Extensions.Exceptions.BufferTooSmall" xml:space="preserve">
37+
<value>缓冲区空间不足:需要 {0} 字节,当前 {1} 字节。</value>
38+
</data>
39+
40+
<data name="Extensions.Exceptions.OffsetOutsideBufferBounds" xml:space="preserve">
41+
<value>偏移量超出缓冲区边界。偏移量:{0},需要:{1},可用:{2}。</value>
42+
</data>
43+
44+
<data name="Extensions.Exceptions.OffsetOutsideBufferBoundsSimple" xml:space="preserve">
45+
<value>偏移量超出缓冲区边界。</value>
46+
</data>
47+
48+
<data name="Extensions.Exceptions.StringTooLong" xml:space="preserve">
49+
<value>字符串过长。最大长度为 {0} 字节。</value>
50+
</data>
51+
52+
<data name="Extensions.Exceptions.ValueMustBeBetween" xml:space="preserve">
53+
<value>值 {0} 必须在 {1} 和 {2} 之间(包含边界)。</value>
54+
</data>
55+
3656
<!-- 日志消息 -->
3757
<data name="Extensions.Logs.CollectionProcessed" xml:space="preserve">
3858
<value>集合处理完成。集合类型:{0},元素数量:{1}。</value>

GameFrameX.Foundation.Extensions/ObjectExtensions.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// GameFrameX 组织下的以及组织衍生的项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
2-
//
2+
//
33
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE 文件。
4-
//
4+
//
55
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
66

7+
using GameFrameX.Foundation.Localization.Core;
8+
using GameFrameX.Foundation.Extensions.Localization;
9+
710
namespace GameFrameX.Foundation.Extensions;
811

912
/// <summary>
@@ -56,7 +59,7 @@ public static void CheckRange(this int value, int minValue = 0, int maxValue = i
5659
ArgumentOutOfRangeException.ThrowIfGreaterThan(minValue, maxValue, nameof(minValue));
5760
if (value < minValue || value > maxValue)
5861
{
59-
throw new ArgumentOutOfRangeException(nameof(value), $"Value {value} must be between {minValue} and {maxValue} (inclusive).");
62+
throw new ArgumentOutOfRangeException(nameof(value), LocalizationService.GetString(LocalizationKeys.Exceptions.ValueMustBeBetween, value, minValue, maxValue));
6063
}
6164
}
6265

@@ -98,7 +101,7 @@ public static void CheckRange(this uint value, uint minValue = 0, uint maxValue
98101
ArgumentOutOfRangeException.ThrowIfGreaterThan(minValue, maxValue, nameof(minValue));
99102
if (value < minValue || value > maxValue)
100103
{
101-
throw new ArgumentOutOfRangeException(nameof(value), $"Value {value} must be between {minValue} and {maxValue} (inclusive).");
104+
throw new ArgumentOutOfRangeException(nameof(value), LocalizationService.GetString(LocalizationKeys.Exceptions.ValueMustBeBetween, value, minValue, maxValue));
102105
}
103106
}
104107

@@ -127,7 +130,7 @@ public static void CheckRange(this long value, long minValue = 0, long maxValue
127130
ArgumentOutOfRangeException.ThrowIfGreaterThan(minValue, maxValue, nameof(minValue));
128131
if (value < minValue || value > maxValue)
129132
{
130-
throw new ArgumentOutOfRangeException(nameof(value), $"Value {value} must be between {minValue} and {maxValue} (inclusive).");
133+
throw new ArgumentOutOfRangeException(nameof(value), LocalizationService.GetString(LocalizationKeys.Exceptions.ValueMustBeBetween, value, minValue, maxValue));
131134
}
132135
}
133136

@@ -156,7 +159,7 @@ public static void CheckRange(this ulong value, ulong minValue = 0, ulong maxVal
156159
ArgumentOutOfRangeException.ThrowIfGreaterThan(minValue, maxValue, nameof(minValue));
157160
if (value < minValue || value > maxValue)
158161
{
159-
throw new ArgumentOutOfRangeException(nameof(value), $"Value {value} must be between {minValue} and {maxValue} (inclusive).");
162+
throw new ArgumentOutOfRangeException(nameof(value), LocalizationService.GetString(LocalizationKeys.Exceptions.ValueMustBeBetween, value, minValue, maxValue));
160163
}
161164
}
162165

@@ -185,7 +188,7 @@ public static void CheckRange(this short value, short minValue = 0, short maxVal
185188
ArgumentOutOfRangeException.ThrowIfGreaterThan(minValue, maxValue, nameof(minValue));
186189
if (value < minValue || value > maxValue)
187190
{
188-
throw new ArgumentOutOfRangeException(nameof(value), $"Value {value} must be between {minValue} and {maxValue} (inclusive).");
191+
throw new ArgumentOutOfRangeException(nameof(value), LocalizationService.GetString(LocalizationKeys.Exceptions.ValueMustBeBetween, value, minValue, maxValue));
189192
}
190193
}
191194

@@ -214,7 +217,7 @@ public static void CheckRange(this ushort value, ushort minValue = 0, ushort max
214217
ArgumentOutOfRangeException.ThrowIfGreaterThan(minValue, maxValue, nameof(minValue));
215218
if (value < minValue || value > maxValue)
216219
{
217-
throw new ArgumentOutOfRangeException(nameof(value), $"Value {value} must be between {minValue} and {maxValue} (inclusive).");
220+
throw new ArgumentOutOfRangeException(nameof(value), LocalizationService.GetString(LocalizationKeys.Exceptions.ValueMustBeBetween, value, minValue, maxValue));
218221
}
219222
}
220223
}

0 commit comments

Comments
 (0)