|
| 1 | +// Copyright 2021 Datafuse Labs |
| 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 | + |
| 15 | +//! Error construction utilities extracted from util.rs and other modules |
| 16 | +
|
| 17 | +use std::fmt::Display; |
| 18 | + |
| 19 | +use databend_common_meta_app::app_error::AppError; |
| 20 | +use databend_common_meta_app::app_error::DatabaseAlreadyExists; |
| 21 | +use databend_common_meta_app::app_error::TableAlreadyExists; |
| 22 | +use databend_common_meta_app::app_error::UnknownDatabase; |
| 23 | +use databend_common_meta_app::app_error::UnknownDatabaseId; |
| 24 | +use databend_common_meta_app::app_error::UnknownTable; |
| 25 | +use databend_common_meta_app::schema::database_name_ident::DatabaseNameIdent; |
| 26 | +use databend_common_meta_app::schema::TableNameIdent; |
| 27 | + |
| 28 | +use crate::kv_app_error::KVAppError; |
| 29 | + |
| 30 | +pub fn unknown_database_error(db_name_ident: &DatabaseNameIdent, msg: impl Display) -> AppError { |
| 31 | + let e = UnknownDatabase::new( |
| 32 | + db_name_ident.database_name(), |
| 33 | + format!("{}: {}", msg, db_name_ident.display()), |
| 34 | + ); |
| 35 | + AppError::UnknownDatabase(e) |
| 36 | +} |
| 37 | + |
| 38 | +pub fn db_has_to_exist( |
| 39 | + seq: u64, |
| 40 | + db_name_ident: &DatabaseNameIdent, |
| 41 | + msg: impl Display, |
| 42 | +) -> Result<(), KVAppError> { |
| 43 | + if seq == 0 { |
| 44 | + Err(KVAppError::AppError(unknown_database_error( |
| 45 | + db_name_ident, |
| 46 | + msg, |
| 47 | + ))) |
| 48 | + } else { |
| 49 | + Ok(()) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +pub fn db_id_has_to_exist(seq: u64, db_id: u64, msg: impl Display) -> Result<(), KVAppError> { |
| 54 | + if seq == 0 { |
| 55 | + let app_err = AppError::UnknownDatabaseId(UnknownDatabaseId::new( |
| 56 | + db_id, |
| 57 | + format!("{}: {}", msg, db_id), |
| 58 | + )); |
| 59 | + Err(KVAppError::AppError(app_err)) |
| 60 | + } else { |
| 61 | + Ok(()) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +pub fn assert_table_exist( |
| 66 | + seq: u64, |
| 67 | + name_ident: &TableNameIdent, |
| 68 | + ctx: impl Display, |
| 69 | +) -> Result<(), AppError> { |
| 70 | + if seq > 0 { |
| 71 | + return Ok(()); |
| 72 | + } |
| 73 | + Err(AppError::UnknownTable(UnknownTable::new( |
| 74 | + &name_ident.table_name, |
| 75 | + format!("{}: {}", ctx, name_ident), |
| 76 | + ))) |
| 77 | +} |
| 78 | + |
| 79 | +pub fn db_has_to_not_exist( |
| 80 | + seq: u64, |
| 81 | + name_ident: &DatabaseNameIdent, |
| 82 | + ctx: impl Display, |
| 83 | +) -> Result<(), KVAppError> { |
| 84 | + if seq == 0 { |
| 85 | + Ok(()) |
| 86 | + } else { |
| 87 | + Err(KVAppError::AppError(AppError::DatabaseAlreadyExists( |
| 88 | + DatabaseAlreadyExists::new( |
| 89 | + name_ident.database_name(), |
| 90 | + format!("{}: {}", ctx, name_ident.display()), |
| 91 | + ), |
| 92 | + ))) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +pub fn table_has_to_not_exist( |
| 97 | + seq: u64, |
| 98 | + name_ident: &TableNameIdent, |
| 99 | + ctx: impl Display, |
| 100 | +) -> Result<(), KVAppError> { |
| 101 | + if seq == 0 { |
| 102 | + Ok(()) |
| 103 | + } else { |
| 104 | + Err(KVAppError::AppError(AppError::TableAlreadyExists( |
| 105 | + TableAlreadyExists::new(&name_ident.table_name, format!("{}: {}", ctx, name_ident)), |
| 106 | + ))) |
| 107 | + } |
| 108 | +} |
0 commit comments