1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use std::io;
use tonic::Status;
#[derive(Copy, Clone, Debug)]
pub enum Error {
DatabaseClosed = 1, NotFound,
HeightIndexedVMNotImplemented,
IndexIncomplete,
StateSyncableVMNotImplemented,
}
impl Error {
pub fn as_str(&self) -> &'static str {
match *self {
Error::DatabaseClosed => "database closed",
Error::NotFound => "not found",
Error::HeightIndexedVMNotImplemented => {
"vm does not implement HeightIndexedChainVM interface"
}
Error::IndexIncomplete => "query failed because height index is incomplete",
Error::StateSyncableVMNotImplemented => {
"vm does not implement StateSyncableVM interface"
}
}
}
pub fn to_i32(&self) -> i32 {
match self {
Error::DatabaseClosed => 1,
Error::NotFound => 2,
Error::HeightIndexedVMNotImplemented => 3,
Error::IndexIncomplete => 4,
Error::StateSyncableVMNotImplemented => 5,
}
}
pub fn to_err(&self) -> io::Error {
match *self {
Error::DatabaseClosed => {
io::Error::new(io::ErrorKind::Other, Error::DatabaseClosed.as_str())
}
Error::NotFound => io::Error::new(io::ErrorKind::NotFound, Error::NotFound.as_str()),
Error::HeightIndexedVMNotImplemented => io::Error::new(
io::ErrorKind::Other,
Error::HeightIndexedVMNotImplemented.as_str(),
),
Error::IndexIncomplete => {
io::Error::new(io::ErrorKind::Other, Error::IndexIncomplete.as_str())
}
Error::StateSyncableVMNotImplemented => io::Error::new(
io::ErrorKind::Other,
Error::StateSyncableVMNotImplemented.as_str(),
),
}
}
}
pub fn from_i32(err: i32) -> io::Result<()> {
match err {
0 => Ok(()),
1 => Err(Error::DatabaseClosed.to_err()),
2 => Err(Error::NotFound.to_err()),
3 => Err(Error::HeightIndexedVMNotImplemented.to_err()),
4 => Err(Error::IndexIncomplete.to_err()),
5 => Err(Error::StateSyncableVMNotImplemented.to_err()),
_ => panic!("invalid error type"),
}
}
pub fn is_corruptible(error: &io::Error) -> bool {
match error {
e if e.kind() == io::ErrorKind::NotFound => false,
e if e.to_string() == Error::DatabaseClosed.as_str() => false,
_ => true,
}
}
pub fn is_not_found(error: &io::Error) -> bool {
error.kind() == io::ErrorKind::NotFound && error.to_string() == Error::NotFound.as_str()
}
pub fn from_string(message: String) -> io::Error {
io::Error::new(io::ErrorKind::Other, message)
}
pub fn from_status(status: Status) -> io::Error {
match status.message() {
m if m.contains("database closed") => Error::DatabaseClosed.to_err(),
m if m.contains("not found") => Error::NotFound.to_err(),
_ => io::Error::new(io::ErrorKind::Other, status.message()),
}
}