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(-)
@@ -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(())
@@ -69,6 +69,8 @@
MissingPermission(crate::users::UserCratePermissionValue),
MissingCrate,
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,
}
}