Skip to content

Commit

Permalink
Merge pull request #90 from picture-pro/unify-server-errors
Browse files Browse the repository at this point in the history
Unify server errors
  • Loading branch information
johnbchron authored Mar 12, 2024
2 parents c8d76df + d8a9db1 commit 3e5501a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 40 deletions.
24 changes: 4 additions & 20 deletions crates/bl/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ pub async fn fetch_user_owned_photo_groups(
Ok(groups)
}
.await
.map_err(|e: Report| {
let error = e.to_string();
tracing::error!("Failed to fetch user owned photo groups: {}", error);
ServerFnError::new(error)
})
.map_err(|e: Report| crate::handle_error(e, "fetch user owned photo groups"))
}

#[server(
Expand All @@ -62,11 +58,7 @@ pub async fn fetch_photo_group(
Ok(group)
}
.await
.map_err(|e: Report| {
let error = e.to_string();
tracing::error!("Failed to fetch photo group: {}", error);
ServerFnError::new(error)
})
.map_err(|e: Report| crate::handle_error(e, "fetch photo group"))
}

#[server(
Expand All @@ -91,11 +83,7 @@ pub async fn fetch_user(
.wrap_err("Failed to fetch user")
}
.await
.map_err(|e: Report| {
let error = e.to_string();
tracing::error!("Failed to fetch user: {}", error);
ServerFnError::new(error)
})
.map_err(|e: Report| crate::handle_error(e, "fetch user"))
}

#[server(
Expand Down Expand Up @@ -153,9 +141,5 @@ pub async fn fetch_photo_thumbnail(
}))
}
.await
.map_err(|e: Report| {
let error = e.to_string();
tracing::error!("Failed to fetch photo thumbnail: {}", error);
ServerFnError::new(error)
})
.map_err(|e: Report| crate::handle_error(e, "fetch photo thumbnail"))
}
11 changes: 11 additions & 0 deletions crates/bl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
use leptos::ServerFnError;

pub mod fetch;
#[cfg(feature = "ssr")]
pub mod model_ext;
pub mod qr_code;
pub mod rmp_sfn;
pub mod upload;

#[cfg(feature = "ssr")]
pub fn handle_error(
error: color_eyre::eyre::Report,
failed_action: &'static str,
) -> ServerFnError {
tracing::error!("Failed to {}: {:?}", failed_action, &error);
ServerFnError::new(error)
}
7 changes: 2 additions & 5 deletions crates/bl/src/qr_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ pub fn generate_qr_code_inner(data: &str) -> color_eyre::eyre::Result<String> {
)]
#[cfg_attr(feature = "ssr", tracing::instrument)]
pub async fn generate_qr_code(data: String) -> Result<String, ServerFnError> {
generate_qr_code_inner(&data).map_err(|e| {
let error = e.to_string();
tracing::error!("Failed to generate QR code: {}", error);
ServerFnError::new(error)
})
generate_qr_code_inner(&data)
.map_err(|e| crate::handle_error(e, "generate qr code"))
}
17 changes: 2 additions & 15 deletions crates/site-app/src/components/gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub fn Gallery() -> impl IntoView {
.into_view();
};

let photo_groups = create_resource(move || user.id, fetch_user_photo_groups);
let photo_groups =
create_resource(move || user.id, bl::fetch::fetch_user_owned_photo_groups);

view! {
<Suspense fallback=|| view!{ }>
Expand Down Expand Up @@ -57,17 +58,3 @@ fn PhotoGroupList(groups: Vec<core_types::PhotoGroup>) -> impl IntoView {
}
.into_view()
}

#[cfg_attr(feature = "ssr", tracing::instrument)]
#[server]
pub async fn fetch_user_photo_groups(
user_id: core_types::UserRecordId,
) -> Result<Vec<core_types::PhotoGroup>, ServerFnError> {
bl::fetch::fetch_user_owned_photo_groups(user_id)
.await
.map_err(|e| {
let error = format!("Failed to fetch user photo groups: {:?}", e);
tracing::error!("{error}");
ServerFnError::new(error)
})
}

0 comments on commit 3e5501a

Please sign in to comment.