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 install ring crypto provider: {}", msg))]
27    InitTlsProvider {
28        #[snafu(implicit)]
29        location: Location,
30        msg: String,
31    },
32    #[snafu(display("Failed to create default catalog and schema"))]
33    InitMetadata {
34        #[snafu(implicit)]
35        location: Location,
36        source: common_meta::error::Error,
37    },
38
39    #[snafu(display("Failed to init DDL manager"))]
40    InitDdlManager {
41        #[snafu(implicit)]
42        location: Location,
43        source: common_meta::error::Error,
44    },
45
46    #[snafu(display("Failed to init default timezone"))]
47    InitTimezone {
48        #[snafu(implicit)]
49        location: Location,
50        source: common_time::error::Error,
51    },
52
53    #[snafu(display("Failed to start procedure manager"))]
54    StartProcedureManager {
55        #[snafu(implicit)]
56        location: Location,
57        source: common_procedure::error::Error,
58    },
59
60    #[snafu(display("Failed to stop procedure manager"))]
61    StopProcedureManager {
62        #[snafu(implicit)]
63        location: Location,
64        source: common_procedure::error::Error,
65    },
66
67    #[snafu(display("Failed to start wal provider"))]
68    StartWalProvider {
69        #[snafu(implicit)]
70        location: Location,
71        source: common_meta::error::Error,
72    },
73
74    #[snafu(display("Failed to start datanode"))]
75    StartDatanode {
76        #[snafu(implicit)]
77        location: Location,
78        source: datanode::error::Error,
79    },
80
81    #[snafu(display("Failed to shutdown datanode"))]
82    ShutdownDatanode {
83        #[snafu(implicit)]
84        location: Location,
85        source: datanode::error::Error,
86    },
87
88    #[snafu(display("Failed to close the metadata kv backend"))]
89    CloseMetadataKvBackend {
90        #[snafu(implicit)]
91        location: Location,
92        source: common_meta::error::Error,
93    },
94
95    #[snafu(display("Failed to start flownode"))]
96    StartFlownode {
97        #[snafu(implicit)]
98        location: Location,
99        source: flow::Error,
100    },
101
102    #[snafu(display("Failed to shutdown flownode"))]
103    ShutdownFlownode {
104        #[snafu(implicit)]
105        location: Location,
106        source: flow::Error,
107    },
108
109    #[snafu(display("Failed to start frontend"))]
110    StartFrontend {
111        #[snafu(implicit)]
112        location: Location,
113        source: frontend::error::Error,
114    },
115
116    #[snafu(display("Failed to shutdown frontend"))]
117    ShutdownFrontend {
118        #[snafu(implicit)]
119        location: Location,
120        source: frontend::error::Error,
121    },
122
123    #[snafu(display("Failed to build cli"))]
124    BuildCli {
125        #[snafu(implicit)]
126        location: Location,
127        source: BoxedError,
128    },
129
130    #[snafu(display("Failed to start cli"))]
131    StartCli {
132        #[snafu(implicit)]
133        location: Location,
134        source: BoxedError,
135    },
136
137    #[snafu(display("Failed to build meta server"))]
138    BuildMetaServer {
139        #[snafu(implicit)]
140        location: Location,
141        source: meta_srv::error::Error,
142    },
143
144    #[snafu(display("Failed to start meta server"))]
145    StartMetaServer {
146        #[snafu(implicit)]
147        location: Location,
148        source: meta_srv::error::Error,
149    },
150
151    #[snafu(display("Failed to shutdown meta server"))]
152    ShutdownMetaServer {
153        #[snafu(implicit)]
154        location: Location,
155        source: meta_srv::error::Error,
156    },
157
158    #[snafu(display("Missing config, msg: {}", msg))]
159    MissingConfig {
160        msg: String,
161        #[snafu(implicit)]
162        location: Location,
163    },
164
165    #[snafu(display("Illegal config: {}", msg))]
166    IllegalConfig {
167        msg: String,
168        #[snafu(implicit)]
169        location: Location,
170    },
171
172    #[snafu(display("Failed to daemonize: {msg}"))]
173    Daemonize {
174        msg: String,
175        #[snafu(implicit)]
176        location: Location,
177    },
178
179    #[snafu(display("Unsupported selector type: {}", selector_type))]
180    UnsupportedSelectorType {
181        selector_type: String,
182        #[snafu(implicit)]
183        location: Location,
184        source: meta_srv::error::Error,
185    },
186
187    #[snafu(display("Failed to parse SQL: {}", sql))]
188    ParseSql {
189        sql: String,
190        #[snafu(implicit)]
191        location: Location,
192        source: query::error::Error,
193    },
194
195    #[snafu(display("Failed to plan statement"))]
196    PlanStatement {
197        #[snafu(implicit)]
198        location: Location,
199        source: query::error::Error,
200    },
201
202    #[snafu(display("Failed to load layered config"))]
203    LoadLayeredConfig {
204        #[snafu(source(from(common_config::error::Error, Box::new)))]
205        source: Box<common_config::error::Error>,
206        #[snafu(implicit)]
207        location: Location,
208    },
209
210    #[snafu(display("Failed to connect to Etcd at {etcd_addr}"))]
211    ConnectEtcd {
212        etcd_addr: String,
213        #[snafu(source)]
214        error: etcd_client::Error,
215        #[snafu(implicit)]
216        location: Location,
217    },
218
219    #[snafu(display("Failed to serde json"))]
220    SerdeJson {
221        #[snafu(source)]
222        error: serde_json::error::Error,
223        #[snafu(implicit)]
224        location: Location,
225    },
226
227    #[snafu(display("Failed to run http request: {reason}"))]
228    HttpQuerySql {
229        reason: String,
230        #[snafu(source)]
231        error: reqwest::Error,
232        #[snafu(implicit)]
233        location: Location,
234    },
235
236    #[snafu(display("Empty result from output"))]
237    EmptyResult {
238        #[snafu(implicit)]
239        location: Location,
240    },
241
242    #[snafu(display("Failed to manipulate file"))]
243    FileIo {
244        #[snafu(implicit)]
245        location: Location,
246        #[snafu(source)]
247        error: std::io::Error,
248    },
249
250    #[snafu(display("Failed to create directory {}", dir))]
251    CreateDir {
252        dir: String,
253        #[snafu(source)]
254        error: std::io::Error,
255    },
256
257    #[snafu(display("Failed to spawn thread"))]
258    SpawnThread {
259        #[snafu(source)]
260        error: std::io::Error,
261    },
262
263    #[snafu(display("Other error"))]
264    Other {
265        source: BoxedError,
266        #[snafu(implicit)]
267        location: Location,
268    },
269
270    #[snafu(display("Failed to build runtime"))]
271    BuildRuntime {
272        #[snafu(implicit)]
273        location: Location,
274        source: common_runtime::error::Error,
275    },
276
277    #[snafu(display("Failed to get cache from cache registry: {}", name))]
278    CacheRequired {
279        #[snafu(implicit)]
280        location: Location,
281        name: String,
282    },
283
284    #[snafu(display("Failed to build cache registry"))]
285    BuildCacheRegistry {
286        #[snafu(implicit)]
287        location: Location,
288        source: cache::error::Error,
289    },
290
291    #[snafu(display("Failed to initialize meta client"))]
292    MetaClientInit {
293        #[snafu(implicit)]
294        location: Location,
295        source: meta_client::error::Error,
296    },
297
298    #[snafu(display("Cannot find schema {schema} in catalog {catalog}"))]
299    SchemaNotFound {
300        catalog: String,
301        schema: String,
302        #[snafu(implicit)]
303        location: Location,
304    },
305
306    #[snafu(display("Failed to build wal provider"))]
307    BuildWalProvider {
308        #[snafu(implicit)]
309        location: Location,
310        source: common_meta::error::Error,
311    },
312
313    #[snafu(display("Failed to build metadata kvbackend"))]
314    BuildMetadataKvbackend {
315        #[snafu(implicit)]
316        location: Location,
317        source: standalone::error::Error,
318    },
319
320    #[snafu(display("Failed to setup standalone plugins"))]
321    SetupStandalonePlugins {
322        #[snafu(implicit)]
323        location: Location,
324        source: standalone::error::Error,
325    },
326
327    #[snafu(display("Invalid WAL provider"))]
328    InvalidWalProvider {
329        #[snafu(implicit)]
330        location: Location,
331        source: common_wal::error::Error,
332    },
333}
334
335pub type Result<T> = std::result::Result<T, Error>;
336
337impl ErrorExt for Error {
338    fn status_code(&self) -> StatusCode {
339        match self {
340            Error::StartDatanode { source, .. } => source.status_code(),
341            Error::StartFrontend { source, .. } => source.status_code(),
342            Error::ShutdownDatanode { source, .. } => source.status_code(),
343            Error::CloseMetadataKvBackend { source, .. } => source.status_code(),
344            Error::ShutdownFrontend { source, .. } => source.status_code(),
345            Error::StartMetaServer { source, .. } => source.status_code(),
346            Error::ShutdownMetaServer { source, .. } => source.status_code(),
347            Error::BuildMetaServer { source, .. } => source.status_code(),
348            Error::UnsupportedSelectorType { source, .. } => source.status_code(),
349            Error::BuildCli { source, .. } => source.status_code(),
350            Error::StartCli { source, .. } => source.status_code(),
351            Error::BuildMetadataKvbackend { source, .. } => source.status_code(),
352            Error::SetupStandalonePlugins { source, .. } => source.status_code(),
353
354            Error::InitMetadata { source, .. } | Error::InitDdlManager { source, .. } => {
355                source.status_code()
356            }
357
358            Error::MissingConfig { .. }
359            | Error::LoadLayeredConfig { .. }
360            | Error::IllegalConfig { .. }
361            | Error::InitTimezone { .. }
362            | Error::ConnectEtcd { .. }
363            | Error::CreateDir { .. }
364            | Error::EmptyResult { .. } => StatusCode::InvalidArguments,
365
366            Error::StartProcedureManager { source, .. }
367            | Error::StopProcedureManager { source, .. } => source.status_code(),
368            Error::BuildWalProvider { source, .. } | Error::StartWalProvider { source, .. } => {
369                source.status_code()
370            }
371            Error::HttpQuerySql { .. } => StatusCode::Internal,
372            Error::ParseSql { source, .. } | Error::PlanStatement { source, .. } => {
373                source.status_code()
374            }
375
376            Error::SerdeJson { .. }
377            | Error::FileIo { .. }
378            | Error::SpawnThread { .. }
379            | Error::InitTlsProvider { .. }
380            | Error::Daemonize { .. } => StatusCode::Unexpected,
381
382            Error::Other { source, .. } => source.status_code(),
383
384            Error::BuildRuntime { source, .. } => source.status_code(),
385
386            Error::CacheRequired { .. } | Error::BuildCacheRegistry { .. } => StatusCode::Internal,
387            Self::StartFlownode { source, .. } | Self::ShutdownFlownode { source, .. } => {
388                source.status_code()
389            }
390            Error::MetaClientInit { source, .. } => source.status_code(),
391            Error::SchemaNotFound { .. } => StatusCode::DatabaseNotFound,
392            Error::InvalidWalProvider { .. } => StatusCode::InvalidArguments,
393        }
394    }
395
396    fn as_any(&self) -> &dyn Any {
397        self
398    }
399}