NAC_Blockchain/nac-acc-1643/src/error.rs

88 lines
2.4 KiB
Rust

/// ACC-1643 错误类型定义
use std::fmt;
#[derive(Debug, Clone)]
pub enum Acc1643Error {
/// 未授权操作
Unauthorized {
operator: String,
required_role: String,
},
/// 文档不存在
DocumentNotFound {
doc_id: String,
},
/// 文档类型无效
InvalidDocumentType {
doc_type: String,
},
/// 文档已存在
DocumentAlreadyExists {
doc_id: String,
},
/// 文档验证失败
DocumentVerificationFailed {
doc_id: String,
reason: String,
},
/// 文档版本冲突
VersionConflict {
doc_id: String,
expected_version: u64,
actual_version: u64,
},
/// Merkle证明无效
InvalidMerkleProof,
/// 宪法收据无效
InvalidConstitutionalReceipt {
receipt_hash: String,
},
}
impl fmt::Display for Acc1643Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Acc1643Error::Unauthorized { operator, required_role } => {
write!(f, "Unauthorized: {} requires role {}", operator, required_role)
}
Acc1643Error::DocumentNotFound { doc_id } => {
write!(f, "Document not found: {}", doc_id)
}
Acc1643Error::InvalidDocumentType { doc_type } => {
write!(f, "Invalid document type: {}", doc_type)
}
Acc1643Error::DocumentAlreadyExists { doc_id } => {
write!(f, "Document already exists: {}", doc_id)
}
Acc1643Error::DocumentVerificationFailed { doc_id, reason } => {
write!(f, "Document verification failed for {}: {}", doc_id, reason)
}
Acc1643Error::VersionConflict { doc_id, expected_version, actual_version } => {
write!(
f,
"Version conflict for {}: expected {}, got {}",
doc_id, expected_version, actual_version
)
}
Acc1643Error::InvalidMerkleProof => {
write!(f, "Invalid Merkle proof")
}
Acc1643Error::InvalidConstitutionalReceipt { receipt_hash } => {
write!(f, "Invalid constitutional receipt: {}", receipt_hash)
}
}
}
}
impl std::error::Error for Acc1643Error {}
pub type Result<T> = std::result::Result<T, Acc1643Error>;