From 5b8fef624d17607005b7d0d28fcb820044ee3817 Mon Sep 17 00:00:00 2001 From: Philippe Loctaux Date: Wed, 8 Mar 2023 23:11:33 +0100 Subject: [PATCH] avatar: return 404 if user does not exist --- crates/ezidam/src/error.rs | 4 ++++ crates/ezidam/src/routes/root.rs | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/ezidam/src/error.rs b/crates/ezidam/src/error.rs index 05e109a..70e0eb4 100644 --- a/crates/ezidam/src/error.rs +++ b/crates/ezidam/src/error.rs @@ -39,4 +39,8 @@ impl Error { pub fn internal_server_error(error: E) -> Self { Self::new(Status::InternalServerError, error) } + + pub fn not_found(value: M) -> Self { + Self::new(Status::NotFound, value) + } } diff --git a/crates/ezidam/src/routes/root.rs b/crates/ezidam/src/routes/root.rs index a7a41c3..70a9f96 100644 --- a/crates/ezidam/src/routes/root.rs +++ b/crates/ezidam/src/routes/root.rs @@ -47,7 +47,9 @@ async fn avatar( size: Option, ) -> Result { // Verify existence of user - let _user = User::get_by_id(&mut *db, &user_id.0).await?; + let _user = User::get_by_id(&mut *db, &user_id.0) + .await? + .ok_or_else(|| Error::not_found(user_id.0.to_string()))?; // Generate avatar let avatar = task::spawn_blocking(move || {