Skip to main content
All async Rust SDK calls return wacht::Result<T> (Result<T, wacht::Error>). The error type is designed to separate:
  • transport/runtime failures (Request, Json, Io)
  • backend HTTP failures (Api { status, message, details })
  • SDK usage/config failures (Config, Auth, InvalidRequest, Uninitialized)

Error Variants

use wacht::Error;

fn classify(err: &Error) -> &'static str {
    match err {
        Error::Api { .. } => "backend_api_error",
        Error::Request(_) => "transport_error",
        Error::Json(_) | Error::Io(_) => "runtime_error",
        Error::Config(_) | Error::Auth(_) | Error::InvalidRequest(_) | Error::Uninitialized(_) => {
            "sdk_usage_error"
        }
    }
}

Inspecting Status Codes

Error::status_code() returns a reqwest::StatusCode only for Error::Api.
use reqwest::StatusCode;
use wacht::{Error, WachtClient};

async fn fetch_user_or_none(client: &WachtClient, user_id: &str) -> Result<Option<wacht::models::UserDetails>, Error> {
    match client.users().fetch_user_details(user_id).send().await {
        Ok(user) => Ok(Some(user)),
        Err(err) => match err.status_code() {
            Some(StatusCode::NOT_FOUND) => Ok(None),
            _ => Err(err),
        },
    }
}

Retry-Aware Handling

Use Error::is_retryable() to decide if retry logic should run.
use std::time::Duration;
use tokio::time::sleep;
use wacht::{Error, WachtClient};

async fn fetch_health_with_retry(client: &WachtClient) -> Result<wacht::models::HealthStatus, Error> {
    let mut attempt = 0;

    loop {
        match client.health().check_health().send().await {
            Ok(v) => return Ok(v),
            Err(err) if err.is_retryable() && attempt < 3 => {
                attempt += 1;
                sleep(Duration::from_millis(250 * attempt)).await;
            }
            Err(err) => return Err(err),
        }
    }
}

Authentication-Aware Handling

Use Error::is_authentication_error() to branch on authn/authz failures (401, 403, or Error::Auth).
use wacht::{Error, WachtClient};

async fn guarded_call(client: &WachtClient) -> Result<(), Error> {
    match client.organizations().fetch_organizations().send().await {
        Ok(_) => Ok(()),
        Err(err) if err.is_authentication_error() => {
            eprintln!("Authentication/authorization failed: {err}");
            Err(err)
        }
        Err(err) => Err(err),
    }
}

SDK Initialization Errors

If you use global helpers without calling init(...), the SDK returns Error::Uninitialized.
use wacht::{try_get_client, Error};

fn get_client_checked() -> Result<reqwest::Client, Error> {
    try_get_client()
}

Notes

  • Error::Api includes parsed JSON in details when response body is valid JSON.
  • Internal SDK API failures are normalized through Error::api_from_text(...) to keep message and details formatting consistent.
  • Prefer checking helper methods (status_code, is_retryable, is_authentication_error) over ad-hoc string parsing.