]> Untitled Git - lemmy.git/commitdiff
Rework error handling (fixes #1714) (#2135)
authorNutomic <me@nutomic.com>
Wed, 16 Mar 2022 20:11:49 +0000 (20:11 +0000)
committerGitHub <noreply@github.com>
Wed, 16 Mar 2022 20:11:49 +0000 (20:11 +0000)
38 files changed:
crates/api/src/comment.rs
crates/api/src/comment_report.rs
crates/api/src/community.rs
crates/api/src/local_user.rs
crates/api/src/post.rs
crates/api/src/post_report.rs
crates/api/src/private_message.rs
crates/api/src/site.rs
crates/api_common/src/lib.rs
crates/api_crud/src/comment/create.rs
crates/api_crud/src/comment/delete.rs
crates/api_crud/src/comment/read.rs
crates/api_crud/src/comment/update.rs
crates/api_crud/src/community/create.rs
crates/api_crud/src/community/delete.rs
crates/api_crud/src/community/read.rs
crates/api_crud/src/community/update.rs
crates/api_crud/src/post/create.rs
crates/api_crud/src/post/read.rs
crates/api_crud/src/post/update.rs
crates/api_crud/src/private_message/create.rs
crates/api_crud/src/private_message/delete.rs
crates/api_crud/src/private_message/update.rs
crates/api_crud/src/site/create.rs
crates/api_crud/src/site/read.rs
crates/api_crud/src/site/update.rs
crates/api_crud/src/user/create.rs
crates/api_crud/src/user/delete.rs
crates/api_crud/src/user/read.rs
crates/apub/src/activities/mod.rs
crates/apub/src/fetcher/webfinger.rs
crates/apub/src/http/mod.rs
crates/apub/src/lib.rs
crates/db_schema/src/lib.rs
crates/utils/src/email.rs
crates/utils/src/lib.rs
crates/utils/src/rate_limit/rate_limiter.rs
crates/utils/src/utils.rs

index bd8d3a8e9b1efe6c5bb6b938f08900a355d8d4b7..d505669201a3d938ee658c1d980423293b7597a1 100644 (file)
@@ -58,8 +58,7 @@ impl Perform for MarkCommentAsRead {
       Comment::update_read(conn, comment_id, read)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_comment"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
 
     // Refetch it
     let comment_id = data.comment_id;
@@ -102,14 +101,12 @@ impl Perform for SaveComment {
       let save_comment = move |conn: &'_ _| CommentSaved::save(conn, &comment_saved_form);
       blocking(context.pool(), save_comment)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("couldnt_save_comment"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "couldnt_save_comment"))?;
     } else {
       let unsave_comment = move |conn: &'_ _| CommentSaved::unsave(conn, &comment_saved_form);
       blocking(context.pool(), unsave_comment)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("couldnt_save_comment"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "couldnt_save_comment"))?;
     }
 
     let comment_id = data.comment_id;
@@ -192,8 +189,7 @@ impl Perform for CreateCommentLike {
       let like = move |conn: &'_ _| CommentLike::like(conn, &like_form2);
       blocking(context.pool(), like)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("couldnt_like_comment"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
 
       Vote::send(
         &object,
index 8c3e10e581e8d9b469ee4715dfb9bd2bc2001ace..515c7103b2e297871a272618102258cce595f5ea 100644 (file)
@@ -61,8 +61,7 @@ impl Perform for CreateCommentReport {
       CommentReport::report(conn, &report_form)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_create_report"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
 
     let comment_report_view = blocking(context.pool(), move |conn| {
       CommentReportView::read(conn, report.id, person_id)
@@ -129,8 +128,7 @@ impl Perform for ResolveCommentReport {
 
     blocking(context.pool(), resolve_fun)
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("couldnt_resolve_report"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_resolve_report"))?;
 
     let report_id = data.report_id;
     let comment_report_view = blocking(context.pool(), move |conn| {
index d2e149448a4b60a2aab15f5e1708bc22e4c2bdac..d71ef7fa01ca152dbcd9a05092e6f84d9a772fe3 100644 (file)
@@ -85,15 +85,13 @@ impl Perform for FollowCommunity {
         let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
         blocking(context.pool(), follow)
           .await?
-          .map_err(LemmyError::from)
-          .map_err(|e| e.with_message("community_follower_already_exists"))?;
+          .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
       } else {
         let unfollow =
           move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
         blocking(context.pool(), unfollow)
           .await?
-          .map_err(LemmyError::from)
-          .map_err(|e| e.with_message("community_follower_already_exists"))?;
+          .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
       }
     } else if data.follow {
       // Dont actually add to the community followers here, because you need
@@ -106,8 +104,7 @@ impl Perform for FollowCommunity {
       let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
       blocking(context.pool(), unfollow)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("community_follower_already_exists"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
     }
 
     let community_id = data.community_id;
@@ -153,8 +150,7 @@ impl Perform for BlockCommunity {
       let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
       blocking(context.pool(), block)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("community_block_already_exists"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
 
       // Also, unfollow the community, and send a federated unfollow
       let community_follower_form = CommunityFollowerForm {
@@ -176,8 +172,7 @@ impl Perform for BlockCommunity {
       let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form);
       blocking(context.pool(), unblock)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("community_block_already_exists"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "community_block_already_exists"))?;
     }
 
     let community_view = blocking(context.pool(), move |conn| {
@@ -235,8 +230,7 @@ impl Perform for BanFromCommunity {
       let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
       blocking(context.pool(), ban)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("community_user_already_banned"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?;
 
       // Also unsubscribe them from the community, if they are subscribed
       let community_follower_form = CommunityFollowerForm {
@@ -264,8 +258,7 @@ impl Perform for BanFromCommunity {
       let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
       blocking(context.pool(), unban)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("community_user_already_banned"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "community_user_already_banned"))?;
       UndoBlockUser::send(
         &SiteOrCommunity::Community(community),
         &banned_person,
@@ -352,14 +345,12 @@ impl Perform for AddModToCommunity {
       let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
       blocking(context.pool(), join)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("community_moderator_already_exists"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
     } else {
       let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
       blocking(context.pool(), leave)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("community_moderator_already_exists"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
     }
 
     // Mod tables
@@ -481,8 +472,7 @@ impl Perform for TransferCommunity {
       let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
       blocking(context.pool(), join)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("community_moderator_already_exists"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
     }
 
     // Mod tables
@@ -503,16 +493,14 @@ impl Perform for TransferCommunity {
       CommunityView::read(conn, community_id, Some(person_id))
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_find_community"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
 
     let community_id = data.community_id;
     let moderators = blocking(context.pool(), move |conn| {
       CommunityModeratorView::for_community(conn, community_id)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_find_community"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
 
     // Return the jwt
     Ok(GetCommunityResponse {
index b4e2bfb190b87246c2e4750b2f5d5a7d17c8b671..01ab2ed982980f60070affc4b1edc935e27a0c74 100644 (file)
@@ -80,8 +80,7 @@ impl Perform for Login {
       LocalUserView::find_by_email_or_name(conn, &username_or_email)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_find_that_username_or_email"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_that_username_or_email"))?;
 
     // Verify the password
     let valid: bool = verify(
@@ -263,8 +262,7 @@ impl Perform for SaveUserSettings {
       Person::update(conn, person_id, &person_form)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("user_already_exists"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "user_already_exists"))?;
 
     let local_user_form = LocalUserForm {
       person_id: Some(person_id),
@@ -300,7 +298,7 @@ impl Perform for SaveUserSettings {
           "user_already_exists"
         };
 
-        return Err(LemmyError::from(e).with_message(err_type));
+        return Err(LemmyError::from_error_message(e, err_type));
       }
     };
 
@@ -397,8 +395,7 @@ impl Perform for AddAdmin {
       Person::add_admin(conn, added_person_id, added)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_user"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_user"))?;
 
     // Mod tables
     let form = ModAddForm {
@@ -447,8 +444,7 @@ impl Perform for BanPerson {
     let ban_person = move |conn: &'_ _| Person::ban_person(conn, banned_person_id, ban, expires);
     let person = blocking(context.pool(), ban_person)
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("couldnt_update_user"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_user"))?;
 
     // Remove their data if that's desired
     let remove_data = data.remove_data.unwrap_or(false);
@@ -573,14 +569,12 @@ impl Perform for BlockPerson {
       let block = move |conn: &'_ _| PersonBlock::block(conn, &person_block_form);
       blocking(context.pool(), block)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("person_block_already_exists"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "person_block_already_exists"))?;
     } else {
       let unblock = move |conn: &'_ _| PersonBlock::unblock(conn, &person_block_form);
       blocking(context.pool(), unblock)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("person_block_already_exists"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "person_block_already_exists"))?;
     }
 
     // TODO does any federated stuff need to be done here?
@@ -704,8 +698,7 @@ impl Perform for MarkPersonMentionAsRead {
       move |conn: &'_ _| PersonMention::update_read(conn, person_mention_id, read);
     blocking(context.pool(), update_mention)
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("couldnt_update_comment"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
 
     let person_mention_id = read_person_mention.id;
     let person_id = local_user_view.person.id;
@@ -754,8 +747,7 @@ impl Perform for MarkAllAsRead {
       let mark_as_read = move |conn: &'_ _| Comment::update_read(conn, reply_id, true);
       blocking(context.pool(), mark_as_read)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("couldnt_update_comment"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
     }
 
     // Mark all user mentions as read
@@ -763,15 +755,13 @@ impl Perform for MarkAllAsRead {
       move |conn: &'_ _| PersonMention::mark_all_as_read(conn, person_id);
     blocking(context.pool(), update_person_mentions)
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("couldnt_update_comment"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
 
     // Mark all private_messages as read
     let update_pm = move |conn: &'_ _| PrivateMessage::mark_all_as_read(conn, person_id);
     blocking(context.pool(), update_pm)
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("couldnt_update_private_message"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
 
     Ok(GetRepliesResponse { replies: vec![] })
   }
@@ -795,8 +785,7 @@ impl Perform for PasswordReset {
       LocalUserView::find_by_email(conn, &email)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_find_that_username_or_email"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_that_username_or_email"))?;
 
     // Email the pure token to the user.
     send_password_reset_email(&local_user_view, context.pool(), &context.settings()).await?;
@@ -836,8 +825,7 @@ impl Perform for PasswordChange {
       LocalUser::update_password(conn, local_user_id, &password)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_user"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_user"))?;
 
     // Return the jwt
     Ok(LoginResponse {
@@ -948,8 +936,7 @@ impl Perform for VerifyEmail {
       EmailVerification::read_for_token(conn, &token)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("token_not_found"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "token_not_found"))?;
 
     let form = LocalUserForm {
       // necessary in case this is a new signup
index 0b77bb95dcf59deb299f1d620506aebec1795553..6f0ab00e66e56e484f8866f19269ef3a206d4696 100644 (file)
@@ -81,8 +81,7 @@ impl Perform for CreatePostLike {
       let like = move |conn: &'_ _| PostLike::like(conn, &like_form2);
       blocking(context.pool(), like)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("couldnt_like_post"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_post"))?;
 
       Vote::send(
         &object,
@@ -321,14 +320,12 @@ impl Perform for SavePost {
       let save = move |conn: &'_ _| PostSaved::save(conn, &post_saved_form);
       blocking(context.pool(), save)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("couldnt_save_post"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "couldnt_save_post"))?;
     } else {
       let unsave = move |conn: &'_ _| PostSaved::unsave(conn, &post_saved_form);
       blocking(context.pool(), unsave)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("couldnt_save_post"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "couldnt_save_post"))?;
     }
 
     let post_id = data.post_id;
index 02afc7957ca4af4875cacee6ebc2fa39288709c0..26c4d7acafcd8930cbc4a461fb4eb4718bdb6aee 100644 (file)
@@ -72,8 +72,7 @@ impl Perform for CreatePostReport {
       PostReport::report(conn, &report_form)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_create_report"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
 
     let post_report_view = blocking(context.pool(), move |conn| {
       PostReportView::read(conn, report.id, person_id)
@@ -138,8 +137,7 @@ impl Perform for ResolvePostReport {
 
     blocking(context.pool(), resolve_fun)
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("couldnt_resolve_report"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_resolve_report"))?;
 
     let post_report_view = blocking(context.pool(), move |conn| {
       PostReportView::read(conn, report_id, person_id)
index b0d047315df02c25d672db30bfa33352d180a44d..d46280bdebdec1e19bde4f62bc4f96723fafffb7 100644 (file)
@@ -40,8 +40,7 @@ impl Perform for MarkPrivateMessageAsRead {
       PrivateMessage::update_read(conn, private_message_id, read)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_private_message"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
 
     // No need to send an apub update
     let op = UserOperation::MarkPrivateMessageAsRead;
index eab15a77b254a80debe7ae9d6ef5f751e8c9e206..e978569905b450045388567190304e7b83947376 100644 (file)
@@ -416,11 +416,9 @@ impl Perform for ResolveObject {
 
     let res = search_by_apub_id(&self.q, context)
       .await
-      .map_err(LemmyError::from)
       .map_err(|e| e.with_message("couldnt_find_object"))?;
     convert_response(res, local_user_view.map(|l| l.person.id), context.pool())
       .await
-      .map_err(LemmyError::from)
       .map_err(|e| e.with_message("couldnt_find_object"))
   }
 }
@@ -565,7 +563,6 @@ impl Perform for SaveSiteConfig {
 
     // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
     let config_hjson = Settings::save_config_file(&data.config_hjson)
-      .map_err(LemmyError::from)
       .map_err(|e| e.with_message("couldnt_update_site"))?;
 
     Ok(GetSiteConfigResponse { config_hjson })
index bc41b0c957278e155d6151a76d43cbf41fe4b739..59bbae0539b0aead1b277d3c335b30a1ce9e311c 100644 (file)
@@ -87,8 +87,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(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_find_post"))
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))
 }
 
 #[tracing::instrument(skip_all)]
@@ -103,8 +102,7 @@ pub async fn mark_post_as_read(
     PostRead::mark_as_read(conn, &post_read_form)
   })
   .await?
-  .map_err(LemmyError::from)
-  .map_err(|e| e.with_message("couldnt_mark_post_as_read"))
+  .map_err(|e| LemmyError::from_error_message(e, "couldnt_mark_post_as_read"))
 }
 
 #[tracing::instrument(skip_all)]
@@ -119,8 +117,7 @@ pub async fn mark_post_as_unread(
     PostRead::mark_as_unread(conn, &post_read_form)
   })
   .await?
-  .map_err(LemmyError::from)
-  .map_err(|e| e.with_message("couldnt_mark_post_as_read"))
+  .map_err(|e| LemmyError::from_error_message(e, "couldnt_mark_post_as_read"))
 }
 
 #[tracing::instrument(skip_all)]
@@ -130,7 +127,6 @@ pub async fn get_local_user_view_from_jwt(
   secret: &Secret,
 ) -> Result<LocalUserView, LemmyError> {
   let claims = Claims::decode(jwt, &secret.jwt_secret)
-    .map_err(LemmyError::from)
     .map_err(|e| e.with_message("not_logged_in"))?
     .claims;
   let local_user_id = LocalUserId(claims.sub);
@@ -183,7 +179,6 @@ pub async fn get_local_user_settings_view_from_jwt(
   secret: &Secret,
 ) -> Result<LocalUserSettingsView, LemmyError> {
   let claims = Claims::decode(jwt.as_ref(), &secret.jwt_secret)
-    .map_err(LemmyError::from)
     .map_err(|e| e.with_message("not_logged_in"))?
     .claims;
   let local_user_id = LocalUserId(claims.sub);
@@ -237,8 +232,7 @@ pub async fn check_community_deleted_or_removed(
 ) -> Result<(), LemmyError> {
   let community = blocking(pool, move |conn| Community::read(conn, community_id))
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_find_community"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
   if community.deleted || community.removed {
     Err(LemmyError::from_message("deleted"))
   } else {
index 1b0289b42e54e5f263312376ef465528ad516ada..bb3b8c04801c9adbd2fd94a130ee7fcaf706a663 100644 (file)
@@ -75,8 +75,7 @@ impl PerformCrud for CreateComment {
       // Make sure the parent comment exists
       let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("couldnt_create_comment"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_comment"))?;
 
       // Strange issue where sometimes the post ID is incorrect
       if parent.post_id != post_id {
@@ -98,8 +97,7 @@ impl PerformCrud for CreateComment {
       Comment::create(conn, &comment_form2)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_create_comment"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_comment"))?;
 
     // Necessary to update the ap_id
     let inserted_comment_id = inserted_comment.id;
@@ -115,7 +113,6 @@ impl PerformCrud for CreateComment {
         Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
       })
       .await?
-      .map_err(LemmyError::from)
       .map_err(|e| e.with_message("couldnt_create_comment"))?;
 
     // Scan the comment for user mentions, add those rows
@@ -142,8 +139,7 @@ impl PerformCrud for CreateComment {
     let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
     blocking(context.pool(), like)
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("couldnt_like_comment"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
 
     let apub_comment: ApubComment = updated_comment.into();
     CreateOrUpdateComment::send(
@@ -178,8 +174,7 @@ impl PerformCrud for CreateComment {
         Comment::update_read(conn, comment_id, true)
       })
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("couldnt_update_comment"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
     }
     // If its a reply, mark the parent as read
     if let Some(parent_id) = data.parent_id {
@@ -192,8 +187,7 @@ impl PerformCrud for CreateComment {
           Comment::update_read(conn, parent_id, true)
         })
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("couldnt_update_parent_comment"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_parent_comment"))?;
       }
       // If the parent has PersonMentions mark them as read too
       let person_id = local_user_view.person.id;
@@ -206,8 +200,7 @@ impl PerformCrud for CreateComment {
           PersonMention::update_read(conn, mention.id, true)
         })
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("couldnt_update_person_mentions"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_person_mentions"))?;
       }
     }
 
index adbc3cc5b668496becce853120184a54e699a2f2..9ed58946d86f352e761c7fece3e07ca35902ef21 100644 (file)
@@ -68,8 +68,7 @@ impl PerformCrud for DeleteComment {
       Comment::update_deleted(conn, comment_id, deleted)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_comment"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
 
     let post_id = updated_comment.post_id;
     let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@@ -155,8 +154,7 @@ impl PerformCrud for RemoveComment {
       Comment::update_removed(conn, comment_id, removed)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_comment"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
 
     // Mod tables
     let form = ModRemoveCommentForm {
index 56584fc37bf0d2b1040df58432ffa7fe6631c82a..77ef1f962e88801bbe7fc88324808bec29f23246 100644 (file)
@@ -41,8 +41,7 @@ impl PerformCrud for GetComment {
       CommentView::read(conn, id, person_id)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_find_comment"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_comment"))?;
 
     Ok(Self::Response {
       comment_view,
@@ -103,8 +102,7 @@ impl PerformCrud for GetComments {
         .list()
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_get_comments"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?;
 
     // Blank out deleted or removed info
     for cv in comments
index 334181f25a46d27ae66e32f1a4b59502489cca3b..4fede14548e5f21a249c7e3c15e8e07e81c37436 100644 (file)
@@ -70,8 +70,7 @@ impl PerformCrud for EditComment {
       Comment::update_content(conn, comment_id, &content_slurs_removed)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_comment"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
 
     // Do the mentions / recipients
     let updated_comment_content = updated_comment.content.to_owned();
index d78c8ad7a6e4d8722b94a2852f366a3ec44bf806..71e404d33599ed16e49e1d8278af6b4b6191ced6 100644 (file)
@@ -107,8 +107,7 @@ impl PerformCrud for CreateCommunity {
       Community::create(conn, &community_form)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("community_already_exists"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "community_already_exists"))?;
 
     // The community creator becomes a moderator
     let community_moderator_form = CommunityModeratorForm {
@@ -117,11 +116,9 @@ impl PerformCrud for CreateCommunity {
     };
 
     let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
-    if blocking(context.pool(), join).await?.is_err() {
-      return Err(LemmyError::from_message(
-        "community_moderator_already_exists",
-      ));
-    }
+    blocking(context.pool(), join)
+      .await?
+      .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
 
     // Follow your own community
     let community_follower_form = CommunityFollowerForm {
@@ -131,11 +128,9 @@ impl PerformCrud for CreateCommunity {
     };
 
     let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
-    if blocking(context.pool(), follow).await?.is_err() {
-      return Err(LemmyError::from_message(
-        "community_follower_already_exists",
-      ));
-    }
+    blocking(context.pool(), follow)
+      .await?
+      .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
 
     let person_id = local_user_view.person.id;
     let community_view = blocking(context.pool(), move |conn| {
index 17ad70035030756e2b28058f9027ead7bf6d2180..b8a2f2309ecc268002ab29bcef74637f385dd338 100644 (file)
@@ -46,8 +46,7 @@ impl PerformCrud for DeleteCommunity {
       Community::update_deleted(conn, community_id, deleted)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_community"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
 
     let res = send_community_ws_message(
       data.community_id,
@@ -98,8 +97,7 @@ impl PerformCrud for RemoveCommunity {
       Community::update_removed(conn, community_id, removed)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_community"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
 
     // Mod tables
     let expires = data.expires.map(naive_from_unix);
index 7e61bfa717f293359f65c4006e828ab99185c6e8..ca30292f3761adfcb79a20a1859ac3d3c22c6c68 100644 (file)
@@ -46,7 +46,6 @@ impl PerformCrud for GetCommunity {
         let name = data.name.to_owned().unwrap_or_else(|| "main".to_string());
         resolve_actor_identifier::<Community>(&name, context.pool())
           .await
-          .map_err(LemmyError::from)
           .map_err(|e| e.with_message("couldnt_find_community"))?
           .id
       }
@@ -56,8 +55,7 @@ impl PerformCrud for GetCommunity {
       CommunityView::read(conn, community_id, person_id)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_find_community"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
 
     // Blank out deleted or removed info for non-logged in users
     if person_id.is_none() && (community_view.community.deleted || community_view.community.removed)
@@ -69,8 +67,7 @@ impl PerformCrud for GetCommunity {
       CommunityModeratorView::for_community(conn, community_id)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_find_community"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
 
     let online = context
       .chat_server()
index a0b1dd3b4dd6faa712ba8e4aaff43c03f773be7b..b7352a62408837a8f50e66198f7e20bbeed984e8 100644 (file)
@@ -76,8 +76,7 @@ impl PerformCrud for EditCommunity {
       Community::update(conn, community_id, &community_form)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_community"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community"))?;
 
     UpdateCommunity::send(
       updated_community.into(),
@@ -139,8 +138,7 @@ impl PerformCrud for HideCommunity {
       Community::update(conn, community_id, &community_form)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_community_hidden_status"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_community_hidden_status"))?;
 
     blocking(context.pool(), move |conn| {
       ModHideCommunity::create(conn, &mod_hide_community_form)
index 81a12fc54403c91a1e15d574a409aeb0711d1f6d..b732cb13b62e870ae9905787caed02411ea7b1dd 100644 (file)
@@ -99,7 +99,7 @@ impl PerformCrud for CreatePost {
             "couldnt_create_post"
           };
 
-          return Err(LemmyError::from(e).with_message(err_type));
+          return Err(LemmyError::from_error_message(e, err_type));
         }
       };
 
@@ -114,7 +114,6 @@ impl PerformCrud for CreatePost {
       Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?)
     })
     .await?
-    .map_err(LemmyError::from)
     .map_err(|e| e.with_message("couldnt_create_post"))?;
 
     // They like their own post by default
@@ -127,9 +126,9 @@ impl PerformCrud for CreatePost {
     };
 
     let like = move |conn: &'_ _| PostLike::like(conn, &like_form);
-    if blocking(context.pool(), like).await?.is_err() {
-      return Err(LemmyError::from_message("couldnt_like_post"));
-    }
+    blocking(context.pool(), like)
+      .await?
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_post"))?;
 
     // Mark the post as read
     mark_post_as_read(person_id, post_id, context.pool()).await?;
index 8d049e70ae073db8a8b0a3e3a2ede219a891f610..6b54c5b236a2cf6f701edf8a0e103ff8540d5a8a 100644 (file)
@@ -53,8 +53,7 @@ impl PerformCrud for GetPost {
       PostView::read(conn, id, person_id)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_find_post"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?;
 
     // Mark the post as read
     if let Some(person_id) = person_id {
@@ -78,8 +77,7 @@ impl PerformCrud for GetPost {
       CommunityView::read(conn, community_id, person_id)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_find_community"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
 
     // Blank out deleted or removed info for non-logged in users
     if person_id.is_none() {
@@ -179,8 +177,7 @@ impl PerformCrud for GetPosts {
         .list()
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_get_posts"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_posts"))?;
 
     // Blank out deleted or removed info for non-logged in users
     if person_id.is_none() {
index 7a85f8638df159ce8a325c28f5b9722f758ef026..fa19c2c9bddd396b176872ff58691e3c8469716d 100644 (file)
@@ -103,7 +103,7 @@ impl PerformCrud for EditPost {
           "couldnt_update_post"
         };
 
-        return Err(LemmyError::from(e).with_message(err_type));
+        return Err(LemmyError::from_error_message(e, err_type));
       }
     };
 
index 3eddf0a5c5a1752444f0ad803cd2dfa80f93d10d..ad7fd4adf9e4eed768e1dd99a4614322d2c9532a 100644 (file)
@@ -56,7 +56,10 @@ impl PerformCrud for CreatePrivateMessage {
     {
       Ok(private_message) => private_message,
       Err(e) => {
-        return Err(LemmyError::from(e).with_message("couldnt_create_private_message"));
+        return Err(LemmyError::from_error_message(
+          e,
+          "couldnt_create_private_message",
+        ));
       }
     };
 
@@ -78,7 +81,6 @@ impl PerformCrud for CreatePrivateMessage {
       },
     )
     .await?
-    .map_err(LemmyError::from)
     .map_err(|e| e.with_message("couldnt_create_private_message"))?;
 
     CreateOrUpdatePrivateMessage::send(
index f11e9657c7ba71e916e628b519ea36207bc94a9c..00b3ff50e681e5e77991f63322e76ad4c06ccf93 100644 (file)
@@ -41,8 +41,7 @@ impl PerformCrud for DeletePrivateMessage {
       PrivateMessage::update_deleted(conn, private_message_id, deleted)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_private_message"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
 
     // Send the apub update
     send_apub_delete_private_message(
index 15e84f2c6abe36b432c037677aa883c931a0e7d3..5f23f1bcd4da3fc5879444e2be01a3dcae7fab2d 100644 (file)
@@ -44,8 +44,7 @@ impl PerformCrud for EditPrivateMessage {
       PrivateMessage::update_content(conn, private_message_id, &content_slurs_removed)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_private_message"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
 
     // Send the apub update
     CreateOrUpdatePrivateMessage::send(
index b1c69010bbad110bc3bdd8b7ce06b235430a38b6..ddb4f0a0c3b8e9f2953b9afc5a445c493d4ac215 100644 (file)
@@ -85,9 +85,9 @@ impl PerformCrud for CreateSite {
     };
 
     let create_site = move |conn: &'_ _| Site::create(conn, &site_form);
-    if blocking(context.pool(), create_site).await?.is_err() {
-      return Err(LemmyError::from_message("site_already_exists"));
-    }
+    blocking(context.pool(), create_site)
+      .await?
+      .map_err(|e| LemmyError::from_error_message(e, "site_already_exists"))?;
 
     let site_view = blocking(context.pool(), SiteView::read_local).await??;
 
index b726de034240ae1e7b755025b61c341cf2bb8f83..2e8269de8b58149b310c5ca28e24f92f530f51a7 100644 (file)
@@ -101,31 +101,27 @@ impl PerformCrud for GetSite {
         CommunityFollowerView::for_person(conn, person_id)
       })
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("system_err_login"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
 
       let person_id = local_user_view.person.id;
       let community_blocks = blocking(context.pool(), move |conn| {
         CommunityBlockView::for_person(conn, person_id)
       })
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("system_err_login"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
 
       let person_id = local_user_view.person.id;
       let person_blocks = blocking(context.pool(), move |conn| {
         PersonBlockView::for_person(conn, person_id)
       })
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("system_err_login"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
 
       let moderates = blocking(context.pool(), move |conn| {
         CommunityModeratorView::for_person(conn, person_id)
       })
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("system_err_login"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "system_err_login"))?;
 
       Some(MyUserInfo {
         local_user_view,
index 2fc980e3c04c4ebdcf9943cac912e0caadb06704..4e987de9d6af62769b0384659ca956c3322dcbfd 100644 (file)
@@ -77,8 +77,7 @@ impl PerformCrud for EditSite {
       Site::update(conn, local_site.id, &site_form)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("couldnt_update_site"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_site"))?;
 
     // TODO can't think of a better way to do this.
     // If the server suddenly requires email verification, or required applications, no old users
@@ -90,8 +89,7 @@ impl PerformCrud for EditSite {
         LocalUser::set_all_users_registration_applications_accepted(conn)
       })
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("couldnt_set_all_registrations_accepted"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_registrations_accepted"))?;
     }
 
     if !local_site.require_email_verification && update_site.require_email_verification {
@@ -99,8 +97,7 @@ impl PerformCrud for EditSite {
         LocalUser::set_all_users_email_verified(conn)
       })
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("couldnt_set_all_email_verified"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_email_verified"))?;
     }
 
     let site_view = blocking(context.pool(), SiteView::read_local).await??;
index 06b576e51a46c81d0aec209a8fd56cb58c47bd86..be746d2ae39b34346794f8e3427c00582661a34f 100644 (file)
@@ -141,8 +141,7 @@ impl PerformCrud for Register {
       Person::create(conn, &person_form)
     })
     .await?
-    .map_err(LemmyError::from)
-    .map_err(|e| e.with_message("user_already_exists"))?;
+    .map_err(|e| LemmyError::from_error_message(e, "user_already_exists"))?;
 
     // Create the local user
     let local_user_form = LocalUserForm {
@@ -175,7 +174,7 @@ impl PerformCrud for Register {
         })
         .await??;
 
-        return Err(LemmyError::from(e).with_message(err_type));
+        return Err(LemmyError::from_error_message(e, err_type));
       }
     };
 
@@ -240,8 +239,7 @@ impl PerformCrud for Register {
     let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
     blocking(context.pool(), follow)
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("community_follower_already_exists"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "community_follower_already_exists"))?;
 
     // If its an admin, add them as a mod and follower to main
     if no_admins {
@@ -253,8 +251,7 @@ impl PerformCrud for Register {
       let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
       blocking(context.pool(), join)
         .await?
-        .map_err(LemmyError::from)
-        .map_err(|e| e.with_message("community_moderator_already_exists"))?;
+        .map_err(|e| LemmyError::from_error_message(e, "community_moderator_already_exists"))?;
     }
 
     let mut login_response = LoginResponse {
index c3977e72614beb7b7642dd2bcb6a2e4f371288b7..ae92c12e32ad9bf9bda1bc4acdf84de4fdce8048 100644 (file)
@@ -35,15 +35,13 @@ impl PerformCrud for DeleteAccount {
     let permadelete = move |conn: &'_ _| Comment::permadelete_for_creator(conn, person_id);
     blocking(context.pool(), permadelete)
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("couldnt_update_comment"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
 
     // Posts
     let permadelete = move |conn: &'_ _| Post::permadelete_for_creator(conn, person_id);
     blocking(context.pool(), permadelete)
       .await?
-      .map_err(LemmyError::from)
-      .map_err(|e| e.with_message("couldnt_update_post"))?;
+      .map_err(|e| LemmyError::from_error_message(e, "couldnt_update_post"))?;
 
     blocking(context.pool(), move |conn| {
       Person::delete_account(conn, person_id)
index 9368fabdfdf13771d38066be0e9ad9a223be3f72..e98d2ff0c393b6dbd338549ba8600a6fe374930b 100644 (file)
@@ -53,7 +53,6 @@ impl PerformCrud for GetPersonDetails {
 
         resolve_actor_identifier::<Person>(&name, context.pool())
           .await
-          .map_err(LemmyError::from)
           .map_err(|e| e.with_message("couldnt_find_that_username_or_email"))?
           .id
       }
index 0624b32f3384e1aa1c939707b034952a73d8f46e..6334584a41715248781959bbe28ac4feb3a484a4 100644 (file)
@@ -6,6 +6,7 @@ use crate::{
   objects::{community::ApubCommunity, person::ApubPerson},
 };
 use activitystreams_kinds::public;
+use anyhow::anyhow;
 use lemmy_api_common::blocking;
 use lemmy_apub_lib::{
   activity_queue::send_activity,
@@ -44,8 +45,8 @@ async fn verify_person(
     .dereference(context, context.client(), request_counter)
     .await?;
   if person.banned {
-    let error = LemmyError::from(anyhow::anyhow!("Person {} is banned", person_id));
-    return Err(error.with_message("banned"));
+    let err = anyhow!("Person {} is banned", person_id);
+    return Err(LemmyError::from_error_message(err, "banned"));
   }
   Ok(())
 }
index d039c1c1744158c6a5d7295aacb8496d07859c26..918e1de0fe393a2b615325f86ef5e5c3c173db68 100644 (file)
@@ -1,3 +1,4 @@
+use anyhow::anyhow;
 use itertools::Itertools;
 use lemmy_apub_lib::{
   object_id::ObjectId,
@@ -83,9 +84,6 @@ where
       return object.map(|o| o.actor_id().into());
     }
   }
-  let error = LemmyError::from(anyhow::anyhow!(
-    "Failed to resolve actor for {}",
-    identifier
-  ));
-  Err(error.with_message("failed_to_resolve"))
+  let err = anyhow!("Failed to resolve actor for {}", identifier);
+  Err(LemmyError::from_error_message(err, "failed_to_resolve"))
 }
index 1cf705ae24e5122460883c1c7a3dad19343de299..3328e509deac10add4e491ad7c47d0624c192564 100644 (file)
@@ -12,7 +12,7 @@ use actix_web::{
   HttpRequest,
   HttpResponse,
 };
-use anyhow::Context;
+use anyhow::{anyhow, Context};
 use futures::StreamExt;
 use http::StatusCode;
 use lemmy_api_common::blocking;
@@ -181,11 +181,14 @@ fn assert_activity_not_local(id: &Url, hostname: &str) -> Result<(), LemmyError>
   let activity_domain = id.domain().context(location_info!())?;
 
   if activity_domain == hostname {
-    let error = LemmyError::from(anyhow::anyhow!(
+    let err = anyhow!(
       "Error: received activity which was sent by local instance: {:?}",
       id
+    );
+    return Err(LemmyError::from_error_message(
+      err,
+      "received_local_activity",
     ));
-    return Err(error.with_message("received_local_activity"));
   }
   Ok(())
 }
index 0652048dc3d23f93ea6e3e4ebb42c3d056b2bb99..b540ac2ba5f3e6d5111e9e298c107e92d08555d2 100644 (file)
@@ -1,5 +1,5 @@
 use crate::fetcher::post_or_comment::PostOrComment;
-use anyhow::Context;
+use anyhow::{anyhow, Context};
 use lemmy_api_common::blocking;
 use lemmy_db_schema::{newtypes::DbUrl, source::activity::Activity, DbPool};
 use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
@@ -41,28 +41,24 @@ pub(crate) fn check_is_apub_id_valid(
     return if domain == local_instance {
       Ok(())
     } else {
-      let error = LemmyError::from(anyhow::anyhow!(
+      let err = anyhow!(
         "Trying to connect with {}, but federation is disabled",
         domain
-      ));
-      Err(error.with_message("federation_disabled"))
+      );
+      Err(LemmyError::from_error_message(err, "federation_disabled"))
     };
   }
 
   let host = apub_id.host_str().context(location_info!())?;
   let host_as_ip = host.parse::<IpAddr>();
   if host == "localhost" || host_as_ip.is_ok() {
-    let error = LemmyError::from(anyhow::anyhow!("invalid hostname {}: {}", host, apub_id));
-    return Err(error.with_message("invalid_hostname"));
+    let err = anyhow!("invalid hostname {}: {}", host, apub_id);
+    return Err(LemmyError::from_error_message(err, "invalid_hostname"));
   }
 
   if apub_id.scheme() != settings.get_protocol_string() {
-    let error = LemmyError::from(anyhow::anyhow!(
-      "invalid apub id scheme {}: {}",
-      apub_id.scheme(),
-      apub_id
-    ));
-    return Err(error.with_message("invalid_scheme"));
+    let err = anyhow!("invalid apub id scheme {}: {}", apub_id.scheme(), apub_id);
+    return Err(LemmyError::from_error_message(err, "invalid_scheme"));
   }
 
   // TODO: might be good to put the part above in one method, and below in another
@@ -70,8 +66,8 @@ pub(crate) fn check_is_apub_id_valid(
   //        -> no that doesnt make sense, we still need the code below for blocklist and strict allowlist
   if let Some(blocked) = settings.to_owned().federation.blocked_instances {
     if blocked.contains(&domain) {
-      let error = LemmyError::from(anyhow::anyhow!("{} is in federation blocklist", domain));
-      return Err(error.with_message("federation_blocked"));
+      let err = anyhow!("{} is in federation blocklist", domain);
+      return Err(LemmyError::from_error_message(err, "federation_blocked"));
     }
   }
 
@@ -84,8 +80,11 @@ pub(crate) fn check_is_apub_id_valid(
       allowed.push(local_instance);
 
       if !allowed.contains(&domain) {
-        let error = LemmyError::from(anyhow::anyhow!("{} not in federation allowlist", domain));
-        return Err(error.with_message("federation_not_allowed"));
+        let err = anyhow!("{} not in federation allowlist", domain);
+        return Err(LemmyError::from_error_message(
+          err,
+          "federation_not_allowed",
+        ));
       }
     }
   }
index e726aaa5bdeac22ec437ddc880940f7fb0f6b36e..5388d37c2b8a172d76e7213ddde81a4906d444e1 100644 (file)
@@ -106,7 +106,7 @@ pub fn diesel_option_overwrite_to_url(
     Some("") => Ok(Some(None)),
     Some(str_url) => match Url::parse(str_url) {
       Ok(url) => Ok(Some(Some(url.into()))),
-      Err(e) => Err(LemmyError::from(e).with_message("invalid_url")),
+      Err(e) => Err(LemmyError::from_error_message(e, "invalid_url")),
     },
     None => Ok(None),
   }
index a52ed405557c9f124d6816a8f217cd7bba491f9b..dfd66436b6c218adfcc82333fd20ee96408ab478 100644 (file)
@@ -98,6 +98,6 @@ pub fn send_email(
 
   match result {
     Ok(_) => Ok(()),
-    Err(e) => Err(LemmyError::from(e).with_message("email_send_failed")),
+    Err(e) => Err(LemmyError::from_error_message(e, "email_send_failed")),
   }
 }
index 04c60e3fca5854360ac4eb34ccd338f5f948b61a..6e30104e8fa5c555c328b6c6592002f2ad560bf8 100644 (file)
@@ -59,6 +59,7 @@ pub struct LemmyError {
 }
 
 impl LemmyError {
+  /// Create LemmyError from a message, including stack trace
   pub fn from_message(message: &'static str) -> Self {
     let inner = anyhow::anyhow!("{}", message);
     LemmyError {
@@ -67,12 +68,27 @@ impl LemmyError {
       context: SpanTrace::capture(),
     }
   }
+
+  /// Create a LemmyError from error and message, including stack trace
+  pub fn from_error_message<E>(error: E, message: &'static str) -> Self
+  where
+    E: Into<anyhow::Error>,
+  {
+    LemmyError {
+      message: Some(message),
+      inner: error.into(),
+      context: SpanTrace::capture(),
+    }
+  }
+
+  /// Add message to existing LemmyError (or overwrite existing error)
   pub fn with_message(self, message: &'static str) -> Self {
     LemmyError {
       message: Some(message),
       ..self
     }
   }
+
   pub fn to_json(&self) -> Result<String, Self> {
     let api_error = match self.message {
       Some(error) => ApiError { error },
index 580ed3e7de137d3b9d6bfd80b9b1559b1d2e5d0d..75b7808d3dede22fd1a056bf337dc423c4cb4457 100644 (file)
@@ -79,14 +79,16 @@ impl RateLimiter {
             time_passed,
             rate_limit.allowance
           );
-          let error = LemmyError::from(anyhow::anyhow!(
-            "Too many requests. type: {}, IP: {}, {} per {} seconds",
-            type_.as_ref(),
-            ip,
-            rate,
-            per
-          ));
-          Err(error.with_message("too_many_requests"))
+          Err(LemmyError::from_error_message(
+            anyhow::anyhow!(
+              "Too many requests. type: {}, IP: {}, {} per {} seconds",
+              type_.as_ref(),
+              ip,
+              rate,
+              per
+            ),
+            "too_many_requests",
+          ))
         } else {
           if !check_only {
             rate_limit.allowance -= 1.0;
index a1fefffe84a3ff958956b3c57012e7877774b71b..f9c68f4ef6db08f36d2608655b2d36f02c08a557 100644 (file)
@@ -62,8 +62,10 @@ pub(crate) fn slur_check<'a>(
 
 pub fn check_slurs(text: &str, slur_regex: &Option<Regex>) -> Result<(), LemmyError> {
   if let Err(slurs) = slur_check(text, slur_regex) {
-    let error = LemmyError::from(anyhow::anyhow!("{}", slurs_vec_to_str(slurs)));
-    Err(error.with_message("slurs"))
+    Err(LemmyError::from_error_message(
+      anyhow::anyhow!("{}", slurs_vec_to_str(slurs)),
+      "slurs",
+    ))
   } else {
     Ok(())
   }