use alloc::string::String;
#[cfg(feature = "schemars")]
use schemars::JsonSchema;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[cfg(feature = "std")]
use std::error::Error as StdError;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum ErrorCode {
Unknown,
InvalidInput,
NotFound,
Conflict,
Timeout,
Unauthenticated,
PermissionDenied,
RateLimited,
Unavailable,
Internal,
}
#[derive(Debug, Error)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schemars", derive(JsonSchema))]
#[error("{code:?}: {message}")]
pub struct GreenticError {
pub code: ErrorCode,
pub message: String,
#[cfg(feature = "std")]
#[cfg_attr(feature = "serde", serde(skip, default = "default_source"))]
#[cfg_attr(feature = "schemars", schemars(skip))]
#[source]
source: Option<Box<dyn StdError + Send + Sync>>,
}
impl GreenticError {
pub fn new(code: ErrorCode, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
#[cfg(feature = "std")]
source: None,
}
}
#[cfg(feature = "std")]
pub fn with_source<E>(mut self, source: E) -> Self
where
E: StdError + Send + Sync + 'static,
{
self.source = Some(Box::new(source));
self
}
}
#[cfg(feature = "std")]
fn default_source() -> Option<Box<dyn StdError + Send + Sync>> {
None
}
#[cfg(feature = "time")]
impl From<time::error::ComponentRange> for GreenticError {
fn from(err: time::error::ComponentRange) -> Self {
Self::new(ErrorCode::InvalidInput, err.to_string())
}
}
#[cfg(feature = "time")]
impl From<time::error::Parse> for GreenticError {
fn from(err: time::error::Parse) -> Self {
Self::new(ErrorCode::InvalidInput, err.to_string())
}
}
#[cfg(feature = "uuid")]
impl From<uuid::Error> for GreenticError {
fn from(err: uuid::Error) -> Self {
Self::new(ErrorCode::InvalidInput, err.to_string())
}
}
pub type GResult<T> = core::result::Result<T, GreenticError>;