🏡 index : ~doyle/chartered.git

author Jordan Doyle <jordan@doyle.la> 2021-09-21 3:08:12.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2021-09-21 3:08:12.0 +01:00:00
commit
058e4ce26eaab5e83104e83585551e7e54841c05 [patch]
tree
2a2c7d42f6ace69d6c4762e5cafc11d167ca307e
parent
d27847cbb9f25dde54def0b480e60bee2761da1d
download
058e4ce26eaab5e83104e83585551e7e54841c05.tar.gz

More descriptive error for publish version conflict



Diff

 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,
        }
    }