Skip to main content

cli/
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 common_meta::peer::Peer;
21use object_store::Error as ObjectStoreError;
22use snafu::{Location, Snafu};
23use store_api::storage::{RegionId, TableId};
24
25#[derive(Snafu)]
26#[snafu(visibility(pub))]
27#[stack_trace_debug]
28pub enum Error {
29    #[snafu(display("Failed to get table metadata"))]
30    TableMetadata {
31        #[snafu(implicit)]
32        location: Location,
33        source: common_meta::error::Error,
34    },
35
36    #[snafu(display("Failed to build create region request for region: {region_id}"))]
37    BuildCreateRegionRequest {
38        region_id: RegionId,
39        #[snafu(implicit)]
40        location: Location,
41        source: common_meta::error::Error,
42    },
43
44    #[snafu(display("Unexpected error: {}", msg))]
45    Unexpected {
46        msg: String,
47        #[snafu(implicit)]
48        location: Location,
49    },
50
51    #[snafu(display("Missing config, msg: {}", msg))]
52    MissingConfig {
53        msg: String,
54        #[snafu(implicit)]
55        location: Location,
56    },
57
58    #[snafu(display("Failed to parse proxy options: {}", error))]
59    ParseProxyOpts {
60        #[snafu(source)]
61        error: reqwest::Error,
62        #[snafu(implicit)]
63        location: Location,
64    },
65
66    #[snafu(display("Failed to build reqwest client: {}", error))]
67    BuildClient {
68        #[snafu(implicit)]
69        location: Location,
70        #[snafu(source)]
71        error: reqwest::Error,
72    },
73
74    #[snafu(display("Failed to run http request: {reason}"))]
75    HttpQuerySql {
76        reason: String,
77        #[snafu(source)]
78        error: reqwest::Error,
79        #[snafu(implicit)]
80        location: Location,
81    },
82
83    #[snafu(display("Empty result from output"))]
84    EmptyResult {
85        #[snafu(implicit)]
86        location: Location,
87    },
88
89    #[snafu(display("Failed to manipulate file"))]
90    FileIo {
91        #[snafu(implicit)]
92        location: Location,
93        #[snafu(source)]
94        error: std::io::Error,
95    },
96
97    #[snafu(display("Other error"))]
98    Other {
99        source: BoxedError,
100        #[snafu(implicit)]
101        location: Location,
102    },
103
104    #[snafu(display("Cannot find schema {schema} in catalog {catalog}"))]
105    SchemaNotFound {
106        catalog: String,
107        schema: String,
108        #[snafu(implicit)]
109        location: Location,
110    },
111
112    #[snafu(display("Table not found: {table_id}"))]
113    TableNotFound {
114        table_id: TableId,
115        #[snafu(implicit)]
116        location: Location,
117    },
118
119    #[snafu(display("OpenDAL operator failed"))]
120    OpenDal {
121        #[snafu(implicit)]
122        location: Location,
123        #[snafu(source)]
124        error: ObjectStoreError,
125    },
126
127    #[snafu(display("Output directory not set"))]
128    OutputDirNotSet {
129        #[snafu(implicit)]
130        location: Location,
131    },
132
133    #[snafu(display("Empty store addresses"))]
134    EmptyStoreAddrs {
135        #[snafu(implicit)]
136        location: Location,
137    },
138
139    #[snafu(display("Unsupported memory backend"))]
140    UnsupportedMemoryBackend {
141        #[snafu(implicit)]
142        location: Location,
143    },
144
145    #[snafu(display("Invalid arguments: {}", msg))]
146    InvalidArguments {
147        msg: String,
148        #[snafu(implicit)]
149        location: Location,
150    },
151
152    #[snafu(display("Failed to init backend"))]
153    InitBackend {
154        #[snafu(implicit)]
155        location: Location,
156        #[snafu(source)]
157        error: ObjectStoreError,
158    },
159
160    #[snafu(display("Covert column schemas to defs failed"))]
161    CovertColumnSchemasToDefs {
162        #[snafu(implicit)]
163        location: Location,
164        source: operator::error::Error,
165    },
166
167    #[snafu(display("Failed to send request to datanode: {}", peer))]
168    SendRequestToDatanode {
169        peer: Peer,
170        #[snafu(implicit)]
171        location: Location,
172        source: common_meta::error::Error,
173    },
174
175    #[snafu(display("Failed to get current directory"))]
176    GetCurrentDir {
177        #[snafu(implicit)]
178        location: Location,
179        #[snafu(source)]
180        error: std::io::Error,
181    },
182}
183
184pub type Result<T> = std::result::Result<T, Error>;
185
186impl ErrorExt for Error {
187    fn status_code(&self) -> StatusCode {
188        match self {
189            Error::TableMetadata { source, .. }
190            | Error::BuildCreateRegionRequest { source, .. } => source.status_code(),
191
192            Error::MissingConfig { .. }
193            | Error::EmptyResult { .. }
194            | Error::UnsupportedMemoryBackend { .. }
195            | Error::InvalidArguments { .. }
196            | Error::ParseProxyOpts { .. } => StatusCode::InvalidArguments,
197
198            Error::CovertColumnSchemasToDefs { source, .. } => source.status_code(),
199            Error::SendRequestToDatanode { source, .. } => source.status_code(),
200
201            Error::HttpQuerySql { .. } => StatusCode::Internal,
202            Error::Unexpected { .. } => StatusCode::Unexpected,
203
204            Error::FileIo { .. } | Error::BuildClient { .. } => StatusCode::Unexpected,
205
206            Error::Other { source, .. } => source.status_code(),
207            Error::OpenDal { .. } | Error::InitBackend { .. } => StatusCode::Internal,
208            Error::OutputDirNotSet { .. } | Error::EmptyStoreAddrs { .. } => {
209                StatusCode::InvalidArguments
210            }
211
212            Error::GetCurrentDir { .. } => StatusCode::Internal,
213            Error::TableNotFound { .. } => StatusCode::TableNotFound,
214            Error::SchemaNotFound { .. } => StatusCode::DatabaseNotFound,
215        }
216    }
217
218    fn as_any(&self) -> &dyn Any {
219        self
220    }
221}