use std::error::Error;
use std::fmt;
use std::str::FromStr;
use anyhow::Error as AnyhowError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ApiError {
ValidationError,
GameNotAvailable,
ServerError,
GameNotStarted,
}
impl ApiError {
pub fn as_str(&self) -> &'static str {
match self {
ApiError::ValidationError => "VALIDATION_ERROR",
ApiError::GameNotAvailable => "GAME_NOT_AVAILABLE_ERROR",
ApiError::ServerError => "SERVER_ERROR",
ApiError::GameNotStarted => "GAME_NOT_STARTED_ERROR",
}
}
}
impl FromStr for ApiError {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"VALIDATION_ERROR" => Ok(ApiError::ValidationError),
"GAME_NOT_AVAILABLE_ERROR" => Ok(ApiError::GameNotAvailable),
"SERVER_ERROR" => Ok(ApiError::ServerError),
"GAME_NOT_STARTED_ERROR" => Ok(ApiError::GameNotStarted),
_ => Err(()),
}
}
}
impl fmt::Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug)]
pub struct ArcAgiError(pub AnyhowError);
impl ArcAgiError {
pub fn new(msg: impl fmt::Display) -> Self {
Self(anyhow::anyhow!("{}", msg))
}
}
impl fmt::Display for ArcAgiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl Error for ArcAgiError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.0.source()
}
}
impl From<AnyhowError> for ArcAgiError {
fn from(err: AnyhowError) -> Self {
Self(err)
}
}
impl From<reqwest::Error> for ArcAgiError {
fn from(err: reqwest::Error) -> Self {
Self(AnyhowError::new(err))
}
}
impl From<serde_json::Error> for ArcAgiError {
fn from(err: serde_json::Error) -> Self {
Self(AnyhowError::new(err))
}
}