Skip to main content

cmd/
error.rs

1// Copyright 2023 Greptime Team
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.
14
15use std::any::Any;
16
17use common_error::ext::{BoxedError, ErrorExt};
18use common_error::status_code::StatusCode;
19use common_macro::stack_trace_debug;
20use snafu::{Location, Snafu};
21
22#[derive(Snafu)]
23#[snafu(visibility(pub))]
24#[stack_trace_debug]
25pub enum Error {
26    #[snafu(display("Failed to initialize logging"))]
27    InitLogging {
28        #[snafu(implicit)]
29        location: Location,
30        source: common_telemetry::logging::InitLoggingError,
31    },
32
33    #[snafu(display("Failed to install ring crypto provider: {}", msg))]
34    InitTlsProvider {
35        #[snafu(implicit)]
36        location: Location,
37        msg: String,
38    },
39    #[snafu(display("Failed to create default catalog and schema"))]
40    InitMetadata {
41        #[snafu(implicit)]
42        location: Location,
43        source: common_meta::error::Error,
44    },
45
46    #[snafu(display("Failed to init DDL manager"))]
47    InitDdlManager {
48        #[snafu(implicit)]
49        location: Location,
50        source: common_meta::error::Error,
51    },
52
53    #[snafu(display("Failed to init default timezone"))]
54    InitTimezone {
55        #[snafu(implicit)]
56        location: Location,
57        source: common_time::error::Error,
58    },
59
60    #[snafu(display("Failed to start procedure manager"))]
61    StartProcedureManager {
62        #[snafu(implicit)]
63        location: Location,
64        source: common_procedure::error::Error,
65    },
66
67    #[snafu(display("Failed to stop procedure manager"))]
68    StopProcedureManager {
69        #[snafu(implicit)]
70        location: Location,
71        source: common_procedure::error::Error,
72    },
73
74    #[snafu(display("Failed to start wal provider"))]
75    StartWalProvider {
76        #[snafu(implicit)]
77        location: Location,
78        source: common_meta::error::Error,
79    },
80
81    #[snafu(display("Failed to start datanode"))]
82    StartDatanode {
83        #[snafu(implicit)]
84        location: Location,
85        source: datanode::error::Error,
86    },
87
88    #[snafu(display("Failed to shutdown datanode"))]
89    ShutdownDatanode {
90        #[snafu(implicit)]
91        location: Location,
92        source: datanode::error::Error,
93    },
94
95    #[snafu(display("Failed to close the metadata kv backend"))]
96    CloseMetadataKvBackend {
97        #[snafu(implicit)]
98        location: Location,
99        source: common_meta::error::Error,
100    },
101
102    #[snafu(display("Failed to start flownode"))]
103    StartFlownode {
104        #[snafu(implicit)]
105        location: Location,
106        source: flow::Error,
107    },
108
109    #[snafu(display("Failed to shutdown flownode"))]
110    ShutdownFlownode {
111        #[snafu(implicit)]
112        location: Location,
113        source: flow::Error,
114    },
115
116    #[snafu(display("Failed to start frontend"))]
117    StartFrontend {
118        #[snafu(implicit)]
119        location: Location,
120        source: frontend::error::Error,
121    },
122
123    #[snafu(display("Failed to shutdown frontend"))]
124    ShutdownFrontend {
125        #[snafu(implicit)]
126        location: Location,
127        source: frontend::error::Error,
128    },
129
130    #[snafu(display("Failed to build cli"))]
131    BuildCli {
132        #[snafu(implicit)]
133        location: Location,
134        source: BoxedError,
135    },
136
137    #[snafu(display("Failed to start cli"))]
138    StartCli {
139        #[snafu(implicit)]
140        location: Location,
141        source: BoxedError,
142    },
143
144    #[snafu(display("Failed to build meta server"))]
145    BuildMetaServer {
146        #[snafu(implicit)]
147        location: Location,
148        source: meta_srv::error::Error,
149    },
150
151    #[snafu(display("Failed to start meta server"))]
152    StartMetaServer {
153        #[snafu(implicit)]
154        location: Location,
155        source: meta_srv::error::Error,
156    },
157
158    #[snafu(display("Failed to shutdown meta server"))]
159    ShutdownMetaServer {
160        #[snafu(implicit)]
161        location: Location,
162        source: meta_srv::error::Error,
163    },
164
165    #[snafu(display("Missing config, msg: {}", msg))]
166    MissingConfig {
167        msg: String,
168        #[snafu(implicit)]
169        location: Location,
170    },
171
172    #[snafu(display("Illegal config: {}", msg))]
173    IllegalConfig {
174        msg: String,
175        #[snafu(implicit)]
176        location: Location,
177    },
178
179    #[snafu(display("Failed to daemonize: {msg}"))]
180    Daemonize {
181        msg: String,
182        #[snafu(implicit)]
183        location: Location,
184    },
185
186    #[snafu(display("Unsupported selector type: {}", selector_type))]
187    UnsupportedSelectorType {
188        selector_type: String,
189        #[snafu(implicit)]
190        location: Location,
191        source: meta_srv::error::Error,
192    },
193
194    #[snafu(display("Failed to load layered config"))]
195    LoadLayeredConfig {
196        #[snafu(source(from(common_config::error::Error, Box::new)))]
197        source: Box<common_config::error::Error>,
198        #[snafu(implicit)]
199        location: Location,
200    },
201
202    #[snafu(display("Failed to serde json"))]
203    SerdeJson {
204        #[snafu(source)]
205        error: serde_json::error::Error,
206        #[snafu(implicit)]
207        location: Location,
208    },
209
210    #[snafu(display("Failed to manipulate file"))]
211    FileIo {
212        #[snafu(implicit)]
213        location: Location,
214        #[snafu(source)]
215        error: std::io::Error,
216    },
217
218    #[snafu(display("Failed to create directory {}", dir))]
219    CreateDir {
220        dir: String,
221        #[snafu(source)]
222        error: std::io::Error,
223    },
224
225    #[snafu(display("Failed to spawn thread"))]
226    SpawnThread {
227        #[snafu(source)]
228        error: std::io::Error,
229    },
230
231    #[snafu(display("Other error"))]
232    Other {
233        source: BoxedError,
234        #[snafu(implicit)]
235        location: Location,
236    },
237
238    #[snafu(display("Failed to build cache registry"))]
239    BuildCacheRegistry {
240        #[snafu(implicit)]
241        location: Location,
242        source: cache::error::Error,
243    },
244
245    #[snafu(display("Failed to initialize meta client"))]
246    MetaClientInit {
247        #[snafu(implicit)]
248        location: Location,
249        source: meta_client::error::Error,
250    },
251
252    #[snafu(display("Failed to build wal provider"))]
253    BuildWalProvider {
254        #[snafu(implicit)]
255        location: Location,
256        source: common_meta::error::Error,
257    },
258
259    #[snafu(display("Failed to build metadata kvbackend"))]
260    BuildMetadataKvbackend {
261        #[snafu(implicit)]
262        location: Location,
263        source: standalone::error::Error,
264    },
265
266    #[snafu(display("Failed to setup standalone plugins"))]
267    SetupStandalonePlugins {
268        #[snafu(implicit)]
269        location: Location,
270        source: standalone::error::Error,
271    },
272
273    #[snafu(display("Invalid WAL provider"))]
274    InvalidWalProvider {
275        #[snafu(implicit)]
276        location: Location,
277        source: common_wal::error::Error,
278    },
279}
280
281pub type Result<T> = std::result::Result<T, Error>;
282
283impl ErrorExt for Error {
284    fn status_code(&self) -> StatusCode {
285        match self {
286            Error::InitLogging { source, .. } => source.status_code(),
287            Error::StartDatanode { source, .. } => source.status_code(),
288            Error::StartFrontend { source, .. } => source.status_code(),
289            Error::ShutdownDatanode { source, .. } => source.status_code(),
290            Error::CloseMetadataKvBackend { source, .. } => source.status_code(),
291            Error::ShutdownFrontend { source, .. } => source.status_code(),
292            Error::StartMetaServer { source, .. } => source.status_code(),
293            Error::ShutdownMetaServer { source, .. } => source.status_code(),
294            Error::BuildMetaServer { source, .. } => source.status_code(),
295            Error::UnsupportedSelectorType { source, .. } => source.status_code(),
296            Error::BuildCli { source, .. } => source.status_code(),
297            Error::StartCli { source, .. } => source.status_code(),
298            Error::BuildMetadataKvbackend { source, .. } => source.status_code(),
299            Error::SetupStandalonePlugins { source, .. } => source.status_code(),
300
301            Error::InitMetadata { source, .. } | Error::InitDdlManager { source, .. } => {
302                source.status_code()
303            }
304
305            Error::MissingConfig { .. }
306            | Error::LoadLayeredConfig { .. }
307            | Error::IllegalConfig { .. }
308            | Error::InitTimezone { .. }
309            | Error::CreateDir { .. } => StatusCode::InvalidArguments,
310
311            Error::StartProcedureManager { source, .. }
312            | Error::StopProcedureManager { source, .. } => source.status_code(),
313            Error::BuildWalProvider { source, .. } | Error::StartWalProvider { source, .. } => {
314                source.status_code()
315            }
316
317            Error::SerdeJson { .. }
318            | Error::FileIo { .. }
319            | Error::SpawnThread { .. }
320            | Error::InitTlsProvider { .. }
321            | Error::Daemonize { .. } => StatusCode::Unexpected,
322
323            Error::Other { source, .. } => source.status_code(),
324
325            Error::BuildCacheRegistry { .. } => StatusCode::Internal,
326            Self::StartFlownode { source, .. } | Self::ShutdownFlownode { source, .. } => {
327                source.status_code()
328            }
329            Error::MetaClientInit { source, .. } => source.status_code(),
330            Error::InvalidWalProvider { .. } => StatusCode::InvalidArguments,
331        }
332    }
333
334    fn as_any(&self) -> &dyn Any {
335        self
336    }
337}