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 parse SQL: {}", sql))]
195    ParseSql {
196        sql: String,
197        #[snafu(implicit)]
198        location: Location,
199        source: query::error::Error,
200    },
201
202    #[snafu(display("Failed to plan statement"))]
203    PlanStatement {
204        #[snafu(implicit)]
205        location: Location,
206        source: query::error::Error,
207    },
208
209    #[snafu(display("Failed to load layered config"))]
210    LoadLayeredConfig {
211        #[snafu(source(from(common_config::error::Error, Box::new)))]
212        source: Box<common_config::error::Error>,
213        #[snafu(implicit)]
214        location: Location,
215    },
216
217    #[snafu(display("Failed to connect to Etcd at {etcd_addr}"))]
218    ConnectEtcd {
219        etcd_addr: String,
220        #[snafu(source)]
221        error: etcd_client::Error,
222        #[snafu(implicit)]
223        location: Location,
224    },
225
226    #[snafu(display("Failed to serde json"))]
227    SerdeJson {
228        #[snafu(source)]
229        error: serde_json::error::Error,
230        #[snafu(implicit)]
231        location: Location,
232    },
233
234    #[snafu(display("Failed to run http request: {reason}"))]
235    HttpQuerySql {
236        reason: String,
237        #[snafu(source)]
238        error: reqwest::Error,
239        #[snafu(implicit)]
240        location: Location,
241    },
242
243    #[snafu(display("Empty result from output"))]
244    EmptyResult {
245        #[snafu(implicit)]
246        location: Location,
247    },
248
249    #[snafu(display("Failed to manipulate file"))]
250    FileIo {
251        #[snafu(implicit)]
252        location: Location,
253        #[snafu(source)]
254        error: std::io::Error,
255    },
256
257    #[snafu(display("Failed to create directory {}", dir))]
258    CreateDir {
259        dir: String,
260        #[snafu(source)]
261        error: std::io::Error,
262    },
263
264    #[snafu(display("Failed to spawn thread"))]
265    SpawnThread {
266        #[snafu(source)]
267        error: std::io::Error,
268    },
269
270    #[snafu(display("Other error"))]
271    Other {
272        source: BoxedError,
273        #[snafu(implicit)]
274        location: Location,
275    },
276
277    #[snafu(display("Failed to build runtime"))]
278    BuildRuntime {
279        #[snafu(implicit)]
280        location: Location,
281        source: common_runtime::error::Error,
282    },
283
284    #[snafu(display("Failed to get cache from cache registry: {}", name))]
285    CacheRequired {
286        #[snafu(implicit)]
287        location: Location,
288        name: String,
289    },
290
291    #[snafu(display("Failed to build cache registry"))]
292    BuildCacheRegistry {
293        #[snafu(implicit)]
294        location: Location,
295        source: cache::error::Error,
296    },
297
298    #[snafu(display("Failed to initialize meta client"))]
299    MetaClientInit {
300        #[snafu(implicit)]
301        location: Location,
302        source: meta_client::error::Error,
303    },
304
305    #[snafu(display("Cannot find schema {schema} in catalog {catalog}"))]
306    SchemaNotFound {
307        catalog: String,
308        schema: String,
309        #[snafu(implicit)]
310        location: Location,
311    },
312
313    #[snafu(display("Failed to build wal provider"))]
314    BuildWalProvider {
315        #[snafu(implicit)]
316        location: Location,
317        source: common_meta::error::Error,
318    },
319
320    #[snafu(display("Failed to build metadata kvbackend"))]
321    BuildMetadataKvbackend {
322        #[snafu(implicit)]
323        location: Location,
324        source: standalone::error::Error,
325    },
326
327    #[snafu(display("Failed to setup standalone plugins"))]
328    SetupStandalonePlugins {
329        #[snafu(implicit)]
330        location: Location,
331        source: standalone::error::Error,
332    },
333
334    #[snafu(display("Invalid WAL provider"))]
335    InvalidWalProvider {
336        #[snafu(implicit)]
337        location: Location,
338        source: common_wal::error::Error,
339    },
340}
341
342pub type Result<T> = std::result::Result<T, Error>;
343
344impl ErrorExt for Error {
345    fn status_code(&self) -> StatusCode {
346        match self {
347            Error::InitLogging { source, .. } => source.status_code(),
348            Error::StartDatanode { source, .. } => source.status_code(),
349            Error::StartFrontend { source, .. } => source.status_code(),
350            Error::ShutdownDatanode { source, .. } => source.status_code(),
351            Error::CloseMetadataKvBackend { source, .. } => source.status_code(),
352            Error::ShutdownFrontend { source, .. } => source.status_code(),
353            Error::StartMetaServer { source, .. } => source.status_code(),
354            Error::ShutdownMetaServer { source, .. } => source.status_code(),
355            Error::BuildMetaServer { source, .. } => source.status_code(),
356            Error::UnsupportedSelectorType { source, .. } => source.status_code(),
357            Error::BuildCli { source, .. } => source.status_code(),
358            Error::StartCli { source, .. } => source.status_code(),
359            Error::BuildMetadataKvbackend { source, .. } => source.status_code(),
360            Error::SetupStandalonePlugins { source, .. } => source.status_code(),
361
362            Error::InitMetadata { source, .. } | Error::InitDdlManager { source, .. } => {
363                source.status_code()
364            }
365
366            Error::MissingConfig { .. }
367            | Error::LoadLayeredConfig { .. }
368            | Error::IllegalConfig { .. }
369            | Error::InitTimezone { .. }
370            | Error::ConnectEtcd { .. }
371            | Error::CreateDir { .. }
372            | Error::EmptyResult { .. } => StatusCode::InvalidArguments,
373
374            Error::StartProcedureManager { source, .. }
375            | Error::StopProcedureManager { source, .. } => source.status_code(),
376            Error::BuildWalProvider { source, .. } | Error::StartWalProvider { source, .. } => {
377                source.status_code()
378            }
379            Error::HttpQuerySql { .. } => StatusCode::Internal,
380            Error::ParseSql { source, .. } | Error::PlanStatement { source, .. } => {
381                source.status_code()
382            }
383
384            Error::SerdeJson { .. }
385            | Error::FileIo { .. }
386            | Error::SpawnThread { .. }
387            | Error::InitTlsProvider { .. }
388            | Error::Daemonize { .. } => StatusCode::Unexpected,
389
390            Error::Other { source, .. } => source.status_code(),
391
392            Error::BuildRuntime { source, .. } => source.status_code(),
393
394            Error::CacheRequired { .. } | Error::BuildCacheRegistry { .. } => StatusCode::Internal,
395            Self::StartFlownode { source, .. } | Self::ShutdownFlownode { source, .. } => {
396                source.status_code()
397            }
398            Error::MetaClientInit { source, .. } => source.status_code(),
399            Error::SchemaNotFound { .. } => StatusCode::DatabaseNotFound,
400            Error::InvalidWalProvider { .. } => StatusCode::InvalidArguments,
401        }
402    }
403
404    fn as_any(&self) -> &dyn Any {
405        self
406    }
407}