pdns-cli 0.0.2

Rust client library and CLI for the PowerDNS Authoritative Server API
Documentation
//! The Err type

use serde::Deserialize;

// the #[error("")] is used for generating Display implementations
// the #[from] <type> is used for From implementations that support converting the error to [`Error`]

/// The error type. This is an enum describing all possible errors returnable.
/// Unless otherwise specified, assume that a public/exported function returning this enum can return any of its variants.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// error propagation from the [`reqwest`] crate
    #[error("HTTP request failed or response JSON parsing errors")]
    Http(#[from] reqwest::Error),

    /// url parsing error
    #[error("invalid URL")]
    Url(#[from] url::ParseError),

    /// invalid api key for [`reqwest::header`]
    #[error("invalid api key format. could not be inserted into a header")]
    BadApiKey(#[from] reqwest::header::InvalidHeaderValue),

    /// invalid base url
    #[error("base url is invalid")]
    InvalidBaseUrl(String),

    /// error returned by the `PowerDNS` API.
    #[error(transparent)] // done to forward the display format of [`PdnsAPIError`]
    APIError(#[from] PdnsAPIError),
}

// source: <https://doc.powerdns.com/authoritative/http-api/server.html#get--servers-status-codes>
/// `PowerDNS` API error response.
#[derive(Debug, Deserialize, thiserror::Error)]
#[error("{error}: {errors:?}")] // Vec does not implement Display, only Debug and thus we use the debug output of :?
pub struct PdnsAPIError {
    /// Summary error.
    pub error: String,

    /// Individual errors.
    #[serde(default)]
    pub errors: Vec<String>,
}