Skip to content

Commit 312ed85

Browse files
fix(client): throw api enum errors as invalid data exception
1 parent 41e7cb0 commit 312ed85

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/Orb/Core/ApiEnum.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,43 @@ public record class ApiEnum<TRaw, TEnum>(JsonElement Json)
3636
/// <see cref="Json"/> to access the raw value.
3737
/// </exception>
3838
/// </summary>
39-
public TRaw Raw() =>
40-
JsonSerializer.Deserialize<TRaw>(this.Json, ModelBase.SerializerOptions)
41-
?? throw new OrbInvalidDataException(
42-
string.Format("{0} cannot be null", nameof(this.Json))
43-
);
39+
public TRaw Raw()
40+
{
41+
try
42+
{
43+
return JsonSerializer.Deserialize<TRaw>(this.Json, ModelBase.SerializerOptions)
44+
?? throw new OrbInvalidDataException($"{nameof(this.Json)} cannot be null");
45+
}
46+
catch (JsonException e)
47+
{
48+
throw new OrbInvalidDataException(
49+
$"{this.Json} must be of type {typeof(TRaw).FullName}",
50+
e
51+
);
52+
}
53+
}
4454

4555
/// <summary>
4656
/// Returns an enum member corresponding to this instance's value, or <c>(TEnum)(-1)</c> if the
4757
/// class was instantiated with an unknown value.
4858
///
4959
/// <para>Use <see cref="Raw"/> to access the raw <typeparamref name="TRaw"/> value.</para>.
5060
/// </summary>
51-
public TEnum Value() =>
52-
JsonSerializer.Deserialize<TEnum>(this.Json, ModelBase.SerializerOptions);
61+
public TEnum Value()
62+
{
63+
try
64+
{
65+
return JsonSerializer.Deserialize<TEnum?>(this.Json, ModelBase.SerializerOptions)
66+
?? throw new OrbInvalidDataException($"{nameof(this.Json)} cannot be null");
67+
}
68+
catch (JsonException e)
69+
{
70+
throw new OrbInvalidDataException(
71+
$"{this.Json} must be of type {typeof(TRaw).FullName}",
72+
e
73+
);
74+
}
75+
}
5376

5477
/// <summary>
5578
/// Verifies that this instance's raw value is a member of <typeparamref name="TEnum"/>.

0 commit comments

Comments
 (0)