Skip to content

Commit

Permalink
review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
smklein committed Dec 4, 2023
1 parent ec9f450 commit 538d48c
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions nexus/db-queries/src/db/datastore/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,6 @@ impl DataStore {
let gen = snapshot.gen;
opctx.authorize(authz::Action::CreateChild, authz_project).await?;

#[derive(Debug, thiserror::Error)]
pub enum CustomError {
#[error("Resource already exists")]
ResourceAlreadyExists,

#[error("saw AsyncInsertError")]
InsertError(Error),
}

let snapshot_name = snapshot.name().to_string();
let project_id = snapshot.project_id;

let err = OptionalError::new();
Expand All @@ -67,6 +57,7 @@ impl DataStore {
.transaction(&conn, |conn| {
let err = err.clone();
let snapshot = snapshot.clone();
let snapshot_name = snapshot.name().to_string();
async move {
use db::schema::snapshot::dsl;

Expand Down Expand Up @@ -115,9 +106,10 @@ impl DataStore {

if let Some(existing_snapshot_id) = existing_snapshot_id {
if existing_snapshot_id != snapshot.id() {
return Err(
err.bail(CustomError::ResourceAlreadyExists)
);
return Err(err.bail(Error::ObjectAlreadyExists {
type_name: ResourceType::Snapshot,
object_name: snapshot_name,
}));
}
}

Expand All @@ -133,28 +125,20 @@ impl DataStore {
.insert_and_get_result_async(&conn)
.await
.map_err(|e| match e {
AsyncInsertError::CollectionNotFound => err.bail(
CustomError::InsertError(Error::ObjectNotFound {
AsyncInsertError::CollectionNotFound => {
err.bail(Error::ObjectNotFound {
type_name: ResourceType::Project,
lookup_type: LookupType::ById(project_id),
}),
),
})
}
AsyncInsertError::DatabaseError(e) => e,
})
}
})
.await
.map_err(|e| {
if let Some(err) = err.take() {
match err {
CustomError::ResourceAlreadyExists => {
Error::ObjectAlreadyExists {
type_name: ResourceType::Snapshot,
object_name: snapshot_name,
}
}
CustomError::InsertError(e) => e,
}
err
} else {
public_error_from_diesel(e, ErrorHandler::Server)
}
Expand Down

0 comments on commit 538d48c

Please sign in to comment.