Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
rename error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SimranMakhija7 committed May 2, 2024
1 parent 9d51f2b commit da66673
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/handlers/table_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,13 @@ pub async fn rename_table(
State(repo): State<Arc<TableRepository>>,
request: Json<TableRenameRequest>,
) -> Result<StatusCode, (StatusCode, String)> {

repo.rename_table(&request)
.map(|_| StatusCode::OK)
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("Error: {}", e)))
match repo.rename_table(&request) {
Ok(_) => Ok(StatusCode::NO_CONTENT),
Err(e) => match e.kind() {
ErrorKind::NotFound => Err((StatusCode::NOT_FOUND, format!("Error: {}", e))),
ErrorKind::AlreadyExists => Err((StatusCode::CONFLICT, format!("Error: {}", e))),
_ => Err((StatusCode::INTERNAL_SERVER_ERROR, format!("Error: {}", e))),
},
}
}

15 changes: 10 additions & 5 deletions src/repository/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl TableRepository {
pub fn drop_table(&self, namespace: &NamespaceIdent, table_name: String) -> Result<(), Error> {
let db = self.database.lock().unwrap();
let table_id = TableIdent::new(namespace.clone(), table_name.clone());

let _ : Table = match db.get::<TableIdent, Table>("TableData", &table_id)? {
Some(data) => data,
None => {
Expand Down Expand Up @@ -110,15 +110,20 @@ impl TableRepository {
let source = rename_request.source.clone();
let destination = rename_request.destination.clone();
let namespace = source.namespace.clone();

let table = self
.load_table(&namespace, source.name.clone())?
.ok_or_else(|| Error::new(ErrorKind::NotFound, "Table not found"))?;

.ok_or_else(|| Error::new(ErrorKind::NotFound, "Source table not found"))?;

if self.table_exists(&destination.namespace, destination.name.clone())? {
return Err(Error::new(ErrorKind::AlreadyExists, "Destination table already exists"));
}

let mut new_table = table.clone();
new_table.id = destination.clone();

self.drop_table(&namespace, source.name.clone())?;
self.create_table(&destination.namespace.clone(), &TableCreation{name: destination.name.clone()})
self.create_table(&destination.namespace.clone(), &TableCreation{name: destination.name.clone()})?;
self.drop_table(&namespace, source.name.clone())
}
}

0 comments on commit da66673

Please sign in to comment.