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("Failed to start frontend"))]
103    StartFrontend {
104        #[snafu(implicit)]
105        location: Location,
106        source: frontend::error::Error,
107    },
108
109    #[snafu(display("Failed to shutdown frontend"))]
110    ShutdownFrontend {
111        #[snafu(implicit)]
112        location: Location,
113        source: frontend::error::Error,
114    },
115
116    #[snafu(display("Failed to build cli"))]
117    BuildCli {
118        #[snafu(implicit)]
119        location: Location,
120        source: BoxedError,
121    },
122
123    #[snafu(display("Failed to start cli"))]
124    StartCli {
125        #[snafu(implicit)]
126        location: Location,
127        source: BoxedError,
128    },
129
130    #[snafu(display("Failed to build meta server"))]
131    BuildMetaServer {
132        #[snafu(implicit)]
133        location: Location,
134        source: meta_srv::error::Error,
135    },
136
137    #[snafu(display("Failed to start meta server"))]
138    StartMetaServer {
139        #[snafu(implicit)]
140        location: Location,
141        source: meta_srv::error::Error,
142    },
143
144    #[snafu(display("Failed to shutdown meta server"))]
145    ShutdownMetaServer {
146        #[snafu(implicit)]
147        location: Location,
148        source: meta_srv::error::Error,
149    },
150
151    #[snafu(display("Missing config, msg: {}", msg))]
152    MissingConfig {
153        msg: String,
154        #[snafu(implicit)]
155        location: Location,
156    },
157
158    #[snafu(display("Illegal config: {}", msg))]
159    IllegalConfig {
160        msg: String,
161        #[snafu(implicit)]
162        location: Location,
163    },
164
165    #[snafu(display("Unsupported selector type: {}", selector_type))]
166    UnsupportedSelectorType {
167        selector_type: String,
168        #[snafu(implicit)]
169        location: Location,
170        source: meta_srv::error::Error,
171    },
172
173    #[snafu(display("Failed to parse SQL: {}", sql))]
174    ParseSql {
175        sql: String,
176        #[snafu(implicit)]
177        location: Location,
178        source: query::error::Error,
179    },
180
181    #[snafu(display("Failed to plan statement"))]
182    PlanStatement {
183        #[snafu(implicit)]
184        location: Location,
185        source: query::error::Error,
186    },
187
188    #[snafu(display("Failed to load layered config"))]
189    LoadLayeredConfig {
190        #[snafu(source(from(common_config::error::Error, Box::new)))]
191        source: Box<common_config::error::Error>,
192        #[snafu(implicit)]
193        location: Location,
194    },
195
196    #[snafu(display("Failed to connect to Etcd at {etcd_addr}"))]
197    ConnectEtcd {
198        etcd_addr: String,
199        #[snafu(source)]
200        error: etcd_client::Error,
201        #[snafu(implicit)]
202        location: Location,
203    },
204
205    #[snafu(display("Failed to serde json"))]
206    SerdeJson {
207        #[snafu(source)]
208        error: serde_json::error::Error,
209        #[snafu(implicit)]
210        location: Location,
211    },
212
213    #[snafu(display("Failed to run http request: {reason}"))]
214    HttpQuerySql {
215        reason: String,
216        #[snafu(source)]
217        error: reqwest::Error,
218        #[snafu(implicit)]
219        location: Location,
220    },
221
222    #[snafu(display("Empty result from output"))]
223    EmptyResult {
224        #[snafu(implicit)]
225        location: Location,
226    },
227
228    #[snafu(display("Failed to manipulate file"))]
229    FileIo {
230        #[snafu(implicit)]
231        location: Location,
232        #[snafu(source)]
233        error: std::io::Error,
234    },
235
236    #[snafu(display("Failed to create directory {}", dir))]
237    CreateDir {
238        dir: String,
239        #[snafu(source)]
240        error: std::io::Error,
241    },
242
243    #[snafu(display("Failed to spawn thread"))]
244    SpawnThread {
245        #[snafu(source)]
246        error: std::io::Error,
247    },
248
249    #[snafu(display("Other error"))]
250    Other {
251        source: BoxedError,
252        #[snafu(implicit)]
253        location: Location,
254    },
255
256    #[snafu(display("Failed to build runtime"))]
257    BuildRuntime {
258        #[snafu(implicit)]
259        location: Location,
260        source: common_runtime::error::Error,
261    },
262
263    #[snafu(display("Failed to get cache from cache registry: {}", name))]
264    CacheRequired {
265        #[snafu(implicit)]
266        location: Location,
267        name: String,
268    },
269
270    #[snafu(display("Failed to build cache registry"))]
271    BuildCacheRegistry {
272        #[snafu(implicit)]
273        location: Location,
274        source: cache::error::Error,
275    },
276
277    #[snafu(display("Failed to initialize meta client"))]
278    MetaClientInit {
279        #[snafu(implicit)]
280        location: Location,
281        source: meta_client::error::Error,
282    },
283
284    #[snafu(display("Cannot find schema {schema} in catalog {catalog}"))]
285    SchemaNotFound {
286        catalog: String,
287        schema: String,
288        #[snafu(implicit)]
289        location: Location,
290    },
291
292    #[snafu(display("Failed to build wal options allocator"))]
293    BuildWalOptionsAllocator {
294        #[snafu(implicit)]
295        location: Location,
296        source: common_meta::error::Error,
297    },
298
299    #[snafu(display("Failed to build metadata kvbackend"))]
300    BuildMetadataKvbackend {
301        #[snafu(implicit)]
302        location: Location,
303        source: standalone::error::Error,
304    },
305
306    #[snafu(display("Failed to setup standalone plugins"))]
307    SetupStandalonePlugins {
308        #[snafu(implicit)]
309        location: Location,
310        source: standalone::error::Error,
311    },
312
313    #[snafu(display("Invalid WAL provider"))]
314    InvalidWalProvider {
315        #[snafu(implicit)]
316        location: Location,
317        source: common_wal::error::Error,
318    },
319}
320
321pub type Result<T> = std::result::Result<T, Error>;
322
323impl ErrorExt for Error {
324    fn status_code(&self) -> StatusCode {
325        match self {
326            Error::StartDatanode { source, .. } => source.status_code(),
327            Error::StartFrontend { source, .. } => source.status_code(),
328            Error::ShutdownDatanode { source, .. } => source.status_code(),
329            Error::ShutdownFrontend { source, .. } => source.status_code(),
330            Error::StartMetaServer { source, .. } => source.status_code(),
331            Error::ShutdownMetaServer { source, .. } => source.status_code(),
332            Error::BuildMetaServer { source, .. } => source.status_code(),
333            Error::UnsupportedSelectorType { source, .. } => source.status_code(),
334            Error::BuildCli { source, .. } => source.status_code(),
335            Error::StartCli { source, .. } => source.status_code(),
336            Error::BuildMetadataKvbackend { source, .. } => source.status_code(),
337            Error::SetupStandalonePlugins { source, .. } => source.status_code(),
338
339            Error::InitMetadata { source, .. } | Error::InitDdlManager { source, .. } => {
340                source.status_code()
341            }
342
343            Error::MissingConfig { .. }
344            | Error::LoadLayeredConfig { .. }
345            | Error::IllegalConfig { .. }
346            | Error::InitTimezone { .. }
347            | Error::ConnectEtcd { .. }
348            | Error::CreateDir { .. }
349            | Error::EmptyResult { .. } => StatusCode::InvalidArguments,
350
351            Error::StartProcedureManager { source, .. }
352            | Error::StopProcedureManager { source, .. } => source.status_code(),
353            Error::BuildWalOptionsAllocator { source, .. }
354            | Error::StartWalOptionsAllocator { source, .. } => source.status_code(),
355            Error::HttpQuerySql { .. } => StatusCode::Internal,
356            Error::ParseSql { source, .. } | Error::PlanStatement { source, .. } => {
357                source.status_code()
358            }
359
360            Error::SerdeJson { .. }
361            | Error::FileIo { .. }
362            | Error::SpawnThread { .. }
363            | Error::InitTlsProvider { .. } => StatusCode::Unexpected,
364
365            Error::Other { source, .. } => source.status_code(),
366
367            Error::BuildRuntime { source, .. } => source.status_code(),
368
369            Error::CacheRequired { .. } | Error::BuildCacheRegistry { .. } => StatusCode::Internal,
370            Self::StartFlownode { source, .. } | Self::ShutdownFlownode { source, .. } => {
371                source.status_code()
372            }
373            Error::MetaClientInit { source, .. } => source.status_code(),
374            Error::SchemaNotFound { .. } => StatusCode::DatabaseNotFound,
375            Error::InvalidWalProvider { .. } => StatusCode::InvalidArguments,
376        }
377    }
378
379    fn as_any(&self) -> &dyn Any {
380        self
381    }
382}