Skip to content

Commit 6898525

Browse files
authored
improve exception message (Azure#20722)
1 parent 2675518 commit 6898525

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

sdk/formrecognizer/Azure.AI.FormRecognizer/src/FieldValue.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ public string AsString()
222222
throw new InvalidOperationException($"Cannot get field as String. Field value's type is {ValueType}.");
223223
}
224224

225+
// Use when mocking
225226
if (_fieldValue == null)
226227
{
227228
return ValueString;
@@ -242,14 +243,15 @@ public long AsInt64()
242243
throw new InvalidOperationException($"Cannot get field as Integer. Field value's type is {ValueType}.");
243244
}
244245

246+
// Use when mocking
245247
if (_fieldValue == null)
246248
{
247249
return ValueInteger;
248250
}
249251

250252
if (!_fieldValue.ValueInteger.HasValue)
251253
{
252-
throw new InvalidOperationException($"Not able to parse to {nameof(FieldValueType.Int64)} type. Consider using the 'ValueData.Text' property.");
254+
throw new InvalidOperationException($"Value was extracted from the form, but cannot be normalized to {nameof(FieldValueType.Int64)} type. Consider accessing the `ValueData.text` property for a textual representation of the value.");
253255
}
254256

255257
return _fieldValue.ValueInteger.Value;
@@ -267,6 +269,7 @@ public float AsFloat()
267269
throw new InvalidOperationException($"Cannot get field as Float. Field value's type is {ValueType}.");
268270
}
269271

272+
// Use when mocking
270273
if (_fieldValue == null)
271274
{
272275
return ValueNumber;
@@ -281,7 +284,7 @@ public float AsFloat()
281284
}
282285
else
283286
{
284-
throw new InvalidOperationException($"Not able to parse to {nameof(FieldValueType.Float)} type. Consider using the 'ValueData.Text' property.");
287+
throw new InvalidOperationException($"Value was extracted from the form, but cannot be normalized to {nameof(FieldValueType.Float)} type. Consider accessing the `ValueData.text` property for a textual representation of the value.");
285288
}
286289
}
287290

@@ -300,14 +303,15 @@ public DateTime AsDate()
300303
throw new InvalidOperationException($"Cannot get field as Date. Field value's type is {ValueType}.");
301304
}
302305

306+
// Use when mocking
303307
if (_fieldValue == null)
304308
{
305309
return ValueDate;
306310
}
307311

308312
if (!_fieldValue.ValueDate.HasValue)
309313
{
310-
throw new InvalidOperationException($"Not able to parse to {nameof(FieldValueType.Date)} type. Consider using the 'ValueData.Text' property.");
314+
throw new InvalidOperationException($"Value was extracted from the form, but cannot be normalized to {nameof(FieldValueType.Date)} type. Consider accessing the `ValueData.text` property for a textual representation of the value.");
311315
}
312316

313317
return _fieldValue.ValueDate.Value.UtcDateTime;
@@ -325,14 +329,15 @@ public TimeSpan AsTime()
325329
throw new InvalidOperationException($"Cannot get field as Time. Field value's type is {ValueType}.");
326330
}
327331

332+
// Use when mocking
328333
if (_fieldValue == null)
329334
{
330335
return ValueTime;
331336
}
332337

333338
if (!_fieldValue.ValueTime.HasValue)
334339
{
335-
throw new InvalidOperationException($"Not able to parse to {nameof(FieldValueType.Time)} type. Consider using the 'ValueData.Text' property.");
340+
throw new InvalidOperationException($"Value was extracted from the form, but cannot be normalized to {nameof(FieldValueType.Time)} type. Consider accessing the `ValueData.text` property for a textual representation of the value.");
336341
}
337342

338343
return _fieldValue.ValueTime.Value;
@@ -350,6 +355,7 @@ public string AsPhoneNumber()
350355
throw new InvalidOperationException($"Cannot get field as PhoneNumber. Field value's type is {ValueType}.");
351356
}
352357

358+
// Use when mocking
353359
if (_fieldValue == null)
354360
{
355361
return ValueString;
@@ -370,6 +376,7 @@ public IReadOnlyList<FormField> AsList()
370376
throw new InvalidOperationException($"Cannot get field as List. Field value's type is {ValueType}.");
371377
}
372378

379+
// Use when mocking
373380
if (_fieldValue == null)
374381
{
375382
return ValueList;
@@ -396,6 +403,7 @@ public IReadOnlyDictionary<string, FormField> AsDictionary()
396403
throw new InvalidOperationException($"Cannot get field as Dictionary. Field value's type is {ValueType}.");
397404
}
398405

406+
// Use when mocking
399407
if (_fieldValue == null)
400408
{
401409
return ValueDictionary;
@@ -430,14 +438,15 @@ public SelectionMarkState AsSelectionMarkState()
430438
throw new InvalidOperationException($"Cannot get field as SelectionMark. Field value's type is {ValueType}.");
431439
}
432440

441+
// Use when mocking
433442
if (_fieldValue == null)
434443
{
435444
return ValueSelectionMark;
436445
}
437446

438447
if (!_fieldValue.ValueSelectionMark.HasValue)
439448
{
440-
throw new InvalidOperationException($"Not able to parse to {nameof(FieldValueType.SelectionMark)} type. Consider using the 'ValueData.Text' property.");
449+
throw new InvalidOperationException($"Value was extracted from the form, but cannot be normalized to {nameof(FieldValueType.SelectionMark)} type. Consider accessing the `ValueData.text` property for a textual representation of the value.");
441450
}
442451

443452
return _fieldValue.ValueSelectionMark.Value;
@@ -455,6 +464,7 @@ public string AsCountryCode()
455464
throw new InvalidOperationException($"Cannot get field as country code. Field value's type is {ValueType}.");
456465
}
457466

467+
// Use when mocking
458468
if (_fieldValue == null)
459469
{
460470
return ValueString;
@@ -475,14 +485,15 @@ public FieldValueGender AsGender()
475485
throw new InvalidOperationException($"Cannot get field as gender. Field value's type is {ValueType}.");
476486
}
477487

488+
// Use when mocking
478489
if (_fieldValue == null)
479490
{
480491
return ValueGender;
481492
}
482493

483494
if (!_fieldValue.ValueGender.HasValue)
484495
{
485-
throw new InvalidOperationException($"Not able to parse to {nameof(FieldValueType.Gender)} type. Consider using the 'ValueData.Text' property.");
496+
throw new InvalidOperationException($"Value was extracted from the form, but cannot be normalized to {nameof(FieldValueType.Gender)} type. Consider accessing the `ValueData.text` property for a textual representation of the value.");
486497
}
487498

488499
return _fieldValue.ValueGender.Value;

0 commit comments

Comments
 (0)