Dont swallow API errors (fixes #1834) (#1837)

Dont swallow API errors (fixes #1834)
This commit is contained in:
Nutomic 2021-10-13 19:50:21 +00:00 committed by GitHub
parent d26255957b
commit 8067244765
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 224 additions and 234 deletions

View File

@ -50,7 +50,7 @@ impl Perform for MarkCommentAsRead {
// Verify that only the recipient can mark as read // Verify that only the recipient can mark as read
if local_user_view.person.id != orig_comment.get_recipient_id() { if local_user_view.person.id != orig_comment.get_recipient_id() {
return Err(ApiError::err("no_comment_edit_allowed").into()); return Err(ApiError::err_plain("no_comment_edit_allowed").into());
} }
// Do the mark as read // Do the mark as read
@ -59,7 +59,7 @@ impl Perform for MarkCommentAsRead {
Comment::update_read(conn, comment_id, read) Comment::update_read(conn, comment_id, read)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_comment"))?; .map_err(|_| ApiError::err_plain("couldnt_update_comment"))?;
// Refetch it // Refetch it
let comment_id = data.comment_id; let comment_id = data.comment_id;
@ -99,14 +99,14 @@ impl Perform for SaveComment {
if data.save { if data.save {
let save_comment = move |conn: &'_ _| CommentSaved::save(conn, &comment_saved_form); let save_comment = move |conn: &'_ _| CommentSaved::save(conn, &comment_saved_form);
if blocking(context.pool(), save_comment).await?.is_err() { blocking(context.pool(), save_comment)
return Err(ApiError::err("couldnt_save_comment").into()); .await?
} .map_err(|e| ApiError::err("couldnt_save_comment", e))?;
} else { } else {
let unsave_comment = move |conn: &'_ _| CommentSaved::unsave(conn, &comment_saved_form); let unsave_comment = move |conn: &'_ _| CommentSaved::unsave(conn, &comment_saved_form);
if blocking(context.pool(), unsave_comment).await?.is_err() { blocking(context.pool(), unsave_comment)
return Err(ApiError::err("couldnt_save_comment").into()); .await?
} .map_err(|e| ApiError::err("couldnt_save_comment", e))?;
} }
let comment_id = data.comment_id; let comment_id = data.comment_id;
@ -193,9 +193,9 @@ impl Perform for CreateCommentLike {
if do_add { if do_add {
let like_form2 = like_form.clone(); let like_form2 = like_form.clone();
let like = move |conn: &'_ _| CommentLike::like(conn, &like_form2); let like = move |conn: &'_ _| CommentLike::like(conn, &like_form2);
if blocking(context.pool(), like).await?.is_err() { blocking(context.pool(), like)
return Err(ApiError::err("couldnt_like_comment").into()); .await?
} .map_err(|e| ApiError::err("couldnt_like_comment", e))?;
Vote::send( Vote::send(
&object, &object,

View File

@ -33,10 +33,10 @@ impl Perform for CreateCommentReport {
// check size of report and check for whitespace // check size of report and check for whitespace
let reason = data.reason.trim(); let reason = data.reason.trim();
if reason.is_empty() { if reason.is_empty() {
return Err(ApiError::err("report_reason_required").into()); return Err(ApiError::err_plain("report_reason_required").into());
} }
if reason.chars().count() > 1000 { if reason.chars().count() > 1000 {
return Err(ApiError::err("report_too_long").into()); return Err(ApiError::err_plain("report_too_long").into());
} }
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
@ -59,7 +59,7 @@ impl Perform for CreateCommentReport {
CommentReport::report(conn, &report_form) CommentReport::report(conn, &report_form)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_create_report"))?; .map_err(|e| ApiError::err("couldnt_create_report", e))?;
let comment_report_view = blocking(context.pool(), move |conn| { let comment_report_view = blocking(context.pool(), move |conn| {
CommentReportView::read(conn, report.id, person_id) CommentReportView::read(conn, report.id, person_id)
@ -114,9 +114,9 @@ impl Perform for ResolveCommentReport {
} }
}; };
if blocking(context.pool(), resolve_fun).await?.is_err() { blocking(context.pool(), resolve_fun)
return Err(ApiError::err("couldnt_resolve_report").into()); .await?
}; .map_err(|e| ApiError::err("couldnt_resolve_report", e))?;
let report_id = data.report_id; let report_id = data.report_id;
let comment_report_view = blocking(context.pool(), move |conn| { let comment_report_view = blocking(context.pool(), move |conn| {

View File

@ -72,15 +72,15 @@ impl Perform for FollowCommunity {
check_community_ban(local_user_view.person.id, community_id, context.pool()).await?; check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form); let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
if blocking(context.pool(), follow).await?.is_err() { blocking(context.pool(), follow)
return Err(ApiError::err("community_follower_already_exists").into()); .await?
} .map_err(|e| ApiError::err("community_follower_already_exists", e))?;
} else { } else {
let unfollow = let unfollow =
move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form); move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
if blocking(context.pool(), unfollow).await?.is_err() { blocking(context.pool(), unfollow)
return Err(ApiError::err("community_follower_already_exists").into()); .await?
} .map_err(|e| ApiError::err("community_follower_already_exists", e))?;
} }
} else if data.follow { } else if data.follow {
// Dont actually add to the community followers here, because you need // Dont actually add to the community followers here, because you need
@ -89,9 +89,9 @@ impl Perform for FollowCommunity {
} else { } else {
UndoFollowCommunity::send(&local_user_view.person, &community, context).await?; UndoFollowCommunity::send(&local_user_view.person, &community, context).await?;
let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form); let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
if blocking(context.pool(), unfollow).await?.is_err() { blocking(context.pool(), unfollow)
return Err(ApiError::err("community_follower_already_exists").into()); .await?
} .map_err(|e| ApiError::err("community_follower_already_exists", e))?;
} }
let community_id = data.community_id; let community_id = data.community_id;
@ -134,9 +134,9 @@ impl Perform for BlockCommunity {
if data.block { if data.block {
let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form); let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
if blocking(context.pool(), block).await?.is_err() { blocking(context.pool(), block)
return Err(ApiError::err("community_block_already_exists").into()); .await?
} .map_err(|e| ApiError::err("community_block_already_exists", e))?;
// Also, unfollow the community, and send a federated unfollow // Also, unfollow the community, and send a federated unfollow
let community_follower_form = CommunityFollowerForm { let community_follower_form = CommunityFollowerForm {
@ -156,9 +156,9 @@ impl Perform for BlockCommunity {
UndoFollowCommunity::send(&local_user_view.person, &community, context).await?; UndoFollowCommunity::send(&local_user_view.person, &community, context).await?;
} else { } else {
let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form); let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form);
if blocking(context.pool(), unblock).await?.is_err() { blocking(context.pool(), unblock)
return Err(ApiError::err("community_block_already_exists").into()); .await?
} .map_err(|e| ApiError::err("community_block_already_exists", e))?;
} }
let community_view = blocking(context.pool(), move |conn| { let community_view = blocking(context.pool(), move |conn| {
@ -208,9 +208,9 @@ impl Perform for BanFromCommunity {
if data.ban { if data.ban {
let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form); let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
if blocking(context.pool(), ban).await?.is_err() { blocking(context.pool(), ban)
return Err(ApiError::err("community_user_already_banned").into()); .await?
} .map_err(|e| ApiError::err("community_user_already_banned", e))?;
// Also unsubscribe them from the community, if they are subscribed // Also unsubscribe them from the community, if they are subscribed
let community_follower_form = CommunityFollowerForm { let community_follower_form = CommunityFollowerForm {
@ -228,9 +228,9 @@ impl Perform for BanFromCommunity {
.await?; .await?;
} else { } else {
let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form); let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
if blocking(context.pool(), unban).await?.is_err() { blocking(context.pool(), unban)
return Err(ApiError::err("community_user_already_banned").into()); .await?
} .map_err(|e| ApiError::err("community_user_already_banned", e))?;
UndoBlockUserFromCommunity::send( UndoBlockUserFromCommunity::send(
&community, &community,
&banned_person, &banned_person,
@ -332,14 +332,14 @@ impl Perform for AddModToCommunity {
}; };
if data.added { if data.added {
let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form); let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
if blocking(context.pool(), join).await?.is_err() { blocking(context.pool(), join)
return Err(ApiError::err("community_moderator_already_exists").into()); .await?
} .map_err(|e| ApiError::err("community_moderator_already_exists", e))?;
} else { } else {
let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form); let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
if blocking(context.pool(), leave).await?.is_err() { blocking(context.pool(), leave)
return Err(ApiError::err("community_moderator_already_exists").into()); .await?
} .map_err(|e| ApiError::err("community_moderator_already_exists", e))?;
} }
// Mod tables // Mod tables
@ -433,7 +433,7 @@ impl Perform for TransferCommunity {
.map(|a| a.person.id) .map(|a| a.person.id)
.any(|x| x == local_user_view.person.id) .any(|x| x == local_user_view.person.id)
{ {
return Err(ApiError::err("not_an_admin").into()); return Err(ApiError::err_plain("not_an_admin").into());
} }
// You have to re-do the community_moderator table, reordering it. // You have to re-do the community_moderator table, reordering it.
@ -461,9 +461,9 @@ impl Perform for TransferCommunity {
}; };
let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form); let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
if blocking(context.pool(), join).await?.is_err() { blocking(context.pool(), join)
return Err(ApiError::err("community_moderator_already_exists").into()); .await?
} .map_err(|e| ApiError::err("community_moderator_already_exists", e))?;
} }
// Mod tables // Mod tables
@ -484,14 +484,14 @@ impl Perform for TransferCommunity {
CommunityView::read(conn, community_id, Some(person_id)) CommunityView::read(conn, community_id, Some(person_id))
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_find_community"))?; .map_err(|e| ApiError::err("couldnt_find_community", e))?;
let community_id = data.community_id; let community_id = data.community_id;
let moderators = blocking(context.pool(), move |conn| { let moderators = blocking(context.pool(), move |conn| {
CommunityModeratorView::for_community(conn, community_id) CommunityModeratorView::for_community(conn, community_id)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_find_community"))?; .map_err(|e| ApiError::err("couldnt_find_community", e))?;
// Return the jwt // Return the jwt
Ok(GetCommunityResponse { Ok(GetCommunityResponse {

View File

@ -88,7 +88,7 @@ impl Perform for Login {
LocalUserView::find_by_email_or_name(conn, &username_or_email) LocalUserView::find_by_email_or_name(conn, &username_or_email)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?; .map_err(|e| ApiError::err("couldnt_find_that_username_or_email", e))?;
// Verify the password // Verify the password
let valid: bool = verify( let valid: bool = verify(
@ -97,7 +97,7 @@ impl Perform for Login {
) )
.unwrap_or(false); .unwrap_or(false);
if !valid { if !valid {
return Err(ApiError::err("password_incorrect").into()); return Err(ApiError::err_plain("password_incorrect").into());
} }
// Return the jwt // Return the jwt
@ -179,7 +179,7 @@ impl Perform for SaveUserSettings {
if let Some(Some(bio)) = &bio { if let Some(Some(bio)) = &bio {
if bio.chars().count() > 300 { if bio.chars().count() > 300 {
return Err(ApiError::err("bio_length_overflow").into()); return Err(ApiError::err_plain("bio_length_overflow").into());
} }
} }
@ -188,13 +188,13 @@ impl Perform for SaveUserSettings {
display_name.trim(), display_name.trim(),
context.settings().actor_name_max_length, context.settings().actor_name_max_length,
) { ) {
return Err(ApiError::err("invalid_username").into()); return Err(ApiError::err_plain("invalid_username").into());
} }
} }
if let Some(Some(matrix_user_id)) = &matrix_user_id { if let Some(Some(matrix_user_id)) = &matrix_user_id {
if !is_valid_matrix_id(matrix_user_id) { if !is_valid_matrix_id(matrix_user_id) {
return Err(ApiError::err("invalid_matrix_id").into()); return Err(ApiError::err_plain("invalid_matrix_id").into());
} }
} }
@ -226,16 +226,11 @@ impl Perform for SaveUserSettings {
bot_account, bot_account,
}; };
let person_res = blocking(context.pool(), move |conn| { blocking(context.pool(), move |conn| {
Person::update(conn, person_id, &person_form) Person::update(conn, person_id, &person_form)
}) })
.await?; .await?
let _updated_person: Person = match person_res { .map_err(|e| ApiError::err("user_already_exists", e))?;
Ok(p) => p,
Err(_) => {
return Err(ApiError::err("user_already_exists").into());
}
};
let local_user_form = LocalUserForm { let local_user_form = LocalUserForm {
person_id, person_id,
@ -269,7 +264,7 @@ impl Perform for SaveUserSettings {
"user_already_exists" "user_already_exists"
}; };
return Err(ApiError::err(err_type).into()); return Err(ApiError::err(err_type, e).into());
} }
}; };
@ -301,7 +296,7 @@ impl Perform for ChangePassword {
// Make sure passwords match // Make sure passwords match
if data.new_password != data.new_password_verify { if data.new_password != data.new_password_verify {
return Err(ApiError::err("passwords_dont_match").into()); return Err(ApiError::err_plain("passwords_dont_match").into());
} }
// Check the old password // Check the old password
@ -311,7 +306,7 @@ impl Perform for ChangePassword {
) )
.unwrap_or(false); .unwrap_or(false);
if !valid { if !valid {
return Err(ApiError::err("password_incorrect").into()); return Err(ApiError::err_plain("password_incorrect").into());
} }
let local_user_id = local_user_view.local_user.id; let local_user_id = local_user_view.local_user.id;
@ -350,16 +345,11 @@ impl Perform for AddAdmin {
let added = data.added; let added = data.added;
let added_person_id = data.person_id; let added_person_id = data.person_id;
let added_admin = match blocking(context.pool(), move |conn| { let added_admin = blocking(context.pool(), move |conn| {
Person::add_admin(conn, added_person_id, added) Person::add_admin(conn, added_person_id, added)
}) })
.await? .await?
{ .map_err(|e| ApiError::err("couldnt_update_user", e))?;
Ok(a) => a,
Err(_) => {
return Err(ApiError::err("couldnt_update_user").into());
}
};
// Mod tables // Mod tables
let form = ModAddForm { let form = ModAddForm {
@ -414,9 +404,9 @@ impl Perform for BanPerson {
let ban = data.ban; let ban = data.ban;
let banned_person_id = data.person_id; let banned_person_id = data.person_id;
let ban_person = move |conn: &'_ _| Person::ban_person(conn, banned_person_id, ban); let ban_person = move |conn: &'_ _| Person::ban_person(conn, banned_person_id, ban);
if blocking(context.pool(), ban_person).await?.is_err() { blocking(context.pool(), ban_person)
return Err(ApiError::err("couldnt_update_user").into()); .await?
} .map_err(|e| ApiError::err("couldnt_update_user", e))?;
// Remove their data if that's desired // Remove their data if that's desired
if data.remove_data.unwrap_or(false) { if data.remove_data.unwrap_or(false) {
@ -506,7 +496,7 @@ impl Perform for BlockPerson {
// Don't let a person block themselves // Don't let a person block themselves
if target_id == person_id { if target_id == person_id {
return Err(ApiError::err("cant_block_yourself").into()); return Err(ApiError::err_plain("cant_block_yourself").into());
} }
let person_block_form = PersonBlockForm { let person_block_form = PersonBlockForm {
@ -516,14 +506,14 @@ impl Perform for BlockPerson {
if data.block { if data.block {
let block = move |conn: &'_ _| PersonBlock::block(conn, &person_block_form); let block = move |conn: &'_ _| PersonBlock::block(conn, &person_block_form);
if blocking(context.pool(), block).await?.is_err() { blocking(context.pool(), block)
return Err(ApiError::err("person_block_already_exists").into()); .await?
} .map_err(|e| ApiError::err("person_block_already_exists", e))?;
} else { } else {
let unblock = move |conn: &'_ _| PersonBlock::unblock(conn, &person_block_form); let unblock = move |conn: &'_ _| PersonBlock::unblock(conn, &person_block_form);
if blocking(context.pool(), unblock).await?.is_err() { blocking(context.pool(), unblock)
return Err(ApiError::err("person_block_already_exists").into()); .await?
} .map_err(|e| ApiError::err("person_block_already_exists", e))?;
} }
// TODO does any federated stuff need to be done here? // TODO does any federated stuff need to be done here?
@ -635,16 +625,16 @@ impl Perform for MarkPersonMentionAsRead {
.await??; .await??;
if local_user_view.person.id != read_person_mention.recipient_id { if local_user_view.person.id != read_person_mention.recipient_id {
return Err(ApiError::err("couldnt_update_comment").into()); return Err(ApiError::err_plain("couldnt_update_comment").into());
} }
let person_mention_id = read_person_mention.id; let person_mention_id = read_person_mention.id;
let read = data.read; let read = data.read;
let update_mention = let update_mention =
move |conn: &'_ _| PersonMention::update_read(conn, person_mention_id, read); move |conn: &'_ _| PersonMention::update_read(conn, person_mention_id, read);
if blocking(context.pool(), update_mention).await?.is_err() { blocking(context.pool(), update_mention)
return Err(ApiError::err("couldnt_update_comment").into()); .await?
}; .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
let person_mention_id = read_person_mention.id; let person_mention_id = read_person_mention.id;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
@ -690,26 +680,23 @@ impl Perform for MarkAllAsRead {
for comment_view in &replies { for comment_view in &replies {
let reply_id = comment_view.comment.id; let reply_id = comment_view.comment.id;
let mark_as_read = move |conn: &'_ _| Comment::update_read(conn, reply_id, true); let mark_as_read = move |conn: &'_ _| Comment::update_read(conn, reply_id, true);
if blocking(context.pool(), mark_as_read).await?.is_err() { blocking(context.pool(), mark_as_read)
return Err(ApiError::err("couldnt_update_comment").into()); .await?
} .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
} }
// Mark all user mentions as read // Mark all user mentions as read
let update_person_mentions = let update_person_mentions =
move |conn: &'_ _| PersonMention::mark_all_as_read(conn, person_id); move |conn: &'_ _| PersonMention::mark_all_as_read(conn, person_id);
if blocking(context.pool(), update_person_mentions) blocking(context.pool(), update_person_mentions)
.await? .await?
.is_err() .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
{
return Err(ApiError::err("couldnt_update_comment").into());
}
// Mark all private_messages as read // Mark all private_messages as read
let update_pm = move |conn: &'_ _| PrivateMessage::mark_all_as_read(conn, person_id); let update_pm = move |conn: &'_ _| PrivateMessage::mark_all_as_read(conn, person_id);
if blocking(context.pool(), update_pm).await?.is_err() { blocking(context.pool(), update_pm)
return Err(ApiError::err("couldnt_update_private_message").into()); .await?
} .map_err(|e| ApiError::err("couldnt_update_private_message", e))?;
Ok(GetRepliesResponse { replies: vec![] }) Ok(GetRepliesResponse { replies: vec![] })
} }
@ -732,7 +719,7 @@ impl Perform for PasswordReset {
LocalUserView::find_by_email(conn, &email) LocalUserView::find_by_email(conn, &email)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?; .map_err(|e| ApiError::err("couldnt_find_that_username_or_email", e))?;
// Generate a random token // Generate a random token
let token = generate_random_string(); let token = generate_random_string();
@ -758,7 +745,7 @@ impl Perform for PasswordReset {
html, html,
&context.settings(), &context.settings(),
) )
.map_err(|e| ApiError::err(&e))?; .map_err(|e| ApiError::err("email_send_failed", e))?;
Ok(PasswordResetResponse {}) Ok(PasswordResetResponse {})
} }
@ -786,7 +773,7 @@ impl Perform for PasswordChange {
// Make sure passwords match // Make sure passwords match
if data.password != data.password_verify { if data.password != data.password_verify {
return Err(ApiError::err("passwords_dont_match").into()); return Err(ApiError::err_plain("passwords_dont_match").into());
} }
// Update the user with the new password // Update the user with the new password
@ -795,7 +782,7 @@ impl Perform for PasswordChange {
LocalUser::update_password(conn, local_user_id, &password) LocalUser::update_password(conn, local_user_id, &password)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_user"))?; .map_err(|e| ApiError::err("couldnt_update_user", e))?;
// Return the jwt // Return the jwt
Ok(LoginResponse { Ok(LoginResponse {

View File

@ -73,9 +73,9 @@ impl Perform for CreatePostLike {
if do_add { if do_add {
let like_form2 = like_form.clone(); let like_form2 = like_form.clone();
let like = move |conn: &'_ _| PostLike::like(conn, &like_form2); let like = move |conn: &'_ _| PostLike::like(conn, &like_form2);
if blocking(context.pool(), like).await?.is_err() { blocking(context.pool(), like)
return Err(ApiError::err("couldnt_like_post").into()); .await?
} .map_err(|e| ApiError::err("couldnt_like_post", e))?;
Vote::send( Vote::send(
&object, &object,
@ -269,14 +269,14 @@ impl Perform for SavePost {
if data.save { if data.save {
let save = move |conn: &'_ _| PostSaved::save(conn, &post_saved_form); let save = move |conn: &'_ _| PostSaved::save(conn, &post_saved_form);
if blocking(context.pool(), save).await?.is_err() { blocking(context.pool(), save)
return Err(ApiError::err("couldnt_save_post").into()); .await?
} .map_err(|e| ApiError::err("couldnt_save_post", e))?;
} else { } else {
let unsave = move |conn: &'_ _| PostSaved::unsave(conn, &post_saved_form); let unsave = move |conn: &'_ _| PostSaved::unsave(conn, &post_saved_form);
if blocking(context.pool(), unsave).await?.is_err() { blocking(context.pool(), unsave)
return Err(ApiError::err("couldnt_save_post").into()); .await?
} .map_err(|e| ApiError::err("couldnt_save_post", e))?;
} }
let post_id = data.post_id; let post_id = data.post_id;

View File

@ -39,10 +39,10 @@ impl Perform for CreatePostReport {
// check size of report and check for whitespace // check size of report and check for whitespace
let reason = data.reason.trim(); let reason = data.reason.trim();
if reason.is_empty() { if reason.is_empty() {
return Err(ApiError::err("report_reason_required").into()); return Err(ApiError::err_plain("report_reason_required").into());
} }
if reason.chars().count() > 1000 { if reason.chars().count() > 1000 {
return Err(ApiError::err("report_too_long").into()); return Err(ApiError::err_plain("report_too_long").into());
} }
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
@ -67,7 +67,7 @@ impl Perform for CreatePostReport {
PostReport::report(conn, &report_form) PostReport::report(conn, &report_form)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_create_report"))?; .map_err(|e| ApiError::err("couldnt_create_report", e))?;
let post_report_view = blocking(context.pool(), move |conn| { let post_report_view = blocking(context.pool(), move |conn| {
PostReportView::read(conn, report.id, person_id) PostReportView::read(conn, report.id, person_id)
@ -120,9 +120,9 @@ impl Perform for ResolvePostReport {
} }
}; };
if blocking(context.pool(), resolve_fun).await?.is_err() { blocking(context.pool(), resolve_fun)
return Err(ApiError::err("couldnt_resolve_report").into()); .await?
}; .map_err(|e| ApiError::err("couldnt_resolve_report", e))?;
let post_report_view = blocking(context.pool(), move |conn| { let post_report_view = blocking(context.pool(), move |conn| {
PostReportView::read(conn, report_id, person_id) PostReportView::read(conn, report_id, person_id)

View File

@ -30,7 +30,7 @@ impl Perform for MarkPrivateMessageAsRead {
}) })
.await??; .await??;
if local_user_view.person.id != orig_private_message.recipient_id { if local_user_view.person.id != orig_private_message.recipient_id {
return Err(ApiError::err("couldnt_update_private_message").into()); return Err(ApiError::err_plain("couldnt_update_private_message").into());
} }
// Doing the update // Doing the update
@ -40,7 +40,7 @@ impl Perform for MarkPrivateMessageAsRead {
PrivateMessage::update_read(conn, private_message_id, read) PrivateMessage::update_read(conn, private_message_id, read)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_private_message"))?; .map_err(|e| ApiError::err("couldnt_update_private_message", e))?;
// No need to send an apub update // No need to send an apub update
let op = UserOperation::MarkPrivateMessageAsRead; let op = UserOperation::MarkPrivateMessageAsRead;

View File

@ -389,10 +389,10 @@ impl Perform for ResolveObject {
get_local_user_view_from_jwt_opt(&self.auth, context.pool(), context.secret()).await?; get_local_user_view_from_jwt_opt(&self.auth, context.pool(), context.secret()).await?;
let res = search_by_apub_id(&self.q, context) let res = search_by_apub_id(&self.q, context)
.await .await
.map_err(|_| ApiError::err("couldnt_find_object"))?; .map_err(|e| ApiError::err("couldnt_find_object", e))?;
convert_response(res, local_user_view.map(|l| l.person.id), context.pool()) convert_response(res, local_user_view.map(|l| l.person.id), context.pool())
.await .await
.map_err(|_| ApiError::err("couldnt_find_object").into()) .map_err(|e| ApiError::err("couldnt_find_object", e).into())
} }
} }
@ -454,14 +454,14 @@ impl Perform for TransferSite {
// Make sure user is the creator // Make sure user is the creator
if read_site.creator_id != local_user_view.person.id { if read_site.creator_id != local_user_view.person.id {
return Err(ApiError::err("not_an_admin").into()); return Err(ApiError::err_plain("not_an_admin").into());
} }
let new_creator_id = data.person_id; let new_creator_id = data.person_id;
let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id); let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
if blocking(context.pool(), transfer_site).await?.is_err() { blocking(context.pool(), transfer_site)
return Err(ApiError::err("couldnt_update_site").into()); .await?
}; .map_err(|e| ApiError::err("couldnt_update_site", e))?;
// Mod tables // Mod tables
let form = ModAddForm { let form = ModAddForm {
@ -542,7 +542,7 @@ impl Perform for SaveSiteConfig {
// Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem // 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) let config_hjson = Settings::save_config_file(&data.config_hjson)
.map_err(|_| ApiError::err("couldnt_update_site"))?; .map_err(|e| ApiError::err("couldnt_update_site", e))?;
Ok(GetSiteConfigResponse { config_hjson }) Ok(GetSiteConfigResponse { config_hjson })
} }

View File

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

View File

@ -61,7 +61,7 @@ impl PerformCrud for CreateComment {
// Check if post is locked, no new comments // Check if post is locked, no new comments
if post.locked { if post.locked {
return Err(ApiError::err("locked").into()); return Err(ApiError::err_plain("locked").into());
} }
// If there's a parent_id, check to make sure that comment is in that post // If there's a parent_id, check to make sure that comment is in that post
@ -69,13 +69,13 @@ impl PerformCrud for CreateComment {
// Make sure the parent comment exists // Make sure the parent comment exists
let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id)) let parent = blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
.await? .await?
.map_err(|_| ApiError::err("couldnt_create_comment"))?; .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?; check_person_block(local_user_view.person.id, parent.creator_id, context.pool()).await?;
// Strange issue where sometimes the post ID is incorrect // Strange issue where sometimes the post ID is incorrect
if parent.post_id != post_id { if parent.post_id != post_id {
return Err(ApiError::err("couldnt_create_comment").into()); return Err(ApiError::err_plain("couldnt_create_comment").into());
} }
} }
@ -93,7 +93,7 @@ impl PerformCrud for CreateComment {
Comment::create(conn, &comment_form2) Comment::create(conn, &comment_form2)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_create_comment"))?; .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
// Necessary to update the ap_id // Necessary to update the ap_id
let inserted_comment_id = inserted_comment.id; let inserted_comment_id = inserted_comment.id;
@ -109,7 +109,7 @@ impl PerformCrud for CreateComment {
Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?) Ok(Comment::update_ap_id(conn, inserted_comment_id, apub_id)?)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_create_comment"))?; .map_err(|e| ApiError::err("couldnt_create_comment", e))?;
CreateOrUpdateComment::send( CreateOrUpdateComment::send(
&updated_comment, &updated_comment,
@ -142,9 +142,9 @@ impl PerformCrud for CreateComment {
}; };
let like = move |conn: &'_ _| CommentLike::like(conn, &like_form); let like = move |conn: &'_ _| CommentLike::like(conn, &like_form);
if blocking(context.pool(), like).await?.is_err() { blocking(context.pool(), like)
return Err(ApiError::err("couldnt_like_comment").into()); .await?
} .map_err(|e| ApiError::err("couldnt_like_comment", e))?;
let object = PostOrComment::Comment(updated_comment); let object = PostOrComment::Comment(updated_comment);
Vote::send( Vote::send(
@ -170,7 +170,7 @@ impl PerformCrud for CreateComment {
Comment::update_read(conn, comment_id, true) Comment::update_read(conn, comment_id, true)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_comment"))?; .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
} }
// If its a reply, mark the parent as read // If its a reply, mark the parent as read
if let Some(parent_id) = data.parent_id { if let Some(parent_id) = data.parent_id {
@ -183,7 +183,7 @@ impl PerformCrud for CreateComment {
Comment::update_read(conn, parent_id, true) Comment::update_read(conn, parent_id, true)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_parent_comment"))?; .map_err(|e| ApiError::err("couldnt_update_parent_comment", e))?;
} }
// If the parent has PersonMentions mark them as read too // If the parent has PersonMentions mark them as read too
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
@ -196,7 +196,7 @@ impl PerformCrud for CreateComment {
PersonMention::update_read(conn, mention.id, true) PersonMention::update_read(conn, mention.id, true)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_person_mentions"))?; .map_err(|e| ApiError::err("couldnt_update_person_mentions", e))?;
} }
} }

View File

@ -43,7 +43,7 @@ impl PerformCrud for DeleteComment {
// Verify that only the creator can delete // Verify that only the creator can delete
if local_user_view.person.id != orig_comment.creator.id { if local_user_view.person.id != orig_comment.creator.id {
return Err(ApiError::err("no_comment_edit_allowed").into()); return Err(ApiError::err_plain("no_comment_edit_allowed").into());
} }
// Do the delete // Do the delete
@ -52,7 +52,7 @@ impl PerformCrud for DeleteComment {
Comment::update_deleted(conn, comment_id, deleted) Comment::update_deleted(conn, comment_id, deleted)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_comment"))?; .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
// Send the apub message // Send the apub message
let community = blocking(context.pool(), move |conn| { let community = blocking(context.pool(), move |conn| {
@ -134,7 +134,7 @@ impl PerformCrud for RemoveComment {
Comment::update_removed(conn, comment_id, removed) Comment::update_removed(conn, comment_id, removed)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_comment"))?; .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
// Mod tables // Mod tables
let form = ModRemoveCommentForm { let form = ModRemoveCommentForm {

View File

@ -51,7 +51,7 @@ impl PerformCrud for GetComments {
.list() .list()
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_get_comments"))?; .map_err(|e| ApiError::err("couldnt_get_comments", e))?;
// Blank out deleted or removed info // Blank out deleted or removed info
for cv in comments for cv in comments

View File

@ -51,7 +51,7 @@ impl PerformCrud for EditComment {
// Verify that only the creator can edit // Verify that only the creator can edit
if local_user_view.person.id != orig_comment.creator.id { if local_user_view.person.id != orig_comment.creator.id {
return Err(ApiError::err("no_comment_edit_allowed").into()); return Err(ApiError::err_plain("no_comment_edit_allowed").into());
} }
// Do the update // Do the update
@ -62,7 +62,7 @@ impl PerformCrud for EditComment {
Comment::update_content(conn, comment_id, &content_slurs_removed) Comment::update_content(conn, comment_id, &content_slurs_removed)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_comment"))?; .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
// Send the apub update // Send the apub update
CreateOrUpdateComment::send( CreateOrUpdateComment::send(

View File

@ -51,7 +51,7 @@ impl PerformCrud for CreateCommunity {
let site = blocking(context.pool(), move |conn| Site::read(conn, 0)).await??; let site = blocking(context.pool(), move |conn| Site::read(conn, 0)).await??;
if site.community_creation_admin_only && is_admin(&local_user_view).is_err() { if site.community_creation_admin_only && is_admin(&local_user_view).is_err() {
return Err(ApiError::err("only_admins_can_create_communities").into()); return Err(ApiError::err_plain("only_admins_can_create_communities").into());
} }
check_slurs(&data.name, &context.settings().slur_regex())?; check_slurs(&data.name, &context.settings().slur_regex())?;
@ -59,7 +59,7 @@ impl PerformCrud for CreateCommunity {
check_slurs_opt(&data.description, &context.settings().slur_regex())?; check_slurs_opt(&data.description, &context.settings().slur_regex())?;
if !is_valid_actor_name(&data.name, context.settings().actor_name_max_length) { if !is_valid_actor_name(&data.name, context.settings().actor_name_max_length) {
return Err(ApiError::err("invalid_community_name").into()); return Err(ApiError::err_plain("invalid_community_name").into());
} }
// Double check for duplicate community actor_ids // Double check for duplicate community actor_ids
@ -71,7 +71,7 @@ impl PerformCrud for CreateCommunity {
let community_actor_id_wrapped = ObjectId::<Community>::new(community_actor_id.clone()); let community_actor_id_wrapped = ObjectId::<Community>::new(community_actor_id.clone());
let community_dupe = community_actor_id_wrapped.dereference_local(context).await; let community_dupe = community_actor_id_wrapped.dereference_local(context).await;
if community_dupe.is_ok() { if community_dupe.is_ok() {
return Err(ApiError::err("community_already_exists").into()); return Err(ApiError::err_plain("community_already_exists").into());
} }
// Check to make sure the icon and banners are urls // Check to make sure the icon and banners are urls
@ -101,7 +101,7 @@ impl PerformCrud for CreateCommunity {
Community::create(conn, &community_form) Community::create(conn, &community_form)
}) })
.await? .await?
.map_err(|_| ApiError::err("community_already_exists"))?; .map_err(|e| ApiError::err("community_already_exists", e))?;
// The community creator becomes a moderator // The community creator becomes a moderator
let community_moderator_form = CommunityModeratorForm { let community_moderator_form = CommunityModeratorForm {
@ -111,7 +111,7 @@ impl PerformCrud for CreateCommunity {
let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form); let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
if blocking(context.pool(), join).await?.is_err() { if blocking(context.pool(), join).await?.is_err() {
return Err(ApiError::err("community_moderator_already_exists").into()); return Err(ApiError::err_plain("community_moderator_already_exists").into());
} }
// Follow your own community // Follow your own community
@ -123,7 +123,7 @@ impl PerformCrud for CreateCommunity {
let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form); let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
if blocking(context.pool(), follow).await?.is_err() { if blocking(context.pool(), follow).await?.is_err() {
return Err(ApiError::err("community_follower_already_exists").into()); return Err(ApiError::err_plain("community_follower_already_exists").into());
} }
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;

View File

@ -33,7 +33,7 @@ impl PerformCrud for DeleteCommunity {
// Make sure deleter is the top mod // Make sure deleter is the top mod
if local_user_view.person.id != community_mods[0].moderator.id { if local_user_view.person.id != community_mods[0].moderator.id {
return Err(ApiError::err("no_community_edit_allowed").into()); return Err(ApiError::err_plain("no_community_edit_allowed").into());
} }
// Do the delete // Do the delete
@ -43,7 +43,7 @@ impl PerformCrud for DeleteCommunity {
Community::update_deleted(conn, community_id, deleted) Community::update_deleted(conn, community_id, deleted)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_community"))?; .map_err(|e| ApiError::err("couldnt_update_community", e))?;
// Send apub messages // Send apub messages
send_apub_delete( send_apub_delete(
@ -89,7 +89,7 @@ impl PerformCrud for RemoveCommunity {
Community::update_removed(conn, community_id, removed) Community::update_removed(conn, community_id, removed)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_community"))?; .map_err(|e| ApiError::err("couldnt_update_community", e))?;
// Mod tables // Mod tables
let expires = data.expires.map(naive_from_unix); let expires = data.expires.map(naive_from_unix);

View File

@ -35,7 +35,7 @@ impl PerformCrud for GetCommunity {
ObjectId::<Community>::new(community_actor_id) ObjectId::<Community>::new(community_actor_id)
.dereference(context, &mut 0) .dereference(context, &mut 0)
.await .await
.map_err(|_| ApiError::err("couldnt_find_community"))? .map_err(|e| ApiError::err("couldnt_find_community", e))?
.id .id
} }
}; };
@ -44,7 +44,7 @@ impl PerformCrud for GetCommunity {
CommunityView::read(conn, community_id, person_id) CommunityView::read(conn, community_id, person_id)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_find_community"))?; .map_err(|e| ApiError::err("couldnt_find_community", e))?;
// Blank out deleted or removed info // Blank out deleted or removed info
if community_view.community.deleted || community_view.community.removed { if community_view.community.deleted || community_view.community.removed {
@ -55,7 +55,7 @@ impl PerformCrud for GetCommunity {
CommunityModeratorView::for_community(conn, community_id) CommunityModeratorView::for_community(conn, community_id)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_find_community"))?; .map_err(|e| ApiError::err("couldnt_find_community", e))?;
let online = context let online = context
.chat_server() .chat_server()

View File

@ -40,7 +40,7 @@ impl PerformCrud for EditCommunity {
}) })
.await??; .await??;
if !mods.contains(&local_user_view.person.id) { if !mods.contains(&local_user_view.person.id) {
return Err(ApiError::err("not_a_moderator").into()); return Err(ApiError::err_plain("not_a_moderator").into());
} }
let community_id = data.community_id; let community_id = data.community_id;
@ -68,7 +68,7 @@ impl PerformCrud for EditCommunity {
Community::update(conn, community_id, &community_form) Community::update(conn, community_id, &community_form)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_community"))?; .map_err(|e| ApiError::err("couldnt_update_community", e))?;
UpdateCommunity::send(&updated_community, &local_user_view.person, context).await?; UpdateCommunity::send(&updated_community, &local_user_view.person, context).await?;

View File

@ -50,7 +50,7 @@ impl PerformCrud for CreatePost {
honeypot_check(&data.honeypot)?; honeypot_check(&data.honeypot)?;
if !is_valid_post_title(&data.name) { if !is_valid_post_title(&data.name) {
return Err(ApiError::err("invalid_post_title").into()); return Err(ApiError::err_plain("invalid_post_title").into());
} }
check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?; check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
@ -87,7 +87,7 @@ impl PerformCrud for CreatePost {
"couldnt_create_post" "couldnt_create_post"
}; };
return Err(ApiError::err(err_type).into()); return Err(ApiError::err(err_type, e).into());
} }
}; };
@ -102,7 +102,7 @@ impl PerformCrud for CreatePost {
Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?) Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_create_post"))?; .map_err(|e| ApiError::err("couldnt_create_post", e))?;
CreateOrUpdatePost::send( CreateOrUpdatePost::send(
&updated_post, &updated_post,
@ -123,7 +123,7 @@ impl PerformCrud for CreatePost {
let like = move |conn: &'_ _| PostLike::like(conn, &like_form); let like = move |conn: &'_ _| PostLike::like(conn, &like_form);
if blocking(context.pool(), like).await?.is_err() { if blocking(context.pool(), like).await?.is_err() {
return Err(ApiError::err("couldnt_like_post").into()); return Err(ApiError::err_plain("couldnt_like_post").into());
} }
// Mark the post as read // Mark the post as read

View File

@ -38,7 +38,7 @@ impl PerformCrud for DeletePost {
// Verify that only the creator can delete // Verify that only the creator can delete
if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) { if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
return Err(ApiError::err("no_post_edit_allowed").into()); return Err(ApiError::err_plain("no_post_edit_allowed").into());
} }
// Update the post // Update the post

View File

@ -37,7 +37,7 @@ impl PerformCrud for GetPost {
PostView::read(conn, id, person_id) PostView::read(conn, id, person_id)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_find_post"))?; .map_err(|e| ApiError::err("couldnt_find_post", e))?;
// Blank out deleted info // Blank out deleted info
if post_view.post.deleted || post_view.post.removed { if post_view.post.deleted || post_view.post.removed {
@ -79,7 +79,7 @@ impl PerformCrud for GetPost {
CommunityView::read(conn, community_id, person_id) CommunityView::read(conn, community_id, person_id)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_find_community"))?; .map_err(|e| ApiError::err("couldnt_find_community", e))?;
// Blank out deleted or removed info // Blank out deleted or removed info
if community_view.community.deleted || community_view.community.removed { if community_view.community.deleted || community_view.community.removed {
@ -155,7 +155,7 @@ impl PerformCrud for GetPosts {
.list() .list()
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_get_posts"))?; .map_err(|e| ApiError::err("couldnt_get_posts", e))?;
// Blank out deleted or removed info // Blank out deleted or removed info
for pv in posts for pv in posts

View File

@ -32,7 +32,7 @@ impl PerformCrud for EditPost {
if let Some(name) = &data.name { if let Some(name) = &data.name {
if !is_valid_post_title(name) { if !is_valid_post_title(name) {
return Err(ApiError::err("invalid_post_title").into()); return Err(ApiError::err_plain("invalid_post_title").into());
} }
} }
@ -48,7 +48,7 @@ impl PerformCrud for EditPost {
// Verify that only the creator can edit // Verify that only the creator can edit
if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) { if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
return Err(ApiError::err("no_post_edit_allowed").into()); return Err(ApiError::err_plain("no_post_edit_allowed").into());
} }
// Fetch post links and Pictrs cached image // Fetch post links and Pictrs cached image
@ -88,7 +88,7 @@ impl PerformCrud for EditPost {
"couldnt_update_post" "couldnt_update_post"
}; };
return Err(ApiError::err(err_type).into()); return Err(ApiError::err(err_type, e).into());
} }
}; };

View File

@ -52,8 +52,8 @@ impl PerformCrud for CreatePrivateMessage {
.await? .await?
{ {
Ok(private_message) => private_message, Ok(private_message) => private_message,
Err(_e) => { Err(e) => {
return Err(ApiError::err("couldnt_create_private_message").into()); return Err(ApiError::err("couldnt_create_private_message", e).into());
} }
}; };
@ -75,7 +75,7 @@ impl PerformCrud for CreatePrivateMessage {
}, },
) )
.await? .await?
.map_err(|_| ApiError::err("couldnt_create_private_message"))?; .map_err(|e| ApiError::err("couldnt_create_private_message", e))?;
CreateOrUpdatePrivateMessage::send( CreateOrUpdatePrivateMessage::send(
&updated_private_message, &updated_private_message,

View File

@ -34,7 +34,7 @@ impl PerformCrud for DeletePrivateMessage {
}) })
.await??; .await??;
if local_user_view.person.id != orig_private_message.creator_id { if local_user_view.person.id != orig_private_message.creator_id {
return Err(ApiError::err("no_private_message_edit_allowed").into()); return Err(ApiError::err_plain("no_private_message_edit_allowed").into());
} }
// Doing the update // Doing the update
@ -44,7 +44,7 @@ impl PerformCrud for DeletePrivateMessage {
PrivateMessage::update_deleted(conn, private_message_id, deleted) PrivateMessage::update_deleted(conn, private_message_id, deleted)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_private_message"))?; .map_err(|e| ApiError::err("couldnt_update_private_message", e))?;
// Send the apub update // Send the apub update
if data.deleted { if data.deleted {

View File

@ -34,7 +34,7 @@ impl PerformCrud for EditPrivateMessage {
}) })
.await??; .await??;
if local_user_view.person.id != orig_private_message.creator_id { if local_user_view.person.id != orig_private_message.creator_id {
return Err(ApiError::err("no_private_message_edit_allowed").into()); return Err(ApiError::err_plain("no_private_message_edit_allowed").into());
} }
// Doing the update // Doing the update
@ -44,7 +44,7 @@ impl PerformCrud for EditPrivateMessage {
PrivateMessage::update_content(conn, private_message_id, &content_slurs_removed) PrivateMessage::update_content(conn, private_message_id, &content_slurs_removed)
}) })
.await? .await?
.map_err(|_| ApiError::err("couldnt_update_private_message"))?; .map_err(|e| ApiError::err("couldnt_update_private_message", e))?;
// Send the apub update // Send the apub update
CreateOrUpdatePrivateMessage::send( CreateOrUpdatePrivateMessage::send(

View File

@ -36,7 +36,7 @@ impl PerformCrud for CreateSite {
let read_site = move |conn: &'_ _| Site::read_simple(conn); let read_site = move |conn: &'_ _| Site::read_simple(conn);
if blocking(context.pool(), read_site).await?.is_ok() { if blocking(context.pool(), read_site).await?.is_ok() {
return Err(ApiError::err("site_already_exists").into()); return Err(ApiError::err_plain("site_already_exists").into());
}; };
let local_user_view = let local_user_view =
@ -73,7 +73,7 @@ impl PerformCrud for CreateSite {
let create_site = move |conn: &'_ _| Site::create(conn, &site_form); let create_site = move |conn: &'_ _| Site::create(conn, &site_form);
if blocking(context.pool(), create_site).await?.is_err() { if blocking(context.pool(), create_site).await?.is_err() {
return Err(ApiError::err("site_already_exists").into()); return Err(ApiError::err_plain("site_already_exists").into());
} }
let site_view = blocking(context.pool(), SiteView::read).await??; let site_view = blocking(context.pool(), SiteView::read).await??;

View File

@ -100,27 +100,27 @@ impl PerformCrud for GetSite {
CommunityFollowerView::for_person(conn, person_id) CommunityFollowerView::for_person(conn, person_id)
}) })
.await? .await?
.map_err(|_| ApiError::err("system_err_login"))?; .map_err(|e| ApiError::err("system_err_login", e))?;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
let community_blocks = blocking(context.pool(), move |conn| { let community_blocks = blocking(context.pool(), move |conn| {
CommunityBlockView::for_person(conn, person_id) CommunityBlockView::for_person(conn, person_id)
}) })
.await? .await?
.map_err(|_| ApiError::err("system_err_login"))?; .map_err(|e| ApiError::err("system_err_login", e))?;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
let person_blocks = blocking(context.pool(), move |conn| { let person_blocks = blocking(context.pool(), move |conn| {
PersonBlockView::for_person(conn, person_id) PersonBlockView::for_person(conn, person_id)
}) })
.await? .await?
.map_err(|_| ApiError::err("system_err_login"))?; .map_err(|e| ApiError::err("system_err_login", e))?;
let moderates = blocking(context.pool(), move |conn| { let moderates = blocking(context.pool(), move |conn| {
CommunityModeratorView::for_person(conn, person_id) CommunityModeratorView::for_person(conn, person_id)
}) })
.await? .await?
.map_err(|_| ApiError::err("system_err_login"))?; .map_err(|e| ApiError::err("system_err_login", e))?;
Some(MyUserInfo { Some(MyUserInfo {
local_user_view, local_user_view,

View File

@ -65,9 +65,9 @@ impl PerformCrud for EditSite {
}; };
let update_site = move |conn: &'_ _| Site::update(conn, 1, &site_form); let update_site = move |conn: &'_ _| Site::update(conn, 1, &site_form);
if blocking(context.pool(), update_site).await?.is_err() { blocking(context.pool(), update_site)
return Err(ApiError::err("couldnt_update_site").into()); .await?
} .map_err(|e| ApiError::err("couldnt_update_site", e))?;
let site_view = blocking(context.pool(), SiteView::read).await??; let site_view = blocking(context.pool(), SiteView::read).await??;

View File

@ -50,7 +50,7 @@ impl PerformCrud for Register {
// Make sure site has open registration // Make sure site has open registration
if let Ok(site) = blocking(context.pool(), move |conn| Site::read_simple(conn)).await? { if let Ok(site) = blocking(context.pool(), move |conn| Site::read_simple(conn)).await? {
if !site.open_registration { if !site.open_registration {
return Err(ApiError::err("registration_closed").into()); return Err(ApiError::err_plain("registration_closed").into());
} }
} }
@ -59,7 +59,7 @@ impl PerformCrud for Register {
// Make sure passwords match // Make sure passwords match
if data.password != data.password_verify { if data.password != data.password_verify {
return Err(ApiError::err("passwords_dont_match").into()); return Err(ApiError::err_plain("passwords_dont_match").into());
} }
// Check if there are admins. False if admins exist // Check if there are admins. False if admins exist
@ -84,7 +84,7 @@ impl PerformCrud for Register {
}) })
.await?; .await?;
if !check { if !check {
return Err(ApiError::err("captcha_incorrect").into()); return Err(ApiError::err_plain("captcha_incorrect").into());
} }
} }
@ -92,7 +92,7 @@ impl PerformCrud for Register {
let actor_keypair = generate_actor_keypair()?; let actor_keypair = generate_actor_keypair()?;
if !is_valid_actor_name(&data.username, context.settings().actor_name_max_length) { if !is_valid_actor_name(&data.username, context.settings().actor_name_max_length) {
return Err(ApiError::err("invalid_username").into()); return Err(ApiError::err_plain("invalid_username").into());
} }
let actor_id = generate_apub_endpoint( let actor_id = generate_apub_endpoint(
EndpointType::Person, EndpointType::Person,
@ -119,7 +119,7 @@ impl PerformCrud for Register {
Person::create(conn, &person_form) Person::create(conn, &person_form)
}) })
.await? .await?
.map_err(|_| ApiError::err("user_already_exists"))?; .map_err(|e| ApiError::err("user_already_exists", e))?;
// Create the local user // Create the local user
// TODO some of these could probably use the DB defaults // TODO some of these could probably use the DB defaults
@ -161,7 +161,7 @@ impl PerformCrud for Register {
}) })
.await??; .await??;
return Err(ApiError::err(err_type).into()); return Err(ApiError::err(err_type, e).into());
} }
}; };
@ -209,9 +209,9 @@ impl PerformCrud for Register {
}; };
let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form); let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
if blocking(context.pool(), follow).await?.is_err() { blocking(context.pool(), follow)
return Err(ApiError::err("community_follower_already_exists").into()); .await?
}; .map_err(|e| ApiError::err("community_follower_already_exists", e))?;
// If its an admin, add them as a mod and follower to main // If its an admin, add them as a mod and follower to main
if no_admins { if no_admins {
@ -221,9 +221,9 @@ impl PerformCrud for Register {
}; };
let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form); let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
if blocking(context.pool(), join).await?.is_err() { blocking(context.pool(), join)
return Err(ApiError::err("community_moderator_already_exists").into()); .await?
} .map_err(|e| ApiError::err("community_moderator_already_exists", e))?;
} }
// Return the jwt // Return the jwt

View File

@ -27,21 +27,21 @@ impl PerformCrud for DeleteAccount {
) )
.unwrap_or(false); .unwrap_or(false);
if !valid { if !valid {
return Err(ApiError::err("password_incorrect").into()); return Err(ApiError::err_plain("password_incorrect").into());
} }
// Comments // Comments
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
let permadelete = move |conn: &'_ _| Comment::permadelete_for_creator(conn, person_id); let permadelete = move |conn: &'_ _| Comment::permadelete_for_creator(conn, person_id);
if blocking(context.pool(), permadelete).await?.is_err() { blocking(context.pool(), permadelete)
return Err(ApiError::err("couldnt_update_comment").into()); .await?
} .map_err(|e| ApiError::err("couldnt_update_comment", e))?;
// Posts // Posts
let permadelete = move |conn: &'_ _| Post::permadelete_for_creator(conn, person_id); let permadelete = move |conn: &'_ _| Post::permadelete_for_creator(conn, person_id);
if blocking(context.pool(), permadelete).await?.is_err() { blocking(context.pool(), permadelete)
return Err(ApiError::err("couldnt_update_post").into()); .await?
} .map_err(|e| ApiError::err("couldnt_update_post", e))?;
blocking(context.pool(), move |conn| { blocking(context.pool(), move |conn| {
Person::delete_account(conn, person_id) Person::delete_account(conn, person_id)

View File

@ -49,7 +49,7 @@ impl PerformCrud for GetPersonDetails {
.dereference(context, &mut 0) .dereference(context, &mut 0)
.await; .await;
person person
.map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))? .map_err(|e| ApiError::err("couldnt_find_that_username_or_email", e))?
.id .id
} }
}; };

View File

@ -265,7 +265,7 @@ pub fn diesel_option_overwrite_to_url(
Some("") => Ok(Some(None)), Some("") => Ok(Some(None)),
Some(str_url) => match Url::parse(str_url) { Some(str_url) => match Url::parse(str_url) {
Ok(url) => Ok(Some(Some(url.into()))), Ok(url) => Ok(Some(Some(url.into()))),
Err(_) => Err(ApiError::err("invalid_url")), Err(e) => Err(ApiError::err("invalid_url", e)),
}, },
None => Ok(None), None => Ok(None),
} }

View File

@ -18,8 +18,8 @@ pub mod utils;
pub mod version; pub mod version;
use http::StatusCode; use http::StatusCode;
use log::warn;
use std::fmt; use std::{fmt, fmt::Display};
use thiserror::Error; use thiserror::Error;
pub type ConnectionId = usize; pub type ConnectionId = usize;
@ -48,11 +48,17 @@ macro_rules! location_info {
#[derive(Debug, Error)] #[derive(Debug, Error)]
#[error("{{\"error\":\"{message}\"}}")] #[error("{{\"error\":\"{message}\"}}")]
pub struct ApiError { pub struct ApiError {
pub message: String, message: String,
} }
impl ApiError { impl ApiError {
pub fn err(msg: &str) -> Self { pub fn err_plain(msg: &str) -> Self {
ApiError {
message: msg.to_string(),
}
}
pub fn err<E: Display>(msg: &str, original_error: E) -> Self {
warn!("{}", original_error);
ApiError { ApiError {
message: msg.to_string(), message: msg.to_string(),
} }
@ -73,7 +79,7 @@ where
} }
} }
impl std::fmt::Display for LemmyError { impl Display for LemmyError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.inner.fmt(f) self.inner.fmt(f)
} }

View File

@ -47,11 +47,8 @@ pub(crate) fn slur_check<'a>(test: &'a str, slur_regex: &'a Regex) -> Result<(),
} }
pub fn check_slurs(text: &str, slur_regex: &Regex) -> Result<(), ApiError> { pub fn check_slurs(text: &str, slur_regex: &Regex) -> Result<(), ApiError> {
if let Err(slurs) = slur_check(text, slur_regex) { slur_check(text, slur_regex)
Err(ApiError::err(&slurs_vec_to_str(slurs))) .map_err(|slurs| ApiError::err_plain(&slurs_vec_to_str(slurs.clone())))
} else {
Ok(())
}
} }
pub fn check_slurs_opt(text: &Option<String>, slur_regex: &Regex) -> Result<(), ApiError> { pub fn check_slurs_opt(text: &Option<String>, slur_regex: &Regex) -> Result<(), ApiError> {

View File

@ -472,9 +472,9 @@ impl ChatServer {
async move { async move {
let json: Value = serde_json::from_str(&msg.msg)?; let json: Value = serde_json::from_str(&msg.msg)?;
let data = &json["data"].to_string(); let data = &json["data"].to_string();
let op = &json["op"].as_str().ok_or(ApiError { let op = &json["op"]
message: "Unknown op type".to_string(), .as_str()
})?; .ok_or_else(|| ApiError::err_plain("missing op"))?;
if let Ok(user_operation_crud) = UserOperationCrud::from_str(op) { if let Ok(user_operation_crud) = UserOperationCrud::from_str(op) {
let fut = (message_handler_crud)(context, msg.id, user_operation_crud.clone(), data); let fut = (message_handler_crud)(context, msg.id, user_operation_crud.clone(), data);