Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 8e7b6fc

Browse files
committed
Fix exception messages
1 parent 3317501 commit 8e7b6fc

File tree

1 file changed

+84
-26
lines changed

1 file changed

+84
-26
lines changed

src/ServiceStack.Text/Support/StringSegmentExtensions.cs

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace ServiceStack.Text.Support
1010
{
1111
public static class StringSegmentExtensions
1212
{
13-
const string BadFormat = "Input string was not in a correct format";
13+
const string BadFormat = "Input string was not in a correct format.";
14+
const string OverflowMessage = "Value was either too large or too small for an {0}.";
1415

1516
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1617
public static bool IsNullOrEmpty(this StringSegment value)
@@ -119,62 +120,119 @@ enum ParseState
119120
TrailingWhite
120121
}
121122

123+
private static Exception GetOverflowException(Type type) => new OverflowException(String.Format(OverflowMessage, type.Name));
124+
122125
public static sbyte ParseSByte(this StringSegment value)
123126
{
124-
return JsConfig.UseSystemParseMethods
125-
? sbyte.Parse(value.Value, CultureInfo.InvariantCulture)
126-
: (sbyte) ParseSignedInteger(value, sbyte.MaxValue, sbyte.MinValue);
127+
try
128+
{
129+
return JsConfig.UseSystemParseMethods
130+
? sbyte.Parse(value.Value, CultureInfo.InvariantCulture)
131+
: (sbyte) ParseSignedInteger(value, sbyte.MaxValue, sbyte.MinValue);
132+
}
133+
catch (OverflowException)
134+
{
135+
throw GetOverflowException(typeof(byte));
136+
}
127137
}
128138

129139
public static byte ParseByte(this StringSegment value)
130140
{
131-
return JsConfig.UseSystemParseMethods
132-
? byte.Parse(value.Value, CultureInfo.InvariantCulture)
133-
: (byte) ParseUnsignedInteger(value, byte.MaxValue);
141+
try
142+
{
143+
return JsConfig.UseSystemParseMethods
144+
? byte.Parse(value.Value, CultureInfo.InvariantCulture)
145+
: (byte) ParseUnsignedInteger(value, byte.MaxValue);
146+
}
147+
catch (OverflowException)
148+
{
149+
throw GetOverflowException(typeof(byte));
150+
}
134151
}
135152

136153
public static short ParseInt16(this StringSegment value)
137154
{
138-
return JsConfig.UseSystemParseMethods
139-
? short.Parse(value.Value, CultureInfo.InvariantCulture)
140-
: (short) ParseSignedInteger(value, short.MaxValue, short.MinValue);
155+
try
156+
{
157+
return JsConfig.UseSystemParseMethods
158+
? short.Parse(value.Value, CultureInfo.InvariantCulture)
159+
: (short) ParseSignedInteger(value, short.MaxValue, short.MinValue);
160+
}
161+
catch (OverflowException)
162+
{
163+
throw GetOverflowException(typeof(Int16));
164+
}
141165
}
142166

143167
public static ushort ParseUInt16(this StringSegment value)
144168
{
145-
return JsConfig.UseSystemParseMethods
146-
? ushort.Parse(value.Value, CultureInfo.InvariantCulture)
147-
: (ushort) ParseUnsignedInteger(value, ushort.MaxValue);
169+
try
170+
{
171+
return JsConfig.UseSystemParseMethods
172+
? ushort.Parse(value.Value, CultureInfo.InvariantCulture)
173+
: (ushort) ParseUnsignedInteger(value, ushort.MaxValue);
174+
}
175+
catch (OverflowException)
176+
{
177+
throw GetOverflowException(typeof(UInt16));
178+
}
148179
}
149180

150181

151182
public static int ParseInt32(this StringSegment value)
152183
{
153-
return JsConfig.UseSystemParseMethods
154-
? int.Parse(value.Value, CultureInfo.InvariantCulture)
155-
: (int) ParseSignedInteger(value, Int32.MaxValue, Int32.MinValue);
184+
try
185+
{
186+
return JsConfig.UseSystemParseMethods
187+
? int.Parse(value.Value, CultureInfo.InvariantCulture)
188+
: (int) ParseSignedInteger(value, Int32.MaxValue, Int32.MinValue);
189+
}
190+
catch (OverflowException)
191+
{
192+
throw GetOverflowException(typeof(Int32));
193+
}
156194
}
157195

158196
public static uint ParseUInt32(this StringSegment value)
159197
{
160-
return JsConfig.UseSystemParseMethods
161-
? uint.Parse(value.Value, CultureInfo.InvariantCulture)
162-
: (uint) ParseUnsignedInteger(value, UInt32.MaxValue);
198+
try
199+
{
200+
return JsConfig.UseSystemParseMethods
201+
? uint.Parse(value.Value, CultureInfo.InvariantCulture)
202+
: (uint) ParseUnsignedInteger(value, UInt32.MaxValue);
203+
}
204+
catch (OverflowException)
205+
{
206+
throw GetOverflowException(typeof(UInt32));
207+
}
163208
}
164209

165210
public static long ParseInt64(this StringSegment value)
166211
{
167-
return JsConfig.UseSystemParseMethods
168-
? long.Parse(value.Value, CultureInfo.InvariantCulture)
169-
: ParseSignedInteger(value, Int64.MaxValue, Int64.MinValue);
170-
212+
try
213+
{
214+
return JsConfig.UseSystemParseMethods
215+
? long.Parse(value.Value, CultureInfo.InvariantCulture)
216+
: ParseSignedInteger(value, Int64.MaxValue, Int64.MinValue);
217+
}
218+
catch (OverflowException)
219+
{
220+
throw GetOverflowException(typeof(Int64));
221+
}
171222
}
172223

173224
public static ulong ParseUInt64(this StringSegment value)
174225
{
175-
return JsConfig.UseSystemParseMethods
176-
? ulong.Parse(value.Value, CultureInfo.InvariantCulture)
177-
: ParseUnsignedInteger(value, UInt64.MaxValue);
226+
try
227+
{
228+
return JsConfig.UseSystemParseMethods
229+
? ulong.Parse(value.Value, CultureInfo.InvariantCulture)
230+
: ParseUnsignedInteger(value, UInt64.MaxValue);
231+
}
232+
catch (OverflowException)
233+
{
234+
throw GetOverflowException(typeof(UInt64));
235+
}
178236
}
179237

180238
private static ulong ParseUnsignedInteger(StringSegment value, ulong maxValue)

0 commit comments

Comments
 (0)