From 6058c7229d0d6f31273e9a3dced1a99d6310287a Mon Sep 17 00:00:00 2001 From: Bruno Calza Date: Tue, 26 Mar 2024 17:38:04 -0300 Subject: [PATCH] handles case when publication exists [staging] Signed-off-by: Bruno Calza --- lib/common/src/errors.rs | 2 ++ lib/evm/src/client.rs | 8 +++++++- lib/evm/src/testing/mock.rs | 7 ++++++- lib/worker/src/routes/vaults.rs | 29 ++++++++++++++++++++--------- lib/worker/tests/api/helpers.rs | 4 ++-- lib/worker/tests/api/http.rs | 10 ++++++++++ 6 files changed, 47 insertions(+), 13 deletions(-) diff --git a/lib/common/src/errors.rs b/lib/common/src/errors.rs index 9c76c6c..a49eee0 100644 --- a/lib/common/src/errors.rs +++ b/lib/common/src/errors.rs @@ -14,6 +14,8 @@ pub enum Error { Url(String), #[error("EVM error: {0}")] Evm(String), + #[error("EVM publication exists")] + EvmPublicationExists, #[error("Upload error: {0}")] Upload(String), } diff --git a/lib/evm/src/client.rs b/lib/evm/src/client.rs index 5bf197b..0ab1adf 100644 --- a/lib/evm/src/client.rs +++ b/lib/evm/src/client.rs @@ -43,7 +43,13 @@ impl EVMClient for BasinClient { .create_pub(owner, name.into()) .send() .await - .map_err(|e| Error::Evm(e.to_string()))?; + .map_err(|e| { + if e.is_revert() { + return Error::EvmPublicationExists; + } + Error::Evm(e.to_string()) + })?; + Ok(()) } diff --git a/lib/evm/src/testing/mock.rs b/lib/evm/src/testing/mock.rs index de34d89..b9db417 100644 --- a/lib/evm/src/testing/mock.rs +++ b/lib/evm/src/testing/mock.rs @@ -63,7 +63,12 @@ impl EVMClient for MockClient { .create_pub(owner, name.into()) .send() .await - .unwrap(); + .map_err(|e| { + if e.is_revert() { + return Error::EvmPublicationExists; + } + Error::Evm(e.to_string()) + })?; Ok(()) } diff --git a/lib/worker/src/routes/vaults.rs b/lib/worker/src/routes/vaults.rs index 2ede787..8ccf134 100644 --- a/lib/worker/src/routes/vaults.rs +++ b/lib/worker/src/routes/vaults.rs @@ -11,6 +11,7 @@ use std::str::FromStr; use basin_evm::EVMClient; use chrono::DateTime; +use basin_common::errors::Error; use ethers::types::Address; use futures::StreamExt; use google_cloud_storage::http::objects::download::Range; @@ -389,15 +390,25 @@ pub async fn create_vault( .await { Ok(_) => {} - Err(err) => { - log::error!("{}", err); - return Ok(with_status( - json(&ErrorResponse { - error: "failed to create vault".to_string(), - }), - StatusCode::BAD_REQUEST, - )); - } + Err(err) => match err { + Error::EvmPublicationExists => { + return Ok(with_status( + json(&ErrorResponse { + error: "publication already exists".to_string(), + }), + StatusCode::CONFLICT, + )) + } + _ => { + log::error!("{}", err); + return Ok(with_status( + json(&ErrorResponse { + error: "failed to create vault".to_string(), + }), + StatusCode::BAD_REQUEST, + )); + } + }, } let created = match db::namespace_create( diff --git a/lib/worker/tests/api/helpers.rs b/lib/worker/tests/api/helpers.rs index 9620816..62a87c8 100644 --- a/lib/worker/tests/api/helpers.rs +++ b/lib/worker/tests/api/helpers.rs @@ -109,7 +109,7 @@ impl TestApp { .expect("Failed to execute request") } - pub async fn create_vault(&self, name: &str) { + pub async fn create_vault(&self, name: &str) -> Response { self.api_client .post(&format!("{}/vaults/{}", &self.address, name)) .form::(&CreateVaultInput { @@ -118,7 +118,7 @@ impl TestApp { }) .send() .await - .expect("Failed to execute request."); + .expect("Failed to execute request.") } pub async fn create_vault_with_cache(&self, name: &str, cache: i64) { diff --git a/lib/worker/tests/api/http.rs b/lib/worker/tests/api/http.rs index e5a5b42..f265dd3 100644 --- a/lib/worker/tests/api/http.rs +++ b/lib/worker/tests/api/http.rs @@ -253,6 +253,16 @@ async fn create_vault() { assert_eq!("api.test", vaults[0]); } +#[tokio::test] +async fn create_vault_already_exists() { + let app = spawn_app().await; + + app.create_vault("api.test").await; + let response = app.create_vault("api.test").await; + + assert_eq!(response.status(), StatusCode::CONFLICT); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn write_event() { let app = spawn_app().await;