From 85074f3ac884d74744c80ac0d68f625fd6ebd89c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogdan-=C8=98tefan=20Neac=C8=99u?= Date: Wed, 22 Mar 2023 17:21:20 +0200 Subject: [PATCH] Better error log for lacking credential in db --- common/credential-storage/src/coconut.rs | 4 ++-- common/credential-storage/src/error.rs | 3 +++ common/credential-storage/src/lib.rs | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) 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) }