#[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 {
ErrorCode::Transport => "TRANSPORT",
ErrorCode::Handshake => "HANDSHAKE",
ErrorCode::Timeout => "TIMEOUT",
ErrorCode::ConnectionLost => "CONNECTION_LOST",
ErrorCode::LiteServer => "LITESERVER",
ErrorCode::Decode => "DECODE",
ErrorCode::Address => "ADDRESS",
ErrorCode::Config => "CONFIG",
ErrorCode::Cell => "CELL",
ErrorCode::Proof => "PROOF",
ErrorCode::Sync => "SYNC",
ErrorCode::Stale => "STALE",
ErrorCode::ClockBehind => "CLOCK_BEHIND",
ErrorCode::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 {
Error::Transport(_) => ErrorCode::Transport,
Error::Handshake => ErrorCode::Handshake,
Error::Timeout => ErrorCode::Timeout,
Error::ConnectionLost => ErrorCode::ConnectionLost,
Error::LiteServer { .. } => ErrorCode::LiteServer,
Error::Decode(_) => ErrorCode::Decode,
Error::Address(_) => ErrorCode::Address,
Error::Config(_) => ErrorCode::Config,
Error::Cell(_) => ErrorCode::Cell,
Error::Proof(_) => ErrorCode::Proof,
Error::Sync(_) => ErrorCode::Sync,
Error::Stale { .. } => ErrorCode::Stale,
Error::ClockBehind { .. } => ErrorCode::ClockBehind,
}
}
pub(crate) fn decoding(error: ton_net_block::BlockError) -> Error {
Error::Cell(error.to_string())
}
}
impl From<ton_net_block::BlockError> for Error {
fn from(error: ton_net_block::BlockError) -> Self {
Error::Proof(error.to_string())
}
}
impl From<ton_net_adnl::TransportError> for Error {
fn from(error: ton_net_adnl::TransportError) -> Self {
Error::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(_) => Error::Handshake,
AdnlError::Desynchronized => Error::ConnectionLost,
other => Error::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 } => Error::LiteServer { code, message },
LiteError::Decode(decode) => Error::Decode(decode.to_string()),
other => Error::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::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.clone());
assert!(
matches!(mapped, Error::Cell(_)),
"{failure:?} became {mapped:?}"
);
}
}
}