@@ -213,7 +213,7 @@ class _InternetAddress implements InternetAddress {
213
213
_InternetAddress (this .type, this .address, this ._host, this ._in_addr,
214
214
[this ._scope_id = 0 ]);
215
215
216
- factory _InternetAddress . fromString (String address,
216
+ static Object _parseAddressString (String address,
217
217
{InternetAddressType ? type}) {
218
218
// TODO(40614): Remove once non-nullability is sound.
219
219
ArgumentError .checkNotNull (address, 'address' );
@@ -231,14 +231,14 @@ class _InternetAddress implements InternetAddress {
231
231
}
232
232
var inAddr = _parse (address);
233
233
if (inAddr == null ) {
234
- throw ArgumentError ('Invalid internet address $address ' );
234
+ return ArgumentError ('Invalid internet address $address ' );
235
235
}
236
236
InternetAddressType type = inAddr.length == _IPv4AddrLength
237
237
? InternetAddressType .IPv4
238
238
: InternetAddressType .IPv6 ;
239
239
if (scopeID != null && scopeID.length > 0 ) {
240
240
if (type != InternetAddressType .IPv6 ) {
241
- throw ArgumentError .value (
241
+ return ArgumentError .value (
242
242
address, 'address' , 'IPv4 addresses cannot have a scope ID' );
243
243
}
244
244
@@ -248,14 +248,36 @@ class _InternetAddress implements InternetAddress {
248
248
return _InternetAddress (
249
249
InternetAddressType .IPv6 , originalAddress, null , inAddr, scopeID);
250
250
} else {
251
- throw ArgumentError .value (
251
+ return ArgumentError .value (
252
252
address, 'address' , 'Invalid IPv6 address with scope ID' );
253
253
}
254
254
}
255
255
return _InternetAddress (type, originalAddress, null , inAddr, 0 );
256
256
}
257
257
}
258
258
259
+ factory _InternetAddress .fromString (String address,
260
+ {InternetAddressType ? type}) {
261
+ final parsedAddress = _parseAddressString (address, type: type);
262
+ if (parsedAddress is _InternetAddress ) {
263
+ return parsedAddress;
264
+ } else {
265
+ assert (parsedAddress is ArgumentError );
266
+ throw parsedAddress;
267
+ }
268
+ }
269
+
270
+ static _InternetAddress ? tryParse (String address) {
271
+ checkNotNullable (address, "address" );
272
+ final parsedAddress = _parseAddressString (address);
273
+ if (parsedAddress is _InternetAddress ) {
274
+ return parsedAddress;
275
+ } else {
276
+ assert (parsedAddress is ArgumentError );
277
+ return null ;
278
+ }
279
+ }
280
+
259
281
factory _InternetAddress .fromRawAddress (Uint8List rawAddress,
260
282
{InternetAddressType ? type}) {
261
283
if (type == InternetAddressType .unix) {
@@ -279,15 +301,6 @@ class _InternetAddress implements InternetAddress {
279
301
}
280
302
}
281
303
282
- static _InternetAddress ? tryParse (String address) {
283
- checkNotNullable (address, "address" );
284
- try {
285
- return _InternetAddress .fromString (address);
286
- } on ArgumentError catch (_) {
287
- return null ;
288
- }
289
- }
290
-
291
304
factory _InternetAddress .fixed (int id) {
292
305
switch (id) {
293
306
case _addressLoopbackIPv4:
0 commit comments