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
142
143
144
145
146
147
148
149
150
151
152
use serde::{Deserialize, Deserializer};
use serde_json::{Map, Value, Error as JsonError};
use std::io::Error as IoError;
quick_error! {
#[derive(Debug)]
pub enum ParseResponseError {
Json(err: JsonError) {
from()
}
Io(err: IoError) {
from()
}
}
}
quick_error! {
#[derive(Debug)]
pub enum ResponseError {
Api(err: ApiError) {
from()
}
Parse(err: ParseResponseError) {
from()
}
}
}
quick_error! {
#[derive(Debug, PartialEq)]
pub enum ApiError {
IndexNotFound { index: String } {
description("index not found")
display("index not found: '{}'", index)
}
IndexAlreadyExists { index: String } {
description("index already exists")
display("index already exists: '{}'", index)
}
Parsing { line: u64, col: u64, reason: String } {
description("request parse error")
display("request parse error: '{}' on line: {}, col: {}", reason, line, col)
}
MapperParsing { reason: String } {
description("mapper parse error")
display("mapper parse error: '{}'", reason)
}
ActionRequestValidation { reason: String }
Other(v: Map<String, Value>) {
description("error response from Elasticsearch")
display("error response from Elasticsearch: {:?}", v)
}
#[doc(hidden)]
__NonExhaustive {}
}
}
macro_rules! error_key {
($obj:ident [ $key:ident ] : |$cast:ident| $cast_expr:expr) => ({
let key = $obj.get(stringify!($key))
.and_then(|$cast| $cast_expr)
.map(|v| v.to_owned());
match key {
Some(v) => v,
_ => return ApiError::Other($obj).into()
}
}
)
}
impl<'de> Deserialize<'de> for ApiError {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
let value = Map::deserialize(deserializer)?;
Ok(value.into())
}
}
impl From<Map<String, Value>> for ApiError {
fn from(mut value: Map<String, Value>) -> Self {
let obj = {
match value.remove("error") {
Some(Value::Object(value)) => value,
_ => return ApiError::Other(value),
}
};
let ty = {
let ty = obj.get("type")
.and_then(|v| v.as_str())
.map(|v| v.to_owned());
match ty {
Some(ty) => ty,
_ => return ApiError::Other(obj),
}
};
match ty.as_ref() {
"index_not_found_exception" => {
let index = error_key!(obj[index]: |v| v.as_str());
ApiError::IndexNotFound { index: index.into() }
}
"index_already_exists_exception" => {
let index = error_key!(obj[index]: |v| v.as_str());
ApiError::IndexAlreadyExists { index: index.into() }
}
"parsing_exception" => {
let line = error_key!(obj[line]: |v| v.as_u64());
let col = error_key!(obj[col]: |v| v.as_u64());
let reason = error_key!(obj[reason]: |v| v.as_str());
ApiError::Parsing {
line: line,
col: col,
reason: reason.into(),
}
}
"mapper_parsing_exception" => {
let reason = error_key!(obj[reason]: |v| v.as_str());
ApiError::MapperParsing {
reason: reason.into(),
}
}
"action_request_validation_exception" => {
let reason = error_key!(obj[reason]: |v| v.as_str());
ApiError::ActionRequestValidation {
reason: reason.into(),
}
}
_ => ApiError::Other(obj),
}
}
}