From 0783c532deb5fd6858e37d0d97f290a1a7426b5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Mon, 13 May 2024 10:19:01 +0200 Subject: [PATCH] Explicitly handle constraint unique violation when importing credential --- common/credential-storage/src/error.rs | 3 +++ .../src/persistent_storage.rs | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/common/credential-storage/src/error.rs b/common/credential-storage/src/error.rs index 39ea67ba3b..0756fce324 100644 --- a/common/credential-storage/src/error.rs +++ b/common/credential-storage/src/error.rs @@ -18,4 +18,7 @@ pub enum StorageError { #[error("No unused credential in database. You need to buy at least one")] NoCredential, + + #[error("Database unique constraint violation. Is the credential already imported?")] + ContraintUnique, } diff --git a/common/credential-storage/src/persistent_storage.rs b/common/credential-storage/src/persistent_storage.rs index f03d1abf68..d3d2d2ace3 100644 --- a/common/credential-storage/src/persistent_storage.rs +++ b/common/credential-storage/src/persistent_storage.rs @@ -69,9 +69,21 @@ impl Storage for PersistentStorage { bandwidth_credential.credential_data, bandwidth_credential.epoch_id, ) - .await?; - - Ok(()) + .await + .map_err(|err| { + // There is one error we want to handle specifically. + // Check if database_error is `SqliteError` with code 2067 which + // means UNIQUE constraint violation + if let Some(db_error) = err.as_database_error() { + if db_error.code().map_or(false, |code| code == "2067") { + StorageError::ContraintUnique + } else { + err.into() + } + } else { + err.into() + } + }) } async fn get_next_unspent_credential(