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
210
211
212
213
// 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.
//
// SPDX-License-Identifier: Apache-2.0

//! Errors module

use std::any;
use thiserror::Error;

use crate::api;
use crate::auth::{
    authtoken::AuthTokenError, authtoken_scope::AuthTokenScopeError, v3websso::WebSsoError,
    AuthError,
};
use crate::catalog::CatalogError;
use crate::config::ConfigError;

/// Rest errors that may happen during API communication
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum RestError {
    /// Auth error
    #[error("error setting auth header: {}", source)]
    AuthError {
        /// The source of the error.
        #[from]
        source: AuthError,
    },

    /// API communication error
    #[error("communication with openstack: {}", source)]
    Communication {
        /// The source of the error.
        #[from]
        source: reqwest::Error,
    },

    /// HTTP error
    #[error("`http` error: {}", source)]
    Http {
        /// The source of the error.
        #[from]
        source: http::Error,
    },
}

/// OpenStack Client error
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum OpenStackError {
    /// URL parse error
    #[error("failed to parse url: {}", source)]
    UrlParse {
        /// The source of the error.
        #[from]
        source: url::ParseError,
    },

    /// Authentication error
    #[error("No authentication information available")]
    NoAuth,

    /// Authentication error
    #[error("error setting auth header: {}", source)]
    AuthError {
        /// The source of the error.
        #[from]
        source: AuthError,
    },

    /// API Communication error
    #[error("communication with cloud: {}", source)]
    Communication {
        /// The source of the error.
        #[from]
        source: reqwest::Error,
    },

    /// HTTP error
    #[error("openstack HTTP error: {}", status)]
    Http { status: reqwest::StatusCode },

    /// No response
    #[error("no response from API")]
    NoResponse {},

    /// Json deserialization error
    #[error("could not parse {} data from JSON: {}", typename, source)]
    DataType {
        /// The source of the error.
        #[source]
        source: serde_json::Error,
        /// type name that could not be parsed
        typename: &'static str,
    },

    /// API error
    #[error("api error: {}", source)]
    Api {
        /// The source of the error.
        #[from]
        source: api::ApiError<RestError>,
    },

    /// Service catalog error
    #[error("service_catalog error: {}", source)]
    Catalog {
        /// The source of the error.
        #[from]
        /// error source
        source: CatalogError,
    },

    #[error("configuration error: {}", source)]
    ConfigError {
        /// The source of the error.
        #[from]
        source: ConfigError,
    },

    /// Service version discovery error
    #[error(
        "`{}` endpoint version discovery error:\n\tUrl: {}\n\tMessage: {}",
        service,
        url,
        msg
    )]
    Discovery {
        service: String,
        url: String,
        msg: String,
    },

    /// Interactive mode required
    #[error(
        "Interactive mode is required but not available (running `echo foo | osc`?). {}",
        msg
    )]
    NonInteractiveMode { msg: String },

    /// JSON deserialization from OpenStack failed.
    #[error("could not parse JSON response: {}", source)]
    Json {
        /// The source of the error.
        #[from]
        source: serde_json::Error,
    },
    /// IO error.
    #[error("IO error: {}\n\tPath: {}", source, path)]
    IO {
        /// The source of the error.
        source: std::io::Error,
        path: String,
    },
}

impl OpenStackError {
    pub fn http(status: reqwest::StatusCode) -> Self {
        OpenStackError::Http { status }
    }

    pub fn no_response() -> Self {
        OpenStackError::NoResponse {}
    }

    pub fn data_type<T>(source: serde_json::Error) -> Self {
        OpenStackError::DataType {
            source,
            typename: any::type_name::<T>(),
        }
    }

    pub fn catalog(source: CatalogError) -> Self {
        OpenStackError::Catalog { source }
    }
}

// Explicitly implement From to easier propagate nested errors
impl From<AuthTokenError> for OpenStackError {
    fn from(source: AuthTokenError) -> Self {
        Self::AuthError {
            source: AuthError::AuthToken { source },
        }
    }
}

impl From<AuthTokenScopeError> for OpenStackError {
    fn from(source: AuthTokenScopeError) -> Self {
        Self::AuthError {
            source: source.into(),
        }
    }
}

impl From<WebSsoError> for OpenStackError {
    fn from(source: WebSsoError) -> Self {
        Self::AuthError {
            source: source.into(),
        }
    }
}

pub type OpenStackResult<T> = Result<T, OpenStackError>;