ceph_async/
error.rs

1// Copyright 2017 LambdaStack All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14extern crate serde_json;
15
16use crate::ceph_version::CephVersion;
17use serde_json::error::Error as SerdeJsonError;
18use std::error::Error as StdError;
19use std::ffi::{IntoStringError, NulError};
20use std::io::Error;
21use std::num::ParseIntError;
22use std::string::FromUtf8Error;
23use std::{fmt, str::ParseBoolError};
24use uuid::Error as UuidError;
25
26extern crate nix;
27
28/// Custom error handling for the library
29#[derive(Debug)]
30pub enum RadosError {
31    FromUtf8Error(FromUtf8Error),
32    NulError(NulError),
33    Error(String),
34    IoError(Error),
35    ApiError(nix::errno::Errno),
36    IntoStringError(IntoStringError),
37    ParseIntError(ParseIntError),
38    ParseBoolError(ParseBoolError),
39    UuidError(UuidError),
40    SerdeError(SerdeJsonError),
41    /// This should be the minimum version and the current version
42    MinVersion(CephVersion, CephVersion),
43    Parse(String),
44}
45
46pub type RadosResult<T> = Result<T, RadosError>;
47
48impl fmt::Display for RadosError {
49    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
50        match *self {
51            RadosError::FromUtf8Error(ref e) => f.write_str(&e.to_string()),
52            RadosError::NulError(ref e) => f.write_str(&e.to_string()),
53            RadosError::Error(ref e) => f.write_str(&e),
54            RadosError::IoError(ref e) => f.write_str(&e.to_string()),
55            RadosError::ApiError(ref e) => e.fmt(f),
56            RadosError::IntoStringError(ref e) => f.write_str(&e.to_string()),
57            RadosError::UuidError(ref e) => f.write_str(&e.to_string()),
58            RadosError::ParseBoolError(ref e) => f.write_str(&e.to_string()),
59            RadosError::ParseIntError(ref e) => f.write_str(&e.to_string()),
60            RadosError::SerdeError(ref e) => f.write_str(&e.to_string()),
61            RadosError::MinVersion(ref _min, ref _current_version) => {
62                f.write_str("Ceph version is too low")
63            }
64            RadosError::Parse(ref _input) => f.write_str("An error occurred during parsing"),
65        }
66    }
67}
68
69impl StdError for RadosError {
70    fn source(&self) -> Option<&(dyn StdError + 'static)> {
71        match *self {
72            RadosError::FromUtf8Error(ref e) => e.source(),
73            RadosError::NulError(ref e) => e.source(),
74            RadosError::Error(ref _e) => None,
75            RadosError::IoError(ref e) => e.source(),
76            RadosError::ApiError(ref e) => e.source(),
77            RadosError::IntoStringError(ref e) => e.source(),
78            RadosError::UuidError(ref e) => e.source(),
79            RadosError::ParseBoolError(ref e) => e.source(),
80            RadosError::ParseIntError(ref e) => e.source(),
81            RadosError::SerdeError(ref e) => e.source(),
82            RadosError::MinVersion(ref _min, ref _current_version) => None,
83            RadosError::Parse(ref _input) => None,
84        }
85    }
86}
87
88impl RadosError {
89    /// Create a new RadosError with a String message
90    pub fn new(err: String) -> RadosError {
91        RadosError::Error(err)
92    }
93}
94
95impl From<UuidError> for RadosError {
96    fn from(err: UuidError) -> RadosError {
97        RadosError::UuidError(err)
98    }
99}
100
101impl From<ParseBoolError> for RadosError {
102    fn from(err: ParseBoolError) -> RadosError {
103        RadosError::ParseBoolError(err)
104    }
105}
106
107impl From<ParseIntError> for RadosError {
108    fn from(err: ParseIntError) -> RadosError {
109        RadosError::ParseIntError(err)
110    }
111}
112
113impl From<SerdeJsonError> for RadosError {
114    fn from(err: SerdeJsonError) -> RadosError {
115        RadosError::SerdeError(err)
116    }
117}
118
119impl From<NulError> for RadosError {
120    fn from(err: NulError) -> RadosError {
121        RadosError::NulError(err)
122    }
123}
124
125impl From<FromUtf8Error> for RadosError {
126    fn from(err: FromUtf8Error) -> RadosError {
127        RadosError::FromUtf8Error(err)
128    }
129}
130impl From<IntoStringError> for RadosError {
131    fn from(err: IntoStringError) -> RadosError {
132        RadosError::IntoStringError(err)
133    }
134}
135impl From<Error> for RadosError {
136    fn from(err: Error) -> RadosError {
137        RadosError::IoError(err)
138    }
139}
140impl From<i32> for RadosError {
141    fn from(err: i32) -> RadosError {
142        RadosError::ApiError(nix::errno::Errno::from_i32(-err))
143    }
144}