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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::convert::Infallible;
use error_code::{self, ErrorCode, ErrorCodeExt};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum EvaluateError {
#[error("Execution terminated due to exceeding the deadline")]
DeadlineExceeded,
#[error("Invalid {charset} character string")]
InvalidCharacterString { charset: String },
#[error("{msg}")]
Custom { code: i32, msg: String },
#[error("{0}")]
Other(String),
}
impl EvaluateError {
pub fn code(&self) -> i32 {
match self {
EvaluateError::InvalidCharacterString { .. } => 1300,
EvaluateError::DeadlineExceeded => 9007,
EvaluateError::Custom { code, .. } => *code,
EvaluateError::Other(_) => 10000,
}
}
}
impl From<Box<dyn std::error::Error + Send + Sync>> for EvaluateError {
#[inline]
fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
EvaluateError::Other(err.to_string())
}
}
impl From<tikv_util::deadline::DeadlineError> for EvaluateError {
#[inline]
fn from(_: tikv_util::deadline::DeadlineError) -> Self {
EvaluateError::DeadlineExceeded
}
}
impl From<Infallible> for EvaluateError {
fn from(e: Infallible) -> Self {
match e {}
}
}
impl From<std::str::Utf8Error> for EvaluateError {
fn from(err: std::str::Utf8Error) -> Self {
EvaluateError::Other(format!("invalid input value: {:?}", err))
}
}
impl From<std::string::FromUtf8Error> for EvaluateError {
fn from(err: std::string::FromUtf8Error) -> Self {
EvaluateError::Other(format!("invalid input value: {:?}", err))
}
}
impl ErrorCodeExt for EvaluateError {
fn error_code(&self) -> ErrorCode {
match self {
EvaluateError::DeadlineExceeded => error_code::coprocessor::DEADLINE_EXCEEDED,
EvaluateError::InvalidCharacterString { .. } => {
error_code::coprocessor::INVALID_CHARACTER_STRING
}
EvaluateError::Custom { .. } => error_code::coprocessor::EVAL,
EvaluateError::Other(_) => error_code::UNKNOWN,
}
}
}
#[derive(Debug, Error)]
#[error(transparent)]
pub struct StorageError(#[from] pub anyhow::Error);
#[derive(Debug, Error)]
pub enum ErrorInner {
#[error("Storage error: {0}")]
Storage(#[source] StorageError),
#[error("Evaluate error: {0}")]
Evaluate(#[source] EvaluateError),
}
#[derive(Debug, Error)]
#[error(transparent)]
pub struct Error(#[from] pub Box<ErrorInner>);
impl From<ErrorInner> for Error {
#[inline]
fn from(e: ErrorInner) -> Self {
Error(Box::new(e))
}
}
impl From<StorageError> for Error {
#[inline]
fn from(e: StorageError) -> Self {
Error(Box::new(ErrorInner::Storage(e)))
}
}
impl From<EvaluateError> for Error {
#[inline]
fn from(e: EvaluateError) -> Self {
Error(Box::new(ErrorInner::Evaluate(e)))
}
}
impl<T: Into<EvaluateError>> From<T> for Error {
#[inline]
default fn from(err: T) -> Self {
let eval_error = err.into();
eval_error.into()
}
}
pub type Result<T> = std::result::Result<T, Error>;
impl ErrorCodeExt for Error {
fn error_code(&self) -> ErrorCode {
match self.0.as_ref() {
ErrorInner::Storage(_) => error_code::coprocessor::STORAGE_ERROR,
ErrorInner::Evaluate(e) => e.error_code(),
}
}
}