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
// Copyright 2017 LambdaStack All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
extern crate serde_json;

use ceph_version::CephVersion;
use serde_json::error::Error as SerdeJsonError;
use std::error::Error as StdError;
use std::ffi::{IntoStringError, NulError};
use std::io::Error;
use std::num::ParseIntError;
use std::string::FromUtf8Error;
use std::{fmt, str};
use uuid::ParseError;

/// Custom error handling for the library
#[derive(Debug)]
pub enum RadosError {
    FromUtf8Error(FromUtf8Error),
    NulError(NulError),
    Error(String),
    IoError(Error),
    IntoStringError(IntoStringError),
    ParseIntError(ParseIntError),
    ParseError(ParseError),
    SerdeError(SerdeJsonError),
    /// This should be the minimum version and the current version
    MinVersion(CephVersion, CephVersion),
    Parse(String),
}

pub type RadosResult<T> = Result<T, RadosError>;

impl fmt::Display for RadosError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.description())
    }
}

impl StdError for RadosError {
    fn description(&self) -> &str {
        match *self {
            RadosError::FromUtf8Error(ref e) => e.description(),
            RadosError::NulError(ref e) => e.description(),
            RadosError::Error(ref e) => &e,
            RadosError::IoError(ref e) => e.description(),
            RadosError::IntoStringError(ref e) => e.description(),
            RadosError::ParseError(ref e) => e.description(),
            RadosError::ParseIntError(ref e) => e.description(),
            RadosError::SerdeError(ref e) => e.description(),
            RadosError::MinVersion(ref _min, ref _current_version) => "Ceph version is too low",
            RadosError::Parse(ref _input) => "An error occurred during parsing",
        }
    }
    fn cause(&self) -> Option<&StdError> {
        match *self {
            RadosError::FromUtf8Error(ref e) => e.cause(),
            RadosError::NulError(ref e) => e.cause(),
            RadosError::Error(_) => None,
            RadosError::IoError(ref e) => e.cause(),
            RadosError::IntoStringError(ref e) => e.cause(),
            RadosError::ParseError(ref e) => e.cause(),
            RadosError::ParseIntError(ref e) => e.cause(),
            RadosError::SerdeError(ref e) => e.cause(),
            RadosError::MinVersion(ref _min, ref _current_version) => None,
            RadosError::Parse(ref _input) => None,
        }
    }
}

impl RadosError {
    /// Create a new RadosError with a String message
    pub fn new(err: String) -> RadosError {
        RadosError::Error(err)
    }

    /// Convert a RadosError into a String representation.
    pub fn to_string(&self) -> String {
        match *self {
            RadosError::FromUtf8Error(ref err) => err.utf8_error().to_string(),
            RadosError::NulError(ref err) => err.description().to_string(),
            RadosError::Error(ref err) => err.to_string(),
            RadosError::IoError(ref err) => err.description().to_string(),
            RadosError::IntoStringError(ref err) => err.description().to_string(),
            RadosError::ParseError(_) => "Uuid parse error".to_string(),
            RadosError::ParseIntError(ref err) => err.description().to_string(),
            RadosError::SerdeError(ref err) => err.description().to_string(),
            RadosError::MinVersion(ref min, ref current_version) => {
                format!("{:?} minimum, your version is {:?}", min, current_version)
            }
            RadosError::Parse(ref input) => format!("Couldn't parse the CephVersion from {}", input),
        }
    }
}

impl From<ParseError> for RadosError {
    fn from(err: ParseError) -> RadosError {
        RadosError::ParseError(err)
    }
}

impl From<ParseIntError> for RadosError {
    fn from(err: ParseIntError) -> RadosError {
        RadosError::ParseIntError(err)
    }
}

impl From<SerdeJsonError> for RadosError {
    fn from(err: SerdeJsonError) -> RadosError {
        RadosError::SerdeError(err)
    }
}

impl From<NulError> for RadosError {
    fn from(err: NulError) -> RadosError {
        RadosError::NulError(err)
    }
}

impl From<FromUtf8Error> for RadosError {
    fn from(err: FromUtf8Error) -> RadosError {
        RadosError::FromUtf8Error(err)
    }
}
impl From<IntoStringError> for RadosError {
    fn from(err: IntoStringError) -> RadosError {
        RadosError::IntoStringError(err)
    }
}
impl From<Error> for RadosError {
    fn from(err: Error) -> RadosError {
        RadosError::IoError(err)
    }
}