Skip to main content

cli/data/export_v2/
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::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("Invalid URI '{}': {}", uri, reason))]
27    InvalidUri {
28        uri: String,
29        reason: String,
30        #[snafu(implicit)]
31        location: Location,
32    },
33
34    #[snafu(display("Unsupported storage scheme: {}", scheme))]
35    UnsupportedScheme {
36        scheme: String,
37        #[snafu(implicit)]
38        location: Location,
39    },
40
41    #[snafu(display("Storage operation '{}' failed", operation))]
42    StorageOperation {
43        operation: String,
44        #[snafu(source)]
45        error: object_store::Error,
46        #[snafu(implicit)]
47        location: Location,
48    },
49
50    #[snafu(display("Failed to parse manifest"))]
51    ManifestParse {
52        #[snafu(source)]
53        error: serde_json::Error,
54        #[snafu(implicit)]
55        location: Location,
56    },
57
58    #[snafu(display("Failed to serialize manifest"))]
59    ManifestSerialize {
60        #[snafu(source)]
61        error: serde_json::Error,
62        #[snafu(implicit)]
63        location: Location,
64    },
65
66    #[snafu(display("Failed to decode text file as UTF-8"))]
67    TextDecode {
68        #[snafu(source)]
69        error: std::string::FromUtf8Error,
70        #[snafu(implicit)]
71        location: Location,
72    },
73
74    #[snafu(display(
75        "Cannot resume schema-only snapshot with data export. Use --force to recreate."
76    ))]
77    CannotResumeSchemaOnly {
78        #[snafu(implicit)]
79        location: Location,
80    },
81
82    #[snafu(display(
83        "Data export is not implemented yet. Use --schema-only to create a schema snapshot."
84    ))]
85    DataExportNotImplemented {
86        #[snafu(implicit)]
87        location: Location,
88    },
89
90    #[snafu(display("Empty result from query"))]
91    EmptyResult {
92        #[snafu(implicit)]
93        location: Location,
94    },
95
96    #[snafu(display("Unexpected value type in query result"))]
97    UnexpectedValueType {
98        #[snafu(implicit)]
99        location: Location,
100    },
101
102    #[snafu(display("Database error"))]
103    Database {
104        #[snafu(source)]
105        error: crate::error::Error,
106        #[snafu(implicit)]
107        location: Location,
108    },
109
110    #[snafu(display("Snapshot not found at '{}'", uri))]
111    SnapshotNotFound {
112        uri: String,
113        #[snafu(implicit)]
114        location: Location,
115    },
116
117    #[snafu(display("Schema '{}' not found in catalog '{}'", schema, catalog))]
118    SchemaNotFound {
119        catalog: String,
120        schema: String,
121        #[snafu(implicit)]
122        location: Location,
123    },
124
125    #[snafu(display("Failed to parse URL"))]
126    UrlParse {
127        #[snafu(source)]
128        error: url::ParseError,
129        #[snafu(implicit)]
130        location: Location,
131    },
132
133    #[snafu(display("Failed to build object store"))]
134    BuildObjectStore {
135        #[snafu(source)]
136        error: object_store::Error,
137        #[snafu(implicit)]
138        location: Location,
139    },
140
141    #[snafu(display("Manifest version mismatch: expected {}, found {}", expected, found))]
142    ManifestVersionMismatch {
143        expected: u32,
144        found: u32,
145        #[snafu(implicit)]
146        location: Location,
147    },
148}
149
150pub type Result<T> = std::result::Result<T, Error>;
151
152impl ErrorExt for Error {
153    fn status_code(&self) -> StatusCode {
154        match self {
155            Error::InvalidUri { .. }
156            | Error::UnsupportedScheme { .. }
157            | Error::CannotResumeSchemaOnly { .. }
158            | Error::DataExportNotImplemented { .. }
159            | Error::ManifestVersionMismatch { .. } => StatusCode::InvalidArguments,
160
161            Error::StorageOperation { .. }
162            | Error::ManifestParse { .. }
163            | Error::ManifestSerialize { .. }
164            | Error::TextDecode { .. }
165            | Error::BuildObjectStore { .. } => StatusCode::StorageUnavailable,
166
167            Error::EmptyResult { .. }
168            | Error::UnexpectedValueType { .. }
169            | Error::UrlParse { .. } => StatusCode::Internal,
170
171            Error::Database { error, .. } => error.status_code(),
172
173            Error::SnapshotNotFound { .. } => StatusCode::InvalidArguments,
174            Error::SchemaNotFound { .. } => StatusCode::DatabaseNotFound,
175        }
176    }
177
178    fn as_any(&self) -> &dyn Any {
179        self
180    }
181}