]> Untitled Git - lemmy.git/blobdiff - crates/api_common/src/lib.rs
Dont swallow API errors (fixes #1834) (#1837)
[lemmy.git] / crates / api_common / src / lib.rs
index 068de48a173705352d260892bb3a37011b648a43..07edab0f7240fc9d267edcb50a447a9d60d19ca3 100644 (file)
@@ -225,14 +225,14 @@ pub async fn is_mod_or_admin(
   })
   .await?;
   if !is_mod_or_admin {
-    return Err(ApiError::err("not_a_mod_or_admin").into());
+    return Err(ApiError::err_plain("not_a_mod_or_admin").into());
   }
   Ok(())
 }
 
 pub fn is_admin(local_user_view: &LocalUserView) -> Result<(), LemmyError> {
   if !local_user_view.person.admin {
-    return Err(ApiError::err("not_an_admin").into());
+    return Err(ApiError::err_plain("not_an_admin").into());
   }
   Ok(())
 }
@@ -240,7 +240,7 @@ pub fn is_admin(local_user_view: &LocalUserView) -> Result<(), LemmyError> {
 pub async fn get_post(post_id: PostId, pool: &DbPool) -> Result<Post, LemmyError> {
   blocking(pool, move |conn| Post::read(conn, post_id))
     .await?
-    .map_err(|_| ApiError::err("couldnt_find_post").into())
+    .map_err(|_| ApiError::err_plain("couldnt_find_post").into())
 }
 
 pub async fn mark_post_as_read(
@@ -254,7 +254,7 @@ pub async fn mark_post_as_read(
     PostRead::mark_as_read(conn, &post_read_form)
   })
   .await?
-  .map_err(|_| ApiError::err("couldnt_mark_post_as_read").into())
+  .map_err(|_| ApiError::err_plain("couldnt_mark_post_as_read").into())
 }
 
 pub async fn get_local_user_view_from_jwt(
@@ -263,19 +263,19 @@ pub async fn get_local_user_view_from_jwt(
   secret: &Secret,
 ) -> Result<LocalUserView, LemmyError> {
   let claims = Claims::decode(jwt, &secret.jwt_secret)
-    .map_err(|_| ApiError::err("not_logged_in"))?
+    .map_err(|e| ApiError::err("not_logged_in", e))?
     .claims;
   let local_user_id = LocalUserId(claims.sub);
   let local_user_view =
     blocking(pool, move |conn| LocalUserView::read(conn, local_user_id)).await??;
   // Check for a site ban
   if local_user_view.person.banned {
-    return Err(ApiError::err("site_ban").into());
+    return Err(ApiError::err_plain("site_ban").into());
   }
 
   // Check for user deletion
   if local_user_view.person.deleted {
-    return Err(ApiError::err("deleted").into());
+    return Err(ApiError::err_plain("deleted").into());
   }
 
   check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
@@ -290,7 +290,7 @@ pub fn check_validator_time(
 ) -> Result<(), LemmyError> {
   let user_validation_time = validator_time.timestamp();
   if user_validation_time > claims.iat {
-    Err(ApiError::err("not_logged_in").into())
+    Err(ApiError::err_plain("not_logged_in").into())
   } else {
     Ok(())
   }
@@ -313,7 +313,7 @@ pub async fn get_local_user_settings_view_from_jwt(
   secret: &Secret,
 ) -> Result<LocalUserSettingsView, LemmyError> {
   let claims = Claims::decode(jwt, &secret.jwt_secret)
-    .map_err(|_| ApiError::err("not_logged_in"))?
+    .map_err(|e| ApiError::err("not_logged_in", e))?
     .claims;
   let local_user_id = LocalUserId(claims.sub);
   let local_user_view = blocking(pool, move |conn| {
@@ -322,7 +322,7 @@ pub async fn get_local_user_settings_view_from_jwt(
   .await??;
   // Check for a site ban
   if local_user_view.person.banned {
-    return Err(ApiError::err("site_ban").into());
+    return Err(ApiError::err_plain("site_ban").into());
   }
 
   check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
@@ -351,7 +351,7 @@ pub async fn check_community_ban(
   let is_banned =
     move |conn: &'_ _| CommunityPersonBanView::get(conn, person_id, community_id).is_ok();
   if blocking(pool, is_banned).await? {
-    Err(ApiError::err("community_ban").into())
+    Err(ApiError::err_plain("community_ban").into())
   } else {
     Ok(())
   }
@@ -364,7 +364,7 @@ pub async fn check_person_block(
 ) -> Result<(), LemmyError> {
   let is_blocked = move |conn: &'_ _| PersonBlock::read(conn, potential_blocker_id, my_id).is_ok();
   if blocking(pool, is_blocked).await? {
-    Err(ApiError::err("person_block").into())
+    Err(ApiError::err_plain("person_block").into())
   } else {
     Ok(())
   }
@@ -374,7 +374,7 @@ pub async fn check_downvotes_enabled(score: i16, pool: &DbPool) -> Result<(), Le
   if score == -1 {
     let site = blocking(pool, move |conn| Site::read_simple(conn)).await??;
     if !site.enable_downvotes {
-      return Err(ApiError::err("downvotes_disabled").into());
+      return Err(ApiError::err_plain("downvotes_disabled").into());
     }
   }
   Ok(())
@@ -425,7 +425,7 @@ pub async fn build_federated_instances(
 /// Checks the password length
 pub fn password_length_check(pass: &str) -> Result<(), LemmyError> {
   if !(10..=60).contains(&pass.len()) {
-    Err(ApiError::err("invalid_password").into())
+    Err(ApiError::err_plain("invalid_password").into())
   } else {
     Ok(())
   }
@@ -434,7 +434,7 @@ pub fn password_length_check(pass: &str) -> Result<(), LemmyError> {
 /// Checks the site description length
 pub fn site_description_length_check(description: &str) -> Result<(), LemmyError> {
   if description.len() > 150 {
-    Err(ApiError::err("site_description_length_overflow").into())
+    Err(ApiError::err_plain("site_description_length_overflow").into())
   } else {
     Ok(())
   }
@@ -443,7 +443,7 @@ pub fn site_description_length_check(description: &str) -> Result<(), LemmyError
 /// Checks for a honeypot. If this field is filled, fail the rest of the function
 pub fn honeypot_check(honeypot: &Option<String>) -> Result<(), LemmyError> {
   if honeypot.is_some() {
-    Err(ApiError::err("honeypot_fail").into())
+    Err(ApiError::err_plain("honeypot_fail").into())
   } else {
     Ok(())
   }