Skip to content

Commit

Permalink
Merge pull request #32 from tablelandnetwork/bcalza/pubalreadyexists
Browse files Browse the repository at this point in the history
handles case when publication exists
  • Loading branch information
brunocalza authored Mar 28, 2024
2 parents 9e26120 + 6058c72 commit fe95f45
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
2 changes: 2 additions & 0 deletions lib/common/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down
8 changes: 7 additions & 1 deletion lib/evm/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
7 changes: 6 additions & 1 deletion lib/evm/src/testing/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand Down
29 changes: 20 additions & 9 deletions lib/worker/src/routes/vaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -389,15 +390,25 @@ pub async fn create_vault<E: EVMClient + 'static + std::marker::Sync>(
.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(
Expand Down
4 changes: 2 additions & 2 deletions lib/worker/tests/api/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(&CreateVaultInput {
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions lib/worker/tests/api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit fe95f45

Please sign in to comment.