Error handling & literal types
Part of the OhData.Client guide.
Error handling
Non-2xx responses throw ODataClientException, which parses the OData error envelope:
try
{
await client.For<Product>().Key(99999).GetAsync();
}
catch (ODataClientException ex) when (ex.StatusCode == 404)
{
Console.WriteLine(ex.ODataErrorMessage); // "Widget with key '99999' was not found."
}
catch (ODataClientException ex)
{
Console.WriteLine($"HTTP {ex.StatusCode}: [{ex.ODataErrorCode}] {ex.ODataErrorMessage}");
}
ODataClientException properties:
StatusCode— the HTTP status code as anint(e.g.404,412)ODataErrorCode— the"code"field from the OData error body, or an empty string if the response was not an OData error envelopeODataErrorMessage— the"message"field, or the raw response body if not a valid OData error
Literal type support
The filter translator and key formatter handle these CLR types:
| CLR type | OData literal | Example |
|---|---|---|
string |
'value' (single-quoted, ' → '') |
Name eq 'it''s' |
int, long, short |
unquoted decimal | Id eq 42 |
decimal, float, double |
invariant-culture decimal | Price gt 4.99 |
bool |
true / false |
IsActive eq true |
Guid |
36-char hex | Id eq 3f2504e0-... |
DateTime / DateTimeOffset |
ISO 8601, always with Z or an explicit offset |
CreatedAt gt 2024-01-01T00:00:00Z |
DateOnly |
'yyyy-MM-dd' |
Date eq 2024-06-15 |
TimeOnly |
'HH:mm:ss' |
Time eq 09:30:00 |
Enum |
quoted member name | Status eq 'Active' |
DateTime kind semantics. OData Edm.DateTimeOffset literals always require a Z or an
explicit numeric offset, so the client never emits an offset-less value:
DateTimeKind.Utc is emitted as-is with Z; DateTimeKind.Local (e.g. DateTime.Now) is
converted to its UTC instant via ToUniversalTime(); DateTimeKind.Unspecified is treated as
UTC. If your Unspecified values represent local wall-clock time, convert them yourself (or
use DateTimeOffset, which always carries its own offset and passes through unchanged).
Derived types
When a read targets a base type and the server sends @odata.type on a derived row (a table-per-hierarchy
entity set, for example), the client materializes that row as the derived CLR type, so its own members are
kept:
List<Award> awards = await client.For<Award>("Awards").ToListAsync();
// awards[0] is an AcademyAward with Ceremony and IsWinner populated
This applies to every read: collections, single entities, ToAnnotatedPageAsync, GetPropertyAsync, write echoes, and rows
nested under $expand, at any depth of a self-referencing hierarchy. @odata.type (or the OData 4.01 short
form @type) may sit anywhere in the object.
No configuration by default. A subclass of the declared type in the declared type's assembly is matched by namespace plus simple name, which is the server's default EDM naming (even for a nested class), or by its CLR full name. The leading
#is optional.Renamed namespaces and generic subclasses. If the EDM type name is not one of those, or the subclass is generic, register it. Register a closed instantiation of a generic type; abstract and open generic types are rejected.
var options = new OhDataClientOptions(); options.DerivedTypes.Add<AcademyAward>("My.Ns.AcademyAward");Registrations are read once, when the client is constructed; adding one afterwards has no effect.
Unknown or absent
@odata.typereads as the declared type.Types with their own JSON handling (
[JsonConverter]or[JsonPolymorphic]) are left to it.Reads only. Request bodies are serialized as the declared type and carry no
@odata.type.A custom
JsonOptionsis honoured as before and the instance you pass is never modified.Types with no derived types are unaffected.
Limits.
ReferenceHandler.Preserve($id/$ref) fails with aJsonExceptionwhen a$refcrosses a polymorphic row's own object, and a JSON path in a deserialization error is relative to the row.