use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum EscrowError {
#[error("condition error: {0}")]
Condition(#[from] ConditionError),
#[error("invalid state transition")]
InvalidState,
#[error("identity error: {0}")]
Identity(#[from] IdentityError),
#[error("asset error: {0}")]
Asset(#[from] AssetError),
#[error("invalid chain operation: {0}")]
InvalidChainOp(String),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[cfg(feature = "json")]
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("unsupported chain specified")]
UnsupportedChain,
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ConditionError {
#[error("preimage (hashlock) failed: {0}")]
Hashlock(#[from] crate::condition::hashlock::Error),
#[error("ed25519 signature failed: {0}")]
Ed25519(#[from] crate::condition::ed25519::Error),
#[error("secp256k1 signature failed: {0}")]
Secp256k1(#[from] crate::condition::secp256k1::Error),
#[error("threshold check failed: {0}")]
Threshold(#[from] crate::condition::threshold::Error),
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum IdentityError {
#[error("empty identity string")]
EmptyIdentity,
#[error("input length {len} exceeds maximum of {max} characters")]
InputTooLong {
len: usize,
max: usize,
},
#[error("hex decoding error: {0}")]
Hex(#[from] hex::FromHexError),
#[error("Base58 decoding error: {0}")]
Base58(#[from] bs58::decode::Error),
#[error("Base64 decoding error: {0}")]
Base64(#[from] base64::DecodeError),
#[error("unsupported identity format")]
UnsupportedFormat,
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum AssetError {
#[error("could not serialize asset: {0}")]
Serialization(String),
#[error("could not parse asset: {0}")]
Parsing(String),
#[error("amount must be non-zero")]
ZeroAmount,
#[error("missing ID for asset, program, or contract")]
MissingId,
}
impl EscrowError {
pub fn to_str(&self) -> String {
self.to_string()
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum CommitmentError {
#[error("amount exceeds 256 bits and cannot be committed")]
AmountTooLarge,
#[error("field length {0} exceeds the journal's 16-bit size limit")]
FieldTooLong(usize),
#[error("journal truncated at offset {0}")]
Truncated(usize),
#[error("journal has trailing bytes after offset {0}")]
TrailingBytes(usize),
#[error("unknown journal version {0}")]
UnknownVersion(u8),
#[error("unknown {field} tag {value}")]
UnknownTag {
field: &'static str,
value: u8,
},
}
impl CommitmentError {
pub(crate) fn unknown_tag(field: &'static str, value: u8) -> Self {
Self::UnknownTag { field, value }
}
}