ark-api 0.17.0-pre.15

Ark API
Documentation
pub(crate) use crate::ffi::ErrorCode;
use std::fmt::Display;

/// API error enum, describes how an Ark API call can fail
///
/// This is a more ergonomic version of the Ark API FFI `ErrorCode` value as this can only
/// contain errors, not success and it implements [`std::error::Error`] to be able to be used in [`std::result::Result`]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Error {
    /// API not available.
    ///
    /// Typically happens if a module calls an API it has not specified that it requires
    ApiNotAvailable,

    /// Internal error - something went wrong on in the Ark host
    Internal,

    /// HTTP request failed due to external server error - something went wrong when communicating with the remote server
    ///
    /// This can happen when sending a HTTP request to a server that is unavailable, if DNS lookup fails, or other server related issues.
    HttpRequestFailed,

    /// Invalid arguments - the inputs to the called API function were not valid
    InvalidArguments,

    /// Resource not found - used by APIs to indicate a requested resource can't be found
    NotFound,

    /// The API or resource is currently not available
    Unavailable,

    /// Other unknown error with specific code
    ///
    /// This can happen if a newer version of Ark introduces a new type of error
    Other(u32),
}

#[allow(clippy::fallible_impl_from)] // better to panic here if `Success` is used instead of in all use cases
impl From<ErrorCode> for Error {
    fn from(code: ErrorCode) -> Self {
        match code {
            ErrorCode::Success => panic!("Unexpected ErrorCode::Success"),
            ErrorCode::ApiNotAvailable => Self::ApiNotAvailable,
            ErrorCode::InternalError => Self::Internal,
            ErrorCode::HttpRequestFailed => Self::HttpRequestFailed,
            ErrorCode::InvalidArguments => Self::InvalidArguments,
            ErrorCode::NotFound => Self::NotFound,
            ErrorCode::Unavailable => Self::Unavailable,
            code => Self::Other(code as u32),
        }
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ApiNotAvailable => write!(f, "API not available"),
            Self::Internal => write!(f, "Internal error"),
            Self::HttpRequestFailed => {
                write!(f, "HTTP request failed due to external server error")
            }
            Self::InvalidArguments => write!(f, "Invalid arguments"),
            Self::NotFound => write!(f, "Not found"),
            Self::Unavailable => write!(f, "Unavailable"),
            Self::Other(code) => write!(f, "Unknown code {code}"),
        }?;
        Ok(())
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None
    }
}