xet_core_structures/
error.rs1use std::convert::Infallible;
2
3use thiserror::Error;
4use tracing::warn;
5
6use crate::merklehash::MerkleHash;
7
8#[non_exhaustive]
9#[derive(Error, Debug)]
10pub enum CoreError {
11 #[error("I/O error: {0}")]
13 Io(#[from] std::io::Error),
14
15 #[error("Internal error: {0}")]
16 InternalError(String),
17
18 #[error("{0}")]
19 Other(String),
20
21 #[error("Too many collisions for truncated hash: {0}")]
23 TruncatedHashCollision(u64),
24
25 #[error("Shard version error: {0}")]
26 ShardVersion(String),
27
28 #[error("Bad filename: {0}")]
29 BadFilename(String),
30
31 #[error("Shard not found: {0}")]
32 ShardNotFound(MerkleHash),
33
34 #[error("File not found: {0}")]
35 FileNotFound(MerkleHash),
36
37 #[error("Query failed: {0}")]
38 QueryFailed(String),
39
40 #[error("Smudge query policy error: {0}")]
41 SmudgeQueryPolicy(String),
42
43 #[error("Invalid shard: {0}")]
44 InvalidShard(String),
45
46 #[error("Invalid range")]
48 InvalidRange,
49
50 #[error("Invalid arguments")]
51 InvalidArguments,
52
53 #[error("Malformed data: {0}")]
54 MalformedData(String),
55
56 #[error("Hash mismatch")]
57 HashMismatch,
58
59 #[error("Compression error: {0}")]
60 CompressionError(#[from] lz4_flex::frame::Error),
61
62 #[error("Hash parsing error: {0}")]
63 HashParsing(#[from] Infallible),
64
65 #[error("Chunk header parse error")]
66 ChunkHeaderParse,
67
68 #[error("Runtime error: {0}")]
70 RuntimeError(#[from] xet_runtime::RuntimeError),
71
72 #[error("Task lock error: {0}")]
73 TaskRuntime(#[from] xet_runtime::utils::RwTaskLockError),
74
75 #[error("Task join error: {0}")]
76 TaskJoin(#[from] tokio::task::JoinError),
77}
78
79pub type Result<T> = std::result::Result<T, CoreError>;
80
81impl PartialEq for CoreError {
82 fn eq(&self, other: &CoreError) -> bool {
83 std::mem::discriminant(self) == std::mem::discriminant(other)
84 }
85}
86
87impl CoreError {
88 pub fn other(inner: impl ToString) -> Self {
89 Self::Other(inner.to_string())
90 }
91
92 pub fn invalid_shard(inner: impl ToString) -> Self {
93 Self::InvalidShard(inner.to_string())
94 }
95}
96
97pub trait Validate<T> {
99 fn ok_for_format_error(self) -> Result<Option<T>>;
100}
101
102impl<T> Validate<T> for Result<T> {
103 fn ok_for_format_error(self) -> Result<Option<T>> {
104 match self {
105 Ok(v) => Ok(Some(v)),
106 Err(CoreError::MalformedData(e)) => {
107 warn!("XORB Validation: {e}");
108 Ok(None)
109 },
110 Err(e) => Err(e),
111 }
112 }
113}
114
115impl From<crate::merklehash::DataHashHexParseError> for CoreError {
116 fn from(_: crate::merklehash::DataHashHexParseError) -> Self {
117 CoreError::Other("Invalid hex input for DataHash".to_string())
118 }
119}
120
121impl From<crate::merklehash::DataHashBytesParseError> for CoreError {
122 fn from(_: crate::merklehash::DataHashBytesParseError) -> Self {
123 CoreError::Other("Invalid bytes input for DataHash".to_string())
124 }
125}