Skip to main content

openstack_cli_core/
error.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14//! CLI Errors
15
16use indicatif;
17use reqwest;
18use thiserror::Error;
19
20/// CLI error type
21#[derive(Debug, Error)]
22#[non_exhaustive]
23pub enum OpenStackCliError {
24    /// Json serialization error.
25    #[error("failed to serialize data to json: {}", source)]
26    SerializeJson {
27        /// The source of the error.
28        #[from]
29        source: serde_json::Error,
30    },
31
32    /// Json deserialization error.
33    #[error(
34        "failed to deserialize data to json. Try using `-o json` to still see the data. \n\t{}",
35        data
36    )]
37    DeserializeJson {
38        /// The source of the error.
39        source: serde_json::Error,
40        /// Source json data
41        data: String,
42    },
43
44    /// OpenStack Auth error.
45    #[error("authentication error")]
46    Auth {
47        /// The source of the error.
48        source: openstack_sdk_core::OpenStackError,
49    },
50    /// Re-scope error.
51    #[error("error changing scope to {:?}", scope)]
52    ReScope {
53        /// Target scope.
54        scope: openstack_sdk_auth_core::authtoken_scope::AuthTokenScope,
55        /// The source of the error.
56        source: openstack_sdk_core::OpenStackError,
57    },
58
59    /// SDK error.
60    #[error(transparent)]
61    OpenStackSDK {
62        /// The source of the error.
63        #[from]
64        source: openstack_sdk_core::OpenStackError,
65    },
66    /// OpenStack API error.
67    #[error(transparent)]
68    OpenStackApi {
69        /// The source of the error.
70        #[from]
71        source: openstack_sdk_core::api::ApiError<openstack_sdk_core::RestError>,
72    },
73
74    /// Configuration error.
75    #[error(transparent)]
76    CliConfig {
77        /// The source of the error.
78        #[from]
79        source: crate::config::ConfigError,
80    },
81
82    /// Configuration error.
83    #[error(transparent)]
84    CloudConfig {
85        /// The source of the error.
86        #[from]
87        source: openstack_sdk_core::config::ConfigError,
88    },
89
90    /// OpenStack Service Catalog error.
91    #[error(transparent)]
92    OpenStackCatalog {
93        /// The source of the error.
94        #[from]
95        source: openstack_sdk_core::catalog::CatalogError,
96    },
97
98    /// No subcommands.
99    #[error("command has no subcommands")]
100    NoSubcommands,
101
102    /// Resource is not found.
103    #[error("resource not found")]
104    ResourceNotFound,
105
106    /// Resource identifier is not unique.
107    #[error("cannot find resource by identifier")]
108    IdNotUnique,
109
110    /// Resource attribute is not present.
111    #[error("cannot find resource attribute {0}")]
112    ResourceAttributeMissing(String),
113
114    /// Resource attribute is not string.
115    #[error("resource attribute {0} is not a string")]
116    ResourceAttributeNotString(String),
117
118    /// IO error.
119    #[error("IO error: {}", source)]
120    IO {
121        /// The source of the error.
122        #[from]
123        source: std::io::Error,
124    },
125    /// Reqwest library error.
126    #[error("reqwest error: {}", source)]
127    Reqwest {
128        /// The source of the error.
129        #[from]
130        source: reqwest::Error,
131    },
132    /// Clap library error.
133    #[error("argument parsing error: {}", source)]
134    Clap {
135        /// The source of the error.
136        #[from]
137        source: clap::error::Error,
138    },
139    /// Indicativ library error.
140    #[error("indicativ error: {}", source)]
141    Idinticatif {
142        /// The source of the error.
143        #[from]
144        source: indicatif::style::TemplateError,
145    },
146    /// Endpoint builder error.
147    #[error("OpenStackSDK endpoint builder error: `{0}`")]
148    EndpointBuild(String),
149
150    /// Connection error.
151    #[error("cloud connection `{0:?}` cannot be found")]
152    ConnectionNotFound(String),
153
154    /// Invalid header name.
155    #[error("invalid header name `{}`", source)]
156    InvalidHeaderName {
157        /// The source of the error.
158        #[from]
159        source: http::header::InvalidHeaderName,
160    },
161
162    /// Invalid header value.
163    #[error("invalid header value `{}`", source)]
164    InvalidHeaderValue {
165        /// The source of the error.
166        #[from]
167        source: http::header::InvalidHeaderValue,
168    },
169
170    /// Invalid URL.
171    #[error("invalid url: {}", source)]
172    InvalidUri {
173        /// The source of the error.
174        #[from]
175        source: http::uri::InvalidUri,
176    },
177
178    /// User interaction using dialoguer crate failed
179    #[error("dialoguer error `{}`", source)]
180    DialoguerError {
181        /// The source of the error.
182        #[from]
183        source: dialoguer::Error,
184    },
185
186    /// Input parameters
187    #[error("input parameters error: {0}")]
188    InputParameters(String),
189
190    /// Base64 decoding error.
191    #[error(transparent)]
192    Base64Decode(#[from] base64::DecodeError),
193
194    /// Re-authorization not possible without active authentication.
195    #[error("valid authentication is missing to be able to rescope the session")]
196    MissingValidAuthenticationForRescope,
197
198    /// URL parsing error
199    #[error(transparent)]
200    UrlParse {
201        /// The source of the error.
202        #[from]
203        source: url::ParseError,
204    },
205
206    /// Others.
207    #[error(transparent)]
208    Other(#[from] eyre::Report),
209}
210
211impl OpenStackCliError {
212    /// Build a deserialization error
213    pub fn deserialize(error: serde_json::Error, data: String) -> Self {
214        Self::DeserializeJson {
215            source: error,
216            data,
217        }
218    }
219}