use alloc::string::String;
use thiserror::Error;
use crate::rpc::errors::GrpcError;
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum GetAccountError {
#[error("internal server error")]
Internal,
#[error("deserialization failed")]
DeserializationFailed,
#[error("account not found")]
AccountNotFound,
#[error("account is not public")]
AccountNotPublic,
#[error("unknown block")]
UnknownBlock,
#[error("block pruned")]
BlockPruned,
#[error("unknown error code {code}: {message}")]
Unknown { code: u8, message: String },
}
impl GetAccountError {
pub fn from_code(code: u8, message: &str) -> Self {
match code {
0 => Self::Internal,
1 => Self::DeserializationFailed,
2 => Self::AccountNotFound,
3 => Self::AccountNotPublic,
4 => Self::UnknownBlock,
5 => Self::BlockPruned,
_ => Self::Unknown { code, message: String::from(message) },
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum RegisterAccountError {
#[error("invitation code does not exist")]
InvitationNotFound,
#[error("the invitation code or the account is already registered")]
AlreadyRegistered,
#[error("invalid registration request: {0}")]
InvalidRequest(String),
}
impl RegisterAccountError {
pub fn from_grpc_error(error_kind: &GrpcError, message: &str) -> Option<Self> {
match error_kind {
GrpcError::NotFound => Some(Self::InvitationNotFound),
GrpcError::AlreadyExists => Some(Self::AlreadyRegistered),
GrpcError::InvalidArgument => Some(Self::InvalidRequest(String::from(message))),
_ => None,
}
}
}