From: Nutomic Date: Wed, 16 Mar 2022 20:11:49 +0000 (+0000) Subject: Rework error handling (fixes #1714) (#2135) X-Git-Url: http://these/git/%22%7Burl%7D/%22https:/image.com/%7B%60%24%7BghostArchiveUrl%7D/%7BimageSrc%7D?a=commitdiff_plain;h=166ec196b02d82115ab178588604f4a780d0c135;p=lemmy.git Rework error handling (fixes #1714) (#2135) --- diff --git a/crates/api/src/comment.rs b/crates/api/src/comment.rs index bd8d3a8e..d5056692 100644 --- a/crates/api/src/comment.rs +++ b/crates/api/src/comment.rs @@ -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, diff --git a/crates/api/src/comment_report.rs b/crates/api/src/comment_report.rs index 8c3e10e5..515c7103 100644 --- a/crates/api/src/comment_report.rs +++ b/crates/api/src/comment_report.rs @@ -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| { diff --git a/crates/api/src/community.rs b/crates/api/src/community.rs index d2e14944..d71ef7fa 100644 --- a/crates/api/src/community.rs +++ b/crates/api/src/community.rs @@ -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 { diff --git a/crates/api/src/local_user.rs b/crates/api/src/local_user.rs index b4e2bfb1..01ab2ed9 100644 --- a/crates/api/src/local_user.rs +++ b/crates/api/src/local_user.rs @@ -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 diff --git a/crates/api/src/post.rs b/crates/api/src/post.rs index 0b77bb95..6f0ab00e 100644 --- a/crates/api/src/post.rs +++ b/crates/api/src/post.rs @@ -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; diff --git a/crates/api/src/post_report.rs b/crates/api/src/post_report.rs index 02afc795..26c4d7ac 100644 --- a/crates/api/src/post_report.rs +++ b/crates/api/src/post_report.rs @@ -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) diff --git a/crates/api/src/private_message.rs b/crates/api/src/private_message.rs index b0d04731..d46280bd 100644 --- a/crates/api/src/private_message.rs +++ b/crates/api/src/private_message.rs @@ -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; diff --git a/crates/api/src/site.rs b/crates/api/src/site.rs index eab15a77..e9785699 100644 --- a/crates/api/src/site.rs +++ b/crates/api/src/site.rs @@ -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 }) diff --git a/crates/api_common/src/lib.rs b/crates/api_common/src/lib.rs index bc41b0c9..59bbae05 100644 --- a/crates/api_common/src/lib.rs +++ b/crates/api_common/src/lib.rs @@ -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 { 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 { 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 { 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 { diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index 1b0289b4..bb3b8c04 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -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"))?; } } diff --git a/crates/api_crud/src/comment/delete.rs b/crates/api_crud/src/comment/delete.rs index adbc3cc5..9ed58946 100644 --- a/crates/api_crud/src/comment/delete.rs +++ b/crates/api_crud/src/comment/delete.rs @@ -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 { diff --git a/crates/api_crud/src/comment/read.rs b/crates/api_crud/src/comment/read.rs index 56584fc3..77ef1f96 100644 --- a/crates/api_crud/src/comment/read.rs +++ b/crates/api_crud/src/comment/read.rs @@ -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 diff --git a/crates/api_crud/src/comment/update.rs b/crates/api_crud/src/comment/update.rs index 334181f2..4fede145 100644 --- a/crates/api_crud/src/comment/update.rs +++ b/crates/api_crud/src/comment/update.rs @@ -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(); diff --git a/crates/api_crud/src/community/create.rs b/crates/api_crud/src/community/create.rs index d78c8ad7..71e404d3 100644 --- a/crates/api_crud/src/community/create.rs +++ b/crates/api_crud/src/community/create.rs @@ -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| { diff --git a/crates/api_crud/src/community/delete.rs b/crates/api_crud/src/community/delete.rs index 17ad7003..b8a2f230 100644 --- a/crates/api_crud/src/community/delete.rs +++ b/crates/api_crud/src/community/delete.rs @@ -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); diff --git a/crates/api_crud/src/community/read.rs b/crates/api_crud/src/community/read.rs index 7e61bfa7..ca30292f 100644 --- a/crates/api_crud/src/community/read.rs +++ b/crates/api_crud/src/community/read.rs @@ -46,7 +46,6 @@ impl PerformCrud for GetCommunity { let name = data.name.to_owned().unwrap_or_else(|| "main".to_string()); resolve_actor_identifier::(&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() diff --git a/crates/api_crud/src/community/update.rs b/crates/api_crud/src/community/update.rs index a0b1dd3b..b7352a62 100644 --- a/crates/api_crud/src/community/update.rs +++ b/crates/api_crud/src/community/update.rs @@ -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) diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index 81a12fc5..b732cb13 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -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?; diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index 8d049e70..6b54c5b2 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -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() { diff --git a/crates/api_crud/src/post/update.rs b/crates/api_crud/src/post/update.rs index 7a85f863..fa19c2c9 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -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)); } }; diff --git a/crates/api_crud/src/private_message/create.rs b/crates/api_crud/src/private_message/create.rs index 3eddf0a5..ad7fd4ad 100644 --- a/crates/api_crud/src/private_message/create.rs +++ b/crates/api_crud/src/private_message/create.rs @@ -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( diff --git a/crates/api_crud/src/private_message/delete.rs b/crates/api_crud/src/private_message/delete.rs index f11e9657..00b3ff50 100644 --- a/crates/api_crud/src/private_message/delete.rs +++ b/crates/api_crud/src/private_message/delete.rs @@ -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( diff --git a/crates/api_crud/src/private_message/update.rs b/crates/api_crud/src/private_message/update.rs index 15e84f2c..5f23f1bc 100644 --- a/crates/api_crud/src/private_message/update.rs +++ b/crates/api_crud/src/private_message/update.rs @@ -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( diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index b1c69010..ddb4f0a0 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -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??; diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index b726de03..2e8269de 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -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, diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index 2fc980e3..4e987de9 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -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??; diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index 06b576e5..be746d2a 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -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 { diff --git a/crates/api_crud/src/user/delete.rs b/crates/api_crud/src/user/delete.rs index c3977e72..ae92c12e 100644 --- a/crates/api_crud/src/user/delete.rs +++ b/crates/api_crud/src/user/delete.rs @@ -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) diff --git a/crates/api_crud/src/user/read.rs b/crates/api_crud/src/user/read.rs index 9368fabd..e98d2ff0 100644 --- a/crates/api_crud/src/user/read.rs +++ b/crates/api_crud/src/user/read.rs @@ -53,7 +53,6 @@ impl PerformCrud for GetPersonDetails { resolve_actor_identifier::(&name, context.pool()) .await - .map_err(LemmyError::from) .map_err(|e| e.with_message("couldnt_find_that_username_or_email"))? .id } diff --git a/crates/apub/src/activities/mod.rs b/crates/apub/src/activities/mod.rs index 0624b32f..6334584a 100644 --- a/crates/apub/src/activities/mod.rs +++ b/crates/apub/src/activities/mod.rs @@ -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(()) } diff --git a/crates/apub/src/fetcher/webfinger.rs b/crates/apub/src/fetcher/webfinger.rs index d039c1c1..918e1de0 100644 --- a/crates/apub/src/fetcher/webfinger.rs +++ b/crates/apub/src/fetcher/webfinger.rs @@ -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")) } diff --git a/crates/apub/src/http/mod.rs b/crates/apub/src/http/mod.rs index 1cf705ae..3328e509 100644 --- a/crates/apub/src/http/mod.rs +++ b/crates/apub/src/http/mod.rs @@ -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(()) } diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index 0652048d..b540ac2b 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -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::(); 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", + )); } } } diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index e726aaa5..5388d37c 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -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), } diff --git a/crates/utils/src/email.rs b/crates/utils/src/email.rs index a52ed405..dfd66436 100644 --- a/crates/utils/src/email.rs +++ b/crates/utils/src/email.rs @@ -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")), } } diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs index 04c60e3f..6e30104e 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -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(error: E, message: &'static str) -> Self + where + E: Into, + { + 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 { let api_error = match self.message { Some(error) => ApiError { error }, diff --git a/crates/utils/src/rate_limit/rate_limiter.rs b/crates/utils/src/rate_limit/rate_limiter.rs index 580ed3e7..75b7808d 100644 --- a/crates/utils/src/rate_limit/rate_limiter.rs +++ b/crates/utils/src/rate_limit/rate_limiter.rs @@ -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; diff --git a/crates/utils/src/utils.rs b/crates/utils/src/utils.rs index a1fefffe..f9c68f4e 100644 --- a/crates/utils/src/utils.rs +++ b/crates/utils/src/utils.rs @@ -62,8 +62,10 @@ pub(crate) fn slur_check<'a>( pub fn check_slurs(text: &str, slur_regex: &Option) -> 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(()) }