diff --git a/common/credential-storage/src/coconut.rs b/common/credential-storage/src/coconut.rs index a6cd968c2e..f4b78d8846 100644 --- a/common/credential-storage/src/coconut.rs +++ b/common/credential-storage/src/coconut.rs @@ -48,12 +48,12 @@ impl CoconutCredentialManager { /// Tries to retrieve one of the stored, unused credentials. pub(crate) async fn get_next_coconut_credential( &self, - ) -> Result { + ) -> Result, sqlx::Error> { sqlx::query_as!( CoconutCredential, "SELECT * FROM coconut_credentials WHERE NOT consumed" ) - .fetch_one(&self.connection_pool) + .fetch_optional(&self.connection_pool) .await } diff --git a/common/credential-storage/src/error.rs b/common/credential-storage/src/error.rs index fbd9a347f4..83082e0d6a 100644 --- a/common/credential-storage/src/error.rs +++ b/common/credential-storage/src/error.rs @@ -13,4 +13,7 @@ pub enum StorageError { #[error("Inconsistent data in database")] InconsistentData, + + #[error("No unused credential in database. You need to buy at least one")] + NoCredential, } diff --git a/common/credential-storage/src/lib.rs b/common/credential-storage/src/lib.rs index bf3af1b272..202cd952c8 100644 --- a/common/credential-storage/src/lib.rs +++ b/common/credential-storage/src/lib.rs @@ -90,7 +90,8 @@ impl Storage for PersistentStorage { let credential = self .coconut_credential_manager .get_next_coconut_credential() - .await?; + .await? + .ok_or(StorageError::NoCredential)?; Ok(credential) }