Skip to content

Commit

Permalink
Merge pull request #78 from picture-pro/77-cleanup-custom-error-usage
Browse files Browse the repository at this point in the history
77 cleanup custom error usage
  • Loading branch information
johnbchron authored Mar 9, 2024
2 parents d521cfe + 7a7e0e2 commit e51405c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 64 deletions.
11 changes: 2 additions & 9 deletions crates/artifact/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ where
Id: core_types::CoreId,
T: serde::Serialize + for<'a> serde::Deserialize<'a> + Clone,
{
let client = clients::surreal::SurrealRootClient::new()
.await
.wrap_err("Failed to create surreal client")?;

client.use_ns("main").use_db("main").await?;
let client = clients::surreal::SurrealRootClient::new().await?;

let pushed_artifact: Vec<T> = client
.create(Id::TABLE)
Expand All @@ -129,11 +125,8 @@ where
Id: core_types::CoreId<Model = T>,
T: serde::Serialize + for<'a> serde::Deserialize<'a> + Clone,
{
let client = clients::surreal::SurrealRootClient::new()
.await
.wrap_err("Failed to create surreal client")?;
let client = clients::surreal::SurrealRootClient::new().await?;

client.use_ns("main").use_db("main").await?;
let artifact: Option<T> = client
.select(id)
.await
Expand Down
9 changes: 2 additions & 7 deletions crates/auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,9 @@ pub async fn build_auth_layer() -> Result<
tower_sessions_surrealdb_store::SurrealSessionStore<Client>,
>,
> {
let session_store_surreal_client =
clients::surreal::SurrealRootClient::new().await?;
session_store_surreal_client
.use_ns("main")
.use_db("main")
.await?;
let surreal_client = clients::surreal::SurrealRootClient::new().await?;
let session_store = tower_sessions_surrealdb_store::SurrealSessionStore::new(
session_store_surreal_client.into_inner(),
surreal_client.into_inner(),
"user_session".to_string(),
);
let session_manager_layer =
Expand Down
32 changes: 3 additions & 29 deletions crates/bl/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ pub async fn fetch_user_owned_photo_groups(
use core_types::CoreId;

async move {
let client = SurrealRootClient::new()
.await
.wrap_err("Failed to create surreal client")?;
client
.use_ns("main")
.use_db("main")
.await
.wrap_err("Failed to use surreal namespace/database")?;
let client = SurrealRootClient::new().await?;

let mut result = client
.query(format!(
Expand Down Expand Up @@ -60,14 +53,7 @@ pub async fn fetch_photo_group(
use crate::model_ext::ModelExt;

async move {
let client = SurrealRootClient::new()
.await
.wrap_err("Failed to create surreal client")?;
client
.use_ns("main")
.use_db("main")
.await
.wrap_err("Failed to use surreal namespace/database")?;
let client = SurrealRootClient::new().await?;

let group = core_types::PhotoGroup::fetch(photo_group_id, &client)
.await
Expand Down Expand Up @@ -97,14 +83,7 @@ pub async fn fetch_user(
use crate::model_ext::ModelExt;

async move {
let client = SurrealRootClient::new()
.await
.wrap_err("Failed to create surreal client")?;
client
.use_ns("main")
.use_db("main")
.await
.wrap_err("Failed to use surreal namespace/database")?;
let client = SurrealRootClient::new().await?;

core_types::User::fetch(user_id, &client)
.await
Expand Down Expand Up @@ -136,11 +115,6 @@ pub async fn fetch_photo_thumbnail(
async move {
// prep the surreal client
let client = SurrealRootClient::new().await?;
client
.use_ns("main")
.use_db("main")
.await
.wrap_err("Failed to start surreal client")?;

let photo = core_types::Photo::fetch(photo_id, &client)
.await
Expand Down
25 changes: 6 additions & 19 deletions crates/bl/src/upload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub async fn upload_photo_group(
let Some(user) =
leptos::use_context::<core_types::LoggedInUser>().and_then(|u| u.0)
else {
return Err(PhotoUploadError::Unauthenticated.into());
Err(PhotoUploadError::Unauthenticated)?
};

let (tx, mut rx) = mpsc::channel(params.photos.len());
Expand Down Expand Up @@ -103,17 +103,11 @@ pub async fn upload_photo_group(
let client =
clients::surreal::SurrealRootClient::new()
.await
.map_err(|_| {
PhotoUploadError::InternalError(
"Failed to create surreal client".to_string(),
)
.map_err(|e| {
let error = e.to_string();
tracing::error!("Failed to create surreal client: {}", error);
PhotoUploadError::InternalError(error)
})?;
client.use_ns("main").use_db("main").await.map_err(|e| {
PhotoUploadError::InternalError(format!(
"Failed to use surreal namespace/database: {}",
e
))
})?;

// create photo group
group.create(&client).await.map_err(|e| {
Expand Down Expand Up @@ -224,14 +218,7 @@ async fn create_photo(img: image::DynamicImage) -> Result<PhotoRecordId> {
meta: Default::default(),
};

let client = clients::surreal::SurrealRootClient::new()
.await
.wrap_err("Failed to create surreal client")?;
client
.use_ns("main")
.use_db("main")
.await
.wrap_err("Failed to use surreal namespace/database")?;
let client = clients::surreal::SurrealRootClient::new().await?;

photo
.create(&client)
Expand Down
11 changes: 11 additions & 0 deletions crates/clients/src/surreal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl SurrealRootClient {

let client = Self { client };
client.sign_in_as_root().await?;
client.use_main().await?;

Ok(client)
}
Expand All @@ -55,6 +56,16 @@ impl SurrealRootClient {

/// Consumes the client and returns the inner client.
pub fn into_inner(self) -> surrealdb::Surreal<Client> { self.client }

/// Uses the main namespace and database.
pub async fn use_main(&self) -> Result<()> {
self
.client
.use_ns("main")
.use_db("main")
.await
.wrap_err("Failed to use main namespace/database")
}
}

impl Deref for SurrealRootClient {
Expand Down

0 comments on commit e51405c

Please sign in to comment.