1use crate::rlpx::error::PeerConnectionError;
7use ethrex_rlp::error::RLPDecodeError;
8use ethrex_storage::error::StoreError;
9use ethrex_trie::TrieError;
10use spawned_concurrency::ActorError;
11use std::io::ErrorKind;
12use std::path::PathBuf;
13use thiserror::Error;
14
15#[derive(Debug, Error)]
17pub enum SnapError {
18 #[error(transparent)]
20 Store(#[from] StoreError),
21
22 #[error(transparent)]
24 Protocol(#[from] PeerConnectionError),
25
26 #[error(transparent)]
28 Trie(#[from] TrieError),
29
30 #[error(transparent)]
32 RlpDecode(#[from] RLPDecodeError),
33
34 #[error(transparent)]
36 PeerTable(#[from] ActorError),
37
38 #[error("Bad request: {0}")]
40 BadRequest(String),
41
42 #[error("Response validation failed: {0}")]
44 ValidationError(String),
45
46 #[error("Peer selection failed: {0}")]
48 PeerSelection(String),
49
50 #[error("No tasks in queue")]
52 NoTasks,
53
54 #[error("No account hashes available")]
56 NoAccountHashes,
57
58 #[error("No account storages available")]
60 NoAccountStorages,
61
62 #[error("No storage roots available")]
64 NoStorageRoots,
65
66 #[error("Unexpected internal error: {0}")]
68 InternalError(String),
69
70 #[error("File system error: {operation} at {}: {kind:?}", path.display())]
72 FileSystem {
73 operation: &'static str,
74 path: PathBuf,
75 kind: ErrorKind,
76 },
77
78 #[error("Snapshot directory error: {0}")]
80 SnapshotDir(String),
81
82 #[error("Task panicked: {0}")]
84 TaskPanic(String),
85
86 #[error("Invalid data received")]
88 InvalidData,
89
90 #[error("Hash mismatch in received data")]
92 InvalidHash,
93}
94
95impl SnapError {
96 pub fn dir_not_exists(path: PathBuf) -> Self {
98 Self::FileSystem {
99 operation: "check exists",
100 path,
101 kind: ErrorKind::NotFound,
102 }
103 }
104
105 pub fn dir_create_failed(path: PathBuf) -> Self {
107 Self::FileSystem {
108 operation: "create directory",
109 path,
110 kind: ErrorKind::Other,
111 }
112 }
113
114 pub fn write_failed(path: PathBuf, kind: ErrorKind) -> Self {
116 Self::FileSystem {
117 operation: "write",
118 path,
119 kind,
120 }
121 }
122}
123
124impl From<tokio::task::JoinError> for SnapError {
126 fn from(err: tokio::task::JoinError) -> Self {
127 SnapError::TaskPanic(err.to_string())
128 }
129}
130
131#[derive(Debug, thiserror::Error)]
133#[error("Failed to dump snapshot to {}: {:?}", path.display(), error)]
134pub struct DumpError {
135 pub path: PathBuf,
136 pub error: ErrorKind,
137}
138
139impl From<DumpError> for SnapError {
140 fn from(err: DumpError) -> Self {
141 SnapError::FileSystem {
142 operation: "dump snapshot",
143 path: err.path,
144 kind: err.error,
145 }
146 }
147}