use std::fmt::{self, Display};
use encore_sdk_core::api;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrCode {
Canceled,
Unknown,
InvalidArgument,
DeadlineExceeded,
NotFound,
AlreadyExists,
PermissionDenied,
ResourceExhausted,
FailedPrecondition,
Aborted,
OutOfRange,
Unimplemented,
Internal,
Unavailable,
DataLoss,
Unauthenticated,
}
#[derive(Debug, Clone)]
pub struct Error {
pub code: ErrCode,
pub message: String,
}
impl Error {
pub fn new(code: ErrCode, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
}
}
pub fn internal(message: impl Into<String>) -> Self {
Self::new(ErrCode::Internal, message)
}
pub fn invalid_argument(message: impl Into<String>) -> Self {
Self::new(ErrCode::InvalidArgument, message)
}
pub fn not_found(message: impl Into<String>) -> Self {
Self::new(ErrCode::NotFound, message)
}
pub fn unavailable(message: impl Into<String>) -> Self {
Self::new(ErrCode::Unavailable, message)
}
pub fn unauthenticated(message: impl Into<String>) -> Self {
Self::new(ErrCode::Unauthenticated, message)
}
pub(crate) fn wrap(code: ErrCode, cause: impl Display) -> Self {
Self::new(code, cause.to_string())
}
}
impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}: {}", self.code, self.message)
}
}
impl std::error::Error for Error {}
impl From<api::ErrCode> for ErrCode {
fn from(c: api::ErrCode) -> Self {
match c {
api::ErrCode::Canceled => ErrCode::Canceled,
api::ErrCode::Unknown => ErrCode::Unknown,
api::ErrCode::InvalidArgument => ErrCode::InvalidArgument,
api::ErrCode::DeadlineExceeded => ErrCode::DeadlineExceeded,
api::ErrCode::NotFound => ErrCode::NotFound,
api::ErrCode::AlreadyExists => ErrCode::AlreadyExists,
api::ErrCode::PermissionDenied => ErrCode::PermissionDenied,
api::ErrCode::ResourceExhausted => ErrCode::ResourceExhausted,
api::ErrCode::FailedPrecondition => ErrCode::FailedPrecondition,
api::ErrCode::Aborted => ErrCode::Aborted,
api::ErrCode::OutOfRange => ErrCode::OutOfRange,
api::ErrCode::Unimplemented => ErrCode::Unimplemented,
api::ErrCode::Internal => ErrCode::Internal,
api::ErrCode::Unavailable => ErrCode::Unavailable,
api::ErrCode::DataLoss => ErrCode::DataLoss,
api::ErrCode::Unauthenticated => ErrCode::Unauthenticated,
}
}
}
impl From<ErrCode> for api::ErrCode {
fn from(c: ErrCode) -> Self {
match c {
ErrCode::Canceled => api::ErrCode::Canceled,
ErrCode::Unknown => api::ErrCode::Unknown,
ErrCode::InvalidArgument => api::ErrCode::InvalidArgument,
ErrCode::DeadlineExceeded => api::ErrCode::DeadlineExceeded,
ErrCode::NotFound => api::ErrCode::NotFound,
ErrCode::AlreadyExists => api::ErrCode::AlreadyExists,
ErrCode::PermissionDenied => api::ErrCode::PermissionDenied,
ErrCode::ResourceExhausted => api::ErrCode::ResourceExhausted,
ErrCode::FailedPrecondition => api::ErrCode::FailedPrecondition,
ErrCode::Aborted => api::ErrCode::Aborted,
ErrCode::OutOfRange => api::ErrCode::OutOfRange,
ErrCode::Unimplemented => api::ErrCode::Unimplemented,
ErrCode::Internal => api::ErrCode::Internal,
ErrCode::Unavailable => api::ErrCode::Unavailable,
ErrCode::DataLoss => api::ErrCode::DataLoss,
ErrCode::Unauthenticated => api::ErrCode::Unauthenticated,
}
}
}
impl From<api::Error> for Error {
fn from(e: api::Error) -> Self {
Self {
code: e.code.into(),
message: e.message,
}
}
}