servicenow-cli 0.6.3

Fast, safe, human-friendly ServiceNow operations from the terminal
Documentation
mod client;

pub use client::{
    AttachmentMetadata, DisplayValue, ListOptions, MAY_HAVE_ADDED_TO_CART, MAY_HAVE_ORDERED,
    ServiceNowClient, normalize_instance, validate_sys_id, validate_table,
};

use std::fmt;

#[derive(Debug)]
pub enum ApiError {
    InvalidInput(String),
    Auth(String),
    /// The credentials were accepted, but an ACL or missing role refused the operation.
    Permission(String),
    NotFound(String),
    Conflict(String),
    /// The instance accepted a catalog order but created no request: the
    /// item was left in the cart `cart_id` instead.
    NotOrdered {
        message: String,
        cart_id: String,
    },
    RateLimit,
    Api {
        status: u16,
        message: String,
    },
    Http(reqwest::Error),
    Other(String),
}

impl fmt::Display for ApiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidInput(message) => write!(f, "{message}"),
            Self::Auth(message) => write!(f, "authentication failed: {message}"),
            Self::Permission(message) => write!(f, "permission denied: {message}"),
            Self::NotFound(message) => write!(f, "not found: {message}"),
            Self::Conflict(message) => write!(f, "conflict: {message}"),
            Self::NotOrdered { message, .. } => write!(f, "{message}"),
            Self::RateLimit => write!(f, "ServiceNow rate limit exceeded; wait and retry"),
            Self::Api { status, message } => {
                write!(f, "ServiceNow API error ({status}): {message}")
            }
            Self::Http(error) => write!(f, "HTTP error: {error}"),
            Self::Other(message) => write!(f, "{message}"),
        }
    }
}

impl std::error::Error for ApiError {}

impl From<reqwest::Error> for ApiError {
    fn from(value: reqwest::Error) -> Self {
        Self::Http(value)
    }
}