Rework error handling (fixes #1714) (#2135)

This commit is contained in:
Nutomic 2022-03-16 20:11:49 +00:00 committed by GitHub
parent d3d4e76a19
commit 166ec196b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
38 changed files with 155 additions and 221 deletions

View 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,

View 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| {

View 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 {

View 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

View 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;

View 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)

View 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;

View 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 })

View 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 {

View 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"))?;
}
}

View 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 {

View 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

View 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();

View 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| {

View 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);

View 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()

View 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)

View 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?;

View 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() {

View 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));
}
};

View 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(

View 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(

View 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(

View 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??;

View 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,

View 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??;

View 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 {

View 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)

View 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
}

View 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(())
}

View 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"))
}

View 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(())
}

View 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",
));
}
}
}

View 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),
}

View 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")),
}
}

View 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 },

View 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;

View 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(())
}