From dcb11a4d11344368d0298846bbc1329678a6d0da Mon Sep 17 00:00:00 2001 From: realaravinth Date: Tue, 17 May 2022 14:56:33 +0530 Subject: [PATCH] feat: database errors --- db/db-core/src/errors.rs | 24 ++++++++++++++++++++++++ db/db-core/src/tests.rs | 2 +- db/db-sqlx-sqlite/src/errors.rs | 25 +++++++++++++++++-------- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/db/db-core/src/errors.rs b/db/db-core/src/errors.rs index 0925874..599d9fc 100644 --- a/db/db-core/src/errors.rs +++ b/db/db-core/src/errors.rs @@ -32,6 +32,30 @@ pub enum DBError { #[error("DNS challenge hostname is already taken")] DuplicateChallengeHostname, + /// Hostname is already taken + #[error("Hostname is already taken")] + DuplicateHostname, + + /// Forge Type is already taken + #[error("Forge Type is already taken")] + DuplicateForgeType, + + /// HTML link Type is already taken + #[error("User HTML link is already taken")] + DuplicateUserLink, + + /// User profile photo link Type is already taken + #[error("User profile photo link is already taken")] + DuplicateProfilePhotoLink, + + /// Topic is already taken + #[error("Topic is already taken")] + DuplicateTopic, + + /// Repository link is already taken + #[error("Repository link is already taken")] + DuplicateRepositoryLink, + /// forge instance type is unknown #[error("Unknown forge instance specifier {}", _0)] UnknownForgeType(String), diff --git a/db/db-core/src/tests.rs b/db/db-core/src/tests.rs index e7cd6dc..9e77484 100644 --- a/db/db-core/src/tests.rs +++ b/db/db-core/src/tests.rs @@ -26,7 +26,7 @@ pub async fn adding_forge_works<'a, T: SCDatabase>( add_user_msg2: AddUser<'a>, add_repo_msg: AddRepository<'a>, ) { - let _ = db.delete_forge_instance(&create_forge_msg.hostname).await; + let _ = db.delete_forge_instance(create_forge_msg.hostname).await; db.create_forge_isntance(&create_forge_msg).await.unwrap(); assert!( db.forge_exists(create_forge_msg.hostname).await.unwrap(), diff --git a/db/db-sqlx-sqlite/src/errors.rs b/db/db-sqlx-sqlite/src/errors.rs index 52ad254..e6d8165 100644 --- a/db/db-sqlx-sqlite/src/errors.rs +++ b/db/db-sqlx-sqlite/src/errors.rs @@ -23,20 +23,29 @@ pub fn map_register_err(e: Error) -> DBError { if let Error::Database(err) = e { if err.code() == Some(Cow::from("2067")) { let msg = err.message(); - unimplemented!("deal with errors upon insertaion of duplicate values"); - println!("{}", msg); + println!("db err: {msg}"); if msg.contains("starchart_dns_challenges.hostname") { - unimplemented!() + DBError::DuplicateChallengeHostname + } else if msg.contains("starchart_forges.hostname") { + DBError::DuplicateHostname } else if msg.contains("starchart_dns_challenges.challenge") { - unimplemented!() + DBError::DuplicateChallengeText } else if msg.contains("starchart_users.html_url") { - unimplemented!() + DBError::DuplicateUserLink } else if msg.contains("starchart_project_topics.name") { - unimplemented!() + DBError::DuplicateTopic } else if msg.contains("starchart_repositories.html_url") { - unimplemented!() + DBError::DuplicateRepositoryLink } else if msg.contains("starchart_forge_type.name") { - unimplemented!() + DBError::DuplicateForgeType + } else if msg.contains("starchart_users.html_url") { + DBError::DuplicateUserLink + } else if msg.contains("starchart_users.profile_photo_html_url") { + DBError::DuplicateProfilePhotoLink + } else if msg.contains("starchart_project_topics.name") { + DBError::DuplicateTopic + } else if msg.contains("starchart_repositories.name") { + DBError::DuplicateRepositoryLink } else { DBError::DBError(Box::new(Error::Database(err)).into()) }