From 058e4ce26eaab5e83104e83585551e7e54841c05 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Tue, 21 Sep 2021 03:08:12 +0100 Subject: [PATCH] More descriptive error for publish version conflict --- chartered-db/src/crates.rs | 15 ++++++++++++--- chartered-db/src/lib.rs | 4 +++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/chartered-db/src/crates.rs b/chartered-db/src/crates.rs index 6157a79..fbd7729 100644 --- a/chartered-db/src/crates.rs +++ a/chartered-db/src/crates.rs @@ -401,21 +401,28 @@ )) .execute(&conn)?; - insert_into(crate_versions) + let res = insert_into(crate_versions) .values(( crate_id.eq(self.crate_.id), filesystem_object.eq(file_identifier.to_string()), size.eq(file_size), checksum.eq(file_checksum), - version.eq(given.vers), + version.eq(&given.vers), dependencies.eq(CrateDependencies(given.deps)), features.eq(CrateFeatures(given.features)), links.eq(given.links), user_id.eq(user.id), )) - .execute(&conn)?; + .execute(&conn); - Ok(()) + use diesel::result::{DatabaseErrorKind, Error as DieselError}; + match res { + Ok(_) => Ok(()), + Err(DieselError::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) => { + Err(Error::VersionConflict(given.vers.into_owned())) + } + Err(e) => Err(e.into()), + } })?; Ok(()) diff --git a/chartered-db/src/lib.rs b/chartered-db/src/lib.rs index 8648b14..c497e19 100644 --- a/chartered-db/src/lib.rs +++ a/chartered-db/src/lib.rs @@ -69,6 +69,8 @@ MissingPermission(crate::users::UserCratePermissionValue), /// The requested crate does not exist MissingCrate, + /// Version {0} already exists for this crate + VersionConflict(String), } impl Error { @@ -82,7 +84,7 @@ http::StatusCode::NOT_FOUND } Self::MissingPermission(_) => http::StatusCode::FORBIDDEN, - Self::KeyParse(_) => http::StatusCode::BAD_REQUEST, + Self::KeyParse(_) | Self::VersionConflict(_) => http::StatusCode::BAD_REQUEST, _ => http::StatusCode::INTERNAL_SERVER_ERROR, } } -- rgit 0.1.3