use thiserror::Error;
#[derive(Debug, Error)]
pub enum CosmosError {
#[error("Chain ID mismatch: expected '{expected}', got '{actual}'")]
ChainIdMismatch { expected: String, actual: String },
#[error("Validator not found: {pubkey_hex}")]
ValidatorNotFound { pubkey_hex: String },
#[error("Double signing attempt at height {height}, round {round}")]
DoubleSigning { height: i64, round: i32 },
#[error("Invalid vote type: {0}")]
InvalidVoteType(i32),
#[error("Invalid block ID: {0}")]
InvalidBlockId(String),
#[error("Signing failed: {0}")]
SigningFailed(String),
#[error("Missing required field: {0}")]
MissingField(&'static str),
#[error("Failed to bind server: {0}")]
BindError(String),
#[error("Internal error: {0}")]
Internal(String),
#[error("Service not available")]
ServiceUnavailable,
}
impl From<CosmosError> for tonic::Status {
fn from(err: CosmosError) -> Self {
match err {
CosmosError::ChainIdMismatch { .. } => {
tonic::Status::invalid_argument(err.to_string())
}
CosmosError::ValidatorNotFound { .. } => {
tonic::Status::not_found(err.to_string())
}
CosmosError::DoubleSigning { .. } => {
tonic::Status::failed_precondition(err.to_string())
}
CosmosError::InvalidVoteType(_) | CosmosError::InvalidBlockId(_) => {
tonic::Status::invalid_argument(err.to_string())
}
CosmosError::MissingField(_) => {
tonic::Status::invalid_argument(err.to_string())
}
CosmosError::SigningFailed(_) => {
tonic::Status::internal(err.to_string())
}
CosmosError::ServiceUnavailable => {
tonic::Status::unavailable(err.to_string())
}
CosmosError::BindError(_) | CosmosError::Internal(_) => {
tonic::Status::internal(err.to_string())
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum RemoteSignerCode {
Unknown = 0,
DoubleSign = 1,
HeightRegression = 2,
NotFound = 3,
Connection = 4,
NotImplemented = 5,
}
impl From<RemoteSignerCode> for i32 {
fn from(code: RemoteSignerCode) -> Self {
code as i32
}
}