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 options allocator"))]
68    StartWalOptionsAllocator {
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 start flownode"))]
89    StartFlownode {
90        #[snafu(implicit)]
91        location: Location,
92        source: flow::Error,
93    },
94
95    #[snafu(display("Failed to shutdown flownode"))]
96    ShutdownFlownode {
97        #[snafu(implicit)]
98        location: Location,
99        source: flow::Error,
100    },
101
102    #[snafu(display("Servers error"))]
103    Servers {
104        #[snafu(implicit)]
105        location: Location,
106        source: servers::error::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("Unsupported selector type: {}", selector_type))]
173    UnsupportedSelectorType {
174        selector_type: String,
175        #[snafu(implicit)]
176        location: Location,
177        source: meta_srv::error::Error,
178    },
179
180    #[snafu(display("Failed to parse SQL: {}", sql))]
181    ParseSql {
182        sql: String,
183        #[snafu(implicit)]
184        location: Location,
185        source: query::error::Error,
186    },
187
188    #[snafu(display("Failed to plan statement"))]
189    PlanStatement {
190        #[snafu(implicit)]
191        location: Location,
192        source: query::error::Error,
193    },
194
195    #[snafu(display("Failed to load layered config"))]
196    LoadLayeredConfig {
197        #[snafu(source(from(common_config::error::Error, Box::new)))]
198        source: Box<common_config::error::Error>,
199        #[snafu(implicit)]
200        location: Location,
201    },
202
203    #[snafu(display("Failed to connect to Etcd at {etcd_addr}"))]
204    ConnectEtcd {
205        etcd_addr: String,
206        #[snafu(source)]
207        error: etcd_client::Error,
208        #[snafu(implicit)]
209        location: Location,
210    },
211
212    #[snafu(display("Failed to serde json"))]
213    SerdeJson {
214        #[snafu(source)]
215        error: serde_json::error::Error,
216        #[snafu(implicit)]
217        location: Location,
218    },
219
220    #[snafu(display("Failed to run http request: {reason}"))]
221    HttpQuerySql {
222        reason: String,
223        #[snafu(source)]
224        error: reqwest::Error,
225        #[snafu(implicit)]
226        location: Location,
227    },
228
229    #[snafu(display("Empty result from output"))]
230    EmptyResult {
231        #[snafu(implicit)]
232        location: Location,
233    },
234
235    #[snafu(display("Failed to manipulate file"))]
236    FileIo {
237        #[snafu(implicit)]
238        location: Location,
239        #[snafu(source)]
240        error: std::io::Error,
241    },
242
243    #[snafu(display("Failed to create directory {}", dir))]
244    CreateDir {
245        dir: String,
246        #[snafu(source)]
247        error: std::io::Error,
248    },
249
250    #[snafu(display("Failed to spawn thread"))]
251    SpawnThread {
252        #[snafu(source)]
253        error: std::io::Error,
254    },
255
256    #[snafu(display("Other error"))]
257    Other {
258        source: BoxedError,
259        #[snafu(implicit)]
260        location: Location,
261    },
262
263    #[snafu(display("Failed to build runtime"))]
264    BuildRuntime {
265        #[snafu(implicit)]
266        location: Location,
267        source: common_runtime::error::Error,
268    },
269
270    #[snafu(display("Failed to get cache from cache registry: {}", name))]
271    CacheRequired {
272        #[snafu(implicit)]
273        location: Location,
274        name: String,
275    },
276
277    #[snafu(display("Failed to build cache registry"))]
278    BuildCacheRegistry {
279        #[snafu(implicit)]
280        location: Location,
281        source: cache::error::Error,
282    },
283
284    #[snafu(display("Failed to initialize meta client"))]
285    MetaClientInit {
286        #[snafu(implicit)]
287        location: Location,
288        source: meta_client::error::Error,
289    },
290
291    #[snafu(display("Cannot find schema {schema} in catalog {catalog}"))]
292    SchemaNotFound {
293        catalog: String,
294        schema: String,
295        #[snafu(implicit)]
296        location: Location,
297    },
298
299    #[snafu(display("Failed to build wal options allocator"))]
300    BuildWalOptionsAllocator {
301        #[snafu(implicit)]
302        location: Location,
303        source: common_meta::error::Error,
304    },
305}
306
307pub type Result<T> = std::result::Result<T, Error>;
308
309impl ErrorExt for Error {
310    fn status_code(&self) -> StatusCode {
311        match self {
312            Error::StartDatanode { source, .. } => source.status_code(),
313            Error::StartFrontend { source, .. } => source.status_code(),
314            Error::ShutdownDatanode { source, .. } => source.status_code(),
315            Error::ShutdownFrontend { source, .. } => source.status_code(),
316            Error::StartMetaServer { source, .. } => source.status_code(),
317            Error::ShutdownMetaServer { source, .. } => source.status_code(),
318            Error::Servers { source, .. } => source.status_code(),
319            Error::BuildMetaServer { source, .. } => source.status_code(),
320            Error::UnsupportedSelectorType { source, .. } => source.status_code(),
321            Error::BuildCli { source, .. } => source.status_code(),
322            Error::StartCli { source, .. } => source.status_code(),
323
324            Error::InitMetadata { source, .. } | Error::InitDdlManager { source, .. } => {
325                source.status_code()
326            }
327
328            Error::MissingConfig { .. }
329            | Error::LoadLayeredConfig { .. }
330            | Error::IllegalConfig { .. }
331            | Error::InitTimezone { .. }
332            | Error::ConnectEtcd { .. }
333            | Error::CreateDir { .. }
334            | Error::EmptyResult { .. } => StatusCode::InvalidArguments,
335
336            Error::StartProcedureManager { source, .. }
337            | Error::StopProcedureManager { source, .. } => source.status_code(),
338            Error::BuildWalOptionsAllocator { source, .. }
339            | Error::StartWalOptionsAllocator { source, .. } => source.status_code(),
340            Error::HttpQuerySql { .. } => StatusCode::Internal,
341            Error::ParseSql { source, .. } | Error::PlanStatement { source, .. } => {
342                source.status_code()
343            }
344
345            Error::SerdeJson { .. }
346            | Error::FileIo { .. }
347            | Error::SpawnThread { .. }
348            | Error::InitTlsProvider { .. } => StatusCode::Unexpected,
349
350            Error::Other { source, .. } => source.status_code(),
351
352            Error::BuildRuntime { source, .. } => source.status_code(),
353
354            Error::CacheRequired { .. } | Error::BuildCacheRegistry { .. } => StatusCode::Internal,
355            Self::StartFlownode { source, .. } | Self::ShutdownFlownode { source, .. } => {
356                source.status_code()
357            }
358            Error::MetaClientInit { source, .. } => source.status_code(),
359            Error::SchemaNotFound { .. } => StatusCode::DatabaseNotFound,
360        }
361    }
362
363    fn as_any(&self) -> &dyn Any {
364        self
365    }
366}