#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("transport: {0}")]
Transport(String),
#[error("handshake failed")]
Handshake,
#[error("timed out")]
Timeout,
#[error("liteserver error {code}: {message}")]
LiteServer {
code: i32,
message: String,
},
#[error("decode: {0}")]
Decode(String),
#[error("address: {0}")]
Address(String),
#[error("config: {0}")]
Config(String),
#[error("cell: {0}")]
Cell(String),
#[error("proof: {0}")]
Proof(String),
#[error("sync: {0}")]
Sync(String),
#[error("the proven head is {age_seconds}s old, past the {limit_seconds}s bound")]
Stale {
age_seconds: u64,
limit_seconds: u64,
},
#[error("the local clock is {by_seconds}s behind the chain, past the {tolerated_seconds}s tolerated")]
ClockBehind {
by_seconds: u64,
tolerated_seconds: u64,
},
#[error("the connection lost its place and has to be reopened")]
ConnectionLost,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorCode {
Transport,
Handshake,
Timeout,
ConnectionLost,
LiteServer,
Decode,
Address,
Config,
Cell,
Proof,
Sync,
Stale,
ClockBehind,
InvalidArgument,
}
impl ErrorCode {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::Transport => "TRANSPORT",
Self::Handshake => "HANDSHAKE",
Self::Timeout => "TIMEOUT",
Self::ConnectionLost => "CONNECTION_LOST",
Self::LiteServer => "LITESERVER",
Self::Decode => "DECODE",
Self::Address => "ADDRESS",
Self::Config => "CONFIG",
Self::Cell => "CELL",
Self::Proof => "PROOF",
Self::Sync => "SYNC",
Self::Stale => "STALE",
Self::ClockBehind => "CLOCK_BEHIND",
Self::InvalidArgument => "INVALID_ARGUMENT",
}
}
}
impl std::fmt::Display for ErrorCode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl Error {
#[must_use]
pub fn code(&self) -> ErrorCode {
match self {
Self::Transport(_) => ErrorCode::Transport,
Self::Handshake => ErrorCode::Handshake,
Self::Timeout => ErrorCode::Timeout,
Self::ConnectionLost => ErrorCode::ConnectionLost,
Self::LiteServer { .. } => ErrorCode::LiteServer,
Self::Decode(_) => ErrorCode::Decode,
Self::Address(_) => ErrorCode::Address,
Self::Config(_) => ErrorCode::Config,
Self::Cell(_) => ErrorCode::Cell,
Self::Proof(_) => ErrorCode::Proof,
Self::Sync(_) => ErrorCode::Sync,
Self::Stale { .. } => ErrorCode::Stale,
Self::ClockBehind { .. } => ErrorCode::ClockBehind,
}
}
pub(crate) fn decoding(error: &ton_net_block::BlockError) -> Self {
Self::Cell(error.to_string())
}
}
impl From<ton_net_block::BlockError> for Error {
fn from(error: ton_net_block::BlockError) -> Self {
Self::Proof(error.to_string())
}
}
impl From<ton_net_adnl::TransportError> for Error {
fn from(error: ton_net_adnl::TransportError) -> Self {
Self::Transport(error.to_string())
}
}
impl From<ton_net_adnl::AdnlError> for Error {
fn from(error: ton_net_adnl::AdnlError) -> Self {
use ton_net_adnl::AdnlError;
match error {
AdnlError::Transport(transport) => transport.into(),
AdnlError::Handshake(_) => Self::Handshake,
AdnlError::Desynchronized => Self::ConnectionLost,
other => Self::Decode(other.to_string()),
}
}
}
impl From<ton_net_lite::LiteError> for Error {
fn from(error: ton_net_lite::LiteError) -> Self {
use ton_net_lite::LiteError;
match error {
LiteError::Adnl(adnl) => adnl.into(),
LiteError::LiteServer { code, message } => Self::LiteServer { code, message },
LiteError::Decode(decode) => Self::Decode(decode.to_string()),
other => Self::Decode(other.to_string()),
}
}
}
#[cfg(test)]
mod tests {
use super::Error;
use ton_net_block::BlockError;
#[test]
fn a_failed_proof_never_arrives_as_something_softer() {
for failure in [
BlockError::NotAMerkleProof,
BlockError::ProofInconsistent,
BlockError::ProofNotAnchored,
BlockError::NotCovered,
BlockError::NotBound,
BlockError::WrongConstructor { expected: "block" },
BlockError::Malformed("shard state"),
BlockError::Cell(ton_net_cell::CellError::LabelTooLong),
BlockError::Cell(ton_net_cell::CellError::NotABagOfCells),
BlockError::Cell(ton_net_cell::CellError::Truncated),
] {
let mapped = Error::from(failure.clone());
assert!(
matches!(mapped, Error::Proof(_)),
"{failure:?} became {mapped:?}"
);
}
}
#[test]
fn a_read_that_checked_nothing_reports_no_proof_failure() {
for failure in [
BlockError::Malformed("account address"),
BlockError::Cell(ton_net_cell::CellError::NotABagOfCells),
] {
let mapped = Error::decoding(&failure);
assert!(
matches!(mapped, Error::Cell(_)),
"{failure:?} became {mapped:?}"
);
}
}
}