Skip to main content

rs_es/
error.rs

1/*
2 * Copyright 2015-2018 Ben Ashford
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Errors and error conversion code for the `rs_es` crate
18
19use std::error::Error;
20use std::fmt;
21use std::io::{self, Read};
22
23use serde_json;
24
25// Error handling
26
27/// Error that can occur include IO and parsing errors, as well as specific
28/// errors from the ElasticSearch server and logic errors from this library
29#[derive(Debug)]
30pub enum EsError {
31    /// An internal error from this library
32    EsError(String),
33
34    /// An error reported in a JSON response from the ElasticSearch server
35    EsServerError(String),
36
37    /// Miscellaneous error from the HTTP library
38    HttpError(reqwest::Error),
39
40    /// Miscellaneous IO error
41    IoError(io::Error),
42
43    /// JSON error
44    JsonError(serde_json::error::Error),
45}
46
47impl From<io::Error> for EsError {
48    fn from(err: io::Error) -> EsError {
49        EsError::IoError(err)
50    }
51}
52
53impl From<reqwest::Error> for EsError {
54    fn from(err: reqwest::Error) -> EsError {
55        EsError::HttpError(err)
56    }
57}
58
59impl From<serde_json::error::Error> for EsError {
60    fn from(err: serde_json::error::Error) -> EsError {
61        EsError::JsonError(err)
62    }
63}
64
65impl<'a> From<&'a mut reqwest::Response> for EsError {
66    fn from(err: &'a mut reqwest::Response) -> EsError {
67        let mut body = String::new();
68        match err.read_to_string(&mut body) {
69            Ok(_) => (),
70            Err(_) => {
71                return EsError::EsServerError(format!(
72                    "{} - cannot read response - {:?}",
73                    err.status(),
74                    err
75                ));
76            }
77        }
78        EsError::EsServerError(format!("{} - {}", err.status(), body))
79    }
80}
81
82impl Error for EsError {
83    fn description(&self) -> &str {
84        match *self {
85            EsError::EsError(ref err) => err,
86            EsError::EsServerError(ref err) => err,
87            EsError::HttpError(ref err) => err.description(),
88            EsError::IoError(ref err) => err.description(),
89            EsError::JsonError(ref err) => err.description(),
90        }
91    }
92
93    fn cause(&self) -> Option<&dyn Error> {
94        match *self {
95            EsError::EsError(_) => None,
96            EsError::EsServerError(_) => None,
97            EsError::HttpError(ref err) => Some(err as &dyn Error),
98            EsError::IoError(ref err) => Some(err as &dyn Error),
99            EsError::JsonError(ref err) => Some(err as &dyn Error),
100        }
101    }
102}
103
104impl fmt::Display for EsError {
105    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
106        match *self {
107            EsError::EsError(ref s) => fmt::Display::fmt(s, f),
108            EsError::EsServerError(ref s) => fmt::Display::fmt(s, f),
109            EsError::HttpError(ref err) => fmt::Display::fmt(err, f),
110            EsError::IoError(ref err) => fmt::Display::fmt(err, f),
111            EsError::JsonError(ref err) => fmt::Display::fmt(err, f),
112        }
113    }
114}