failure static method
Reads a failure sub-map.
An unrecognised kind becomes EcrMalformed rather than a guess. A future native version that adds a failure this one has not heard of will be reported as "outcome unknown", which is the safe reading.
Implementation
static EcrFailure failure(Object? payload) {
final Map<Object?, Object?>? map = _asMap(payload);
if (map == null) {
return EcrMalformed(_notAMap(payload, 'a failure'));
}
final String message = _string(map, EcrFailureKeys.message);
return switch (_string(map, EcrFailureKeys.kind)) {
EcrFailureKinds.unreachable => EcrUnreachable(message),
EcrFailureKinds.timeout => EcrTimeout(message),
EcrFailureKinds.malformed => EcrMalformed(message),
EcrFailureKinds.connectionLost => EcrConnectionLost(message),
EcrFailureKinds.unauthenticated => EcrUnauthenticated(message),
EcrFailureKinds.cancelled =>
message.isEmpty ? const EcrCancelled() : EcrCancelled(message),
EcrFailureKinds.unsupported => EcrUnsupported(message),
final String unknown => EcrMalformed(
'The host reported a failure this version does not know '
'("$unknown"): ${message.isEmpty ? 'no message given' : message}',
),
};
}