Skip to main content

Module error

Module error 

Source
Expand description

Typed recovery over the error chain.

ErrorChainExt answers a few questions about any error without the caller knowing its concrete type, so it need not walk std::error::Error::source itself nor learn that three different types can carry a server rejection. It is a read-only view with a blanket impl: a domain error added later answers the same questions without implementing anything, and no new error type or parallel hierarchy exists.

use whatsapp_rust::ErrorChainExt;

if let Some(rejection) = err.server_rejection() {
    eprintln!("server said {}: {}", rejection.code, rejection.text);
} else if err.is_transport_unavailable() {
    eprintln!("offline, will retry");
}

From an anyhow::Error, annotate the cast: it carries two AsRef<dyn Error> impls and both are covered here.

let cause: &(dyn std::error::Error + 'static) = err.as_ref();
let _ = cause.server_rejection();

§Scope

Only questions the crate already answers internally are exposed. There is deliberately no “invalid input”, “protocol violation” or “internal” query: each domain spells those as its own InvalidRequest(String)-style variant with no shared representation, so any such split would be invented here rather than recovered. crate::features::MexError::ExtensionError is likewise not reported as a server rejection: its code is a GraphQL extension code, a different space from the IQ code attribute, and merging the two would make the number meaningless.

An HTTP status is kept apart from an IQ code for the same reason, but it is recoverable — under its own question, ErrorChainExt::http_status. The media paths refuse a CDN or upload host on a status the caller often has to act on differently from a stanza rejection, so the fact is modelled; what is avoided is one accessor that answers for both layers and leaves the caller unable to tell which refused.

§Rendering

A wrapping variant renders exactly what it wraps, so each error’s own Display is unchanged. A caller that concatenates the whole chain will see consecutive nodes repeat the same sentence, which is the price of keeping the wrapped error downcastable. Print the innermost cause, or collapse equal neighbours, rather than joining every node.

Structs§

ServerRejection
A rejection the server sent in response to a request.
Sources
Iterator over an error and everything reachable from its source.

Traits§

ErrorChainExt
Answers a few questions about any error without knowing its concrete type.