pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Entity not found.")]
NotFound,
#[error("Entity already exists.")]
AlreadyExists,
#[error("Precondition failed: the object was modified concurrently.")]
Conflict,
#[error("Invalid argument: {0}")]
InvalidArgument(String),
#[error("Invalid identifier: {0}")]
InvalidIdentifier(#[from] uuid::Error),
#[error("Generic error: {0}")]
Generic(String),
#[error(transparent)]
SerDe(#[from] serde_json::Error),
}
impl Error {
pub fn generic(msg: impl Into<String>) -> Self {
Self::Generic(msg.into())
}
pub fn invalid_argument(msg: impl Into<String>) -> Self {
Self::InvalidArgument(msg.into())
}
pub(crate) fn kind_str(&self) -> &'static str {
match self {
Error::NotFound => "not_found",
Error::AlreadyExists => "already_exists",
Error::Conflict => "conflict",
Error::InvalidArgument(_) => "invalid_argument",
Error::InvalidIdentifier(_) => "invalid_identifier",
Error::Generic(_) => "generic",
Error::SerDe(_) => "serde",
}
}
}