Skip to content

Commit

Permalink
fix: duplicate key registration giving 500 error (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris13524 authored Feb 22, 2024
1 parent a15bfca commit 089d5b1
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/stores/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@ pub use {
},
};
use {
crate::config::Configuration, relay_rpc::auth::cacao::Cacao,
wither::mongodb::options::FindOneAndUpdateOptions,
crate::config::Configuration,
relay_rpc::auth::cacao::Cacao,
wither::{
mongodb::{self, options::FindOneAndUpdateOptions},
WitherError,
},
};

// https://www.mongodb.com/docs/manual/reference/error-codes/#mongodb-error-11000
const MONGODB_DUPLICATE_KEY_ERROR_CODE: i32 = 11000;

pub type KeysPersistentStorageArc = Arc<dyn KeysPersistentStorage + Send + Sync + 'static>;

#[async_trait]
Expand Down Expand Up @@ -127,7 +134,6 @@ impl KeysPersistentStorage for MongoPersistentStorage {
) -> Result<(), StoreError> {
let filter = doc! {
"account": &account,
"identities.identity_key": {"$ne": &identity_key},
};

let mongo_identity = MongoIdentity {
Expand All @@ -146,18 +152,19 @@ impl KeysPersistentStorage for MongoPersistentStorage {
match MongoKeys::find_one_and_update(&self.db, filter, update, option).await {
Ok(Some(_)) => Ok(()),
Ok(None) => Ok(()),
Err(e) => {
if e.to_string().starts_with(
"Command failed (DuplicateKey): E11000 duplicate key error collection: \
keyserver.keys index: account_1",
)
// Todo add better error matching
{
Ok(())
} else {
Err(StoreError::Database(e))
}
}
Err(e) => match &e {
WitherError::Mongo(mongo_error) => match mongo_error.kind.as_ref() {
mongodb::error::ErrorKind::Command(command_error) => {
if command_error.code == MONGODB_DUPLICATE_KEY_ERROR_CODE {
Ok(())
} else {
Err(StoreError::Database(e))
}
}
_ => Err(StoreError::Database(e)),
},
_ => Err(StoreError::Database(e)),
},
}
}

Expand Down

0 comments on commit 089d5b1

Please sign in to comment.