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;
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;
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,
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)
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| {
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
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;
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 {
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| {
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 {
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,
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
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
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 {
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(
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),
"user_already_exists"
};
- return Err(LemmyError::from(e).with_message(err_type));
+ return Err(LemmyError::from_error_message(e, err_type));
}
};
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 {
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);
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?
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;
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
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![] })
}
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?;
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 {
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
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,
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;
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)
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)
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;
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"))
}
}
// 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 })
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)]
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)]
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)]
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);
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);
) -> 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 {
// 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 {
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;
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
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(
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 {
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;
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"))?;
}
}
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??;
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 {
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,
.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
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();
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 {
};
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 {
};
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| {
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,
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);
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
}
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)
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()
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(),
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)
"couldnt_create_post"
};
- return Err(LemmyError::from(e).with_message(err_type));
+ return Err(LemmyError::from_error_message(e, err_type));
}
};
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
};
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?;
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 {
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() {
.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() {
"couldnt_update_post"
};
- return Err(LemmyError::from(e).with_message(err_type));
+ return Err(LemmyError::from_error_message(e, err_type));
}
};
{
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",
+ ));
}
};
},
)
.await?
- .map_err(LemmyError::from)
.map_err(|e| e.with_message("couldnt_create_private_message"))?;
CreateOrUpdatePrivateMessage::send(
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(
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(
};
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??;
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,
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
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 {
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??;
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 {
})
.await??;
- return Err(LemmyError::from(e).with_message(err_type));
+ return Err(LemmyError::from_error_message(e, err_type));
}
};
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 {
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 {
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)
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
}
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,
.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(())
}
+use anyhow::anyhow;
use itertools::Itertools;
use lemmy_apub_lib::{
object_id::ObjectId,
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"))
}
HttpRequest,
HttpResponse,
};
-use anyhow::Context;
+use anyhow::{anyhow, Context};
use futures::StreamExt;
use http::StatusCode;
use lemmy_api_common::blocking;
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(())
}
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};
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
// -> 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"));
}
}
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",
+ ));
}
}
}
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),
}
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")),
}
}
}
impl LemmyError {
+ /// Create LemmyError from a message, including stack trace
pub fn from_message(message: &'static str) -> Self {
let inner = anyhow::anyhow!("{}", message);
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 },
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;
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(())
}