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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*!
Error types from Elasticsearch
*/

use serde::{Deserialize, Deserializer};
use serde_json::{Error as JsonError, Map, Value};
use std::io::Error as IoError;

quick_error! {
    /** An error parsing a response stream. */
    #[derive(Debug)]
    pub enum ParseResponseError {
        /** The response contains invalid json. */
        Json(err: JsonError) {
            from()
        }
        /** The response caused an io error while buffering. */
        Io(err: IoError) {
            from()
        }
    }
}

quick_error! {
    /** An error parsing a REST API response to a success value. */
    #[derive(Debug)]
    pub enum ResponseError {
        /** A REST API error from Elasticsearch. */
        Api(err: ApiError) {
            from()
        }
        /** An error parsing a response body. */
        Parse(err: ParseResponseError) {
            from()
        }
    }
}

quick_error! {
    /** A REST API error response. */
    #[derive(Debug, PartialEq)]
    pub enum ApiError {
        /**
        An index wasn't found.

        Some endpoints, like search, will return an `IndexNotFound` error if a request is made to a missing index.
        Other endpoints will return a successful response even if the index is missing but include some error property in the response body.
        */
        IndexNotFound { index: String } {
            description("index not found")
            display("index not found: '{}'", index)
        }
        /**
        A document wasn't found.

        This error can occur when attempting to update a document that doesn't already exist.
        */
        DocumentMissing { index: String } {
            description("document missing")
            display("document in index is missing: '{}'", index)
        }
        /**
        An index already exists but was expected to.

        Attempting to create an index with a name that's already in use will result in an `IndexAlreadyExists` error.
        */
        IndexAlreadyExists { index: String } {
            description("index already exists")
            display("index already exists: '{}'", index)
        }
        /**
        The request body contains invalid data.

        If a Query DSL query contains invalid JSON or unrecognised properties then Elasticsearch will return a `Parsing` error.
        */
        Parsing { line: u64, col: u64, reason: String } {
            description("request parse error")
            display("request parse error: '{}' on line: {}, col: {}", reason, line, col)
        }
        /**
        The document mapping contains invalid data.

        If a put mapping request contains invalid JSON or unrecognised properties then Elasticsearch will return a `MapperParsing` error.
        */
        MapperParsing { reason: String } {
            description("mapper parse error")
            display("mapper parse error: '{}'", reason)
        }
        /**
        The request body can't be processed.

        Some endpoints that expect certain constraints of a request to hold will return an `ActionRequestValidation` error if those constraints aren't met.
        */
        ActionRequestValidation { reason: String } {
            description("action request failed validation")
            display("action request failed validation: '{}'", reason)
        }
        /**
        A currently unrecognised error occurred.

        **WARNING:** Don't rely on this variant to capture an error.
        As new variants are introduced they will no longer be matched by `ApiError::Other`.
        For this reason, this variant will disappear in the future.
        */
        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(),
                }
            }
            "document_missing_exception" => {
                let index = error_key!(obj[index]: |v| v.as_str());

                ApiError::DocumentMissing {
                    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),
        }
    }
}