Skip to content

Commit

Permalink
Dont swallow API errors (fixes #1834) (#1837)
Browse files Browse the repository at this point in the history
Dont swallow API errors (fixes #1834)
  • Loading branch information
Nutomic authored Oct 13, 2021
1 parent d262559 commit 8067244
Show file tree
Hide file tree
Showing 34 changed files with 224 additions and 234 deletions.
22 changes: 11 additions & 11 deletions crates/api/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Perform for MarkCommentAsRead {

// Verify that only the recipient can mark as read
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
Expand All @@ -59,7 +59,7 @@ impl Perform for MarkCommentAsRead {
Comment::update_read(conn, comment_id, read)
})
.await?
.map_err(|_| ApiError::err("couldnt_update_comment"))?;
.map_err(|_| ApiError::err_plain("couldnt_update_comment"))?;

// Refetch it
let comment_id = data.comment_id;
Expand Down Expand Up @@ -99,14 +99,14 @@ impl Perform for SaveComment {

if data.save {
let save_comment = move |conn: &'_ _| CommentSaved::save(conn, &comment_saved_form);
if blocking(context.pool(), save_comment).await?.is_err() {
return Err(ApiError::err("couldnt_save_comment").into());
}
blocking(context.pool(), save_comment)
.await?
.map_err(|e| ApiError::err("couldnt_save_comment", e))?;
} else {
let unsave_comment = move |conn: &'_ _| CommentSaved::unsave(conn, &comment_saved_form);
if blocking(context.pool(), unsave_comment).await?.is_err() {
return Err(ApiError::err("couldnt_save_comment").into());
}
blocking(context.pool(), unsave_comment)
.await?
.map_err(|e| ApiError::err("couldnt_save_comment", e))?;
}

let comment_id = data.comment_id;
Expand Down Expand Up @@ -193,9 +193,9 @@ impl Perform for CreateCommentLike {
if do_add {
let like_form2 = like_form.clone();
let like = move |conn: &'_ _| CommentLike::like(conn, &like_form2);
if blocking(context.pool(), like).await?.is_err() {
return Err(ApiError::err("couldnt_like_comment").into());
}
blocking(context.pool(), like)
.await?
.map_err(|e| ApiError::err("couldnt_like_comment", e))?;

Vote::send(
&object,
Expand Down
12 changes: 6 additions & 6 deletions crates/api/src/comment_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ impl Perform for CreateCommentReport {
// check size of report and check for whitespace
let reason = data.reason.trim();
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 {
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;
Expand All @@ -59,7 +59,7 @@ impl Perform for CreateCommentReport {
CommentReport::report(conn, &report_form)
})
.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| {
CommentReportView::read(conn, report.id, person_id)
Expand Down Expand Up @@ -114,9 +114,9 @@ impl Perform for ResolveCommentReport {
}
};

if blocking(context.pool(), resolve_fun).await?.is_err() {
return Err(ApiError::err("couldnt_resolve_report").into());
};
blocking(context.pool(), resolve_fun)
.await?
.map_err(|e| ApiError::err("couldnt_resolve_report", e))?;

let report_id = data.report_id;
let comment_report_view = blocking(context.pool(), move |conn| {
Expand Down
66 changes: 33 additions & 33 deletions crates/api/src/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ impl Perform for FollowCommunity {
check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;

let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
if blocking(context.pool(), follow).await?.is_err() {
return Err(ApiError::err("community_follower_already_exists").into());
}
blocking(context.pool(), follow)
.await?
.map_err(|e| ApiError::err("community_follower_already_exists", e))?;
} else {
let unfollow =
move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
if blocking(context.pool(), unfollow).await?.is_err() {
return Err(ApiError::err("community_follower_already_exists").into());
}
blocking(context.pool(), unfollow)
.await?
.map_err(|e| ApiError::err("community_follower_already_exists", e))?;
}
} else if data.follow {
// Dont actually add to the community followers here, because you need
Expand All @@ -89,9 +89,9 @@ impl Perform for FollowCommunity {
} else {
UndoFollowCommunity::send(&local_user_view.person, &community, context).await?;
let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
if blocking(context.pool(), unfollow).await?.is_err() {
return Err(ApiError::err("community_follower_already_exists").into());
}
blocking(context.pool(), unfollow)
.await?
.map_err(|e| ApiError::err("community_follower_already_exists", e))?;
}

let community_id = data.community_id;
Expand Down Expand Up @@ -134,9 +134,9 @@ impl Perform for BlockCommunity {

if data.block {
let block = move |conn: &'_ _| CommunityBlock::block(conn, &community_block_form);
if blocking(context.pool(), block).await?.is_err() {
return Err(ApiError::err("community_block_already_exists").into());
}
blocking(context.pool(), block)
.await?
.map_err(|e| ApiError::err("community_block_already_exists", e))?;

// Also, unfollow the community, and send a federated unfollow
let community_follower_form = CommunityFollowerForm {
Expand All @@ -156,9 +156,9 @@ impl Perform for BlockCommunity {
UndoFollowCommunity::send(&local_user_view.person, &community, context).await?;
} else {
let unblock = move |conn: &'_ _| CommunityBlock::unblock(conn, &community_block_form);
if blocking(context.pool(), unblock).await?.is_err() {
return Err(ApiError::err("community_block_already_exists").into());
}
blocking(context.pool(), unblock)
.await?
.map_err(|e| ApiError::err("community_block_already_exists", e))?;
}

let community_view = blocking(context.pool(), move |conn| {
Expand Down Expand Up @@ -208,9 +208,9 @@ impl Perform for BanFromCommunity {

if data.ban {
let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
if blocking(context.pool(), ban).await?.is_err() {
return Err(ApiError::err("community_user_already_banned").into());
}
blocking(context.pool(), ban)
.await?
.map_err(|e| ApiError::err("community_user_already_banned", e))?;

// Also unsubscribe them from the community, if they are subscribed
let community_follower_form = CommunityFollowerForm {
Expand All @@ -228,9 +228,9 @@ impl Perform for BanFromCommunity {
.await?;
} else {
let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
if blocking(context.pool(), unban).await?.is_err() {
return Err(ApiError::err("community_user_already_banned").into());
}
blocking(context.pool(), unban)
.await?
.map_err(|e| ApiError::err("community_user_already_banned", e))?;
UndoBlockUserFromCommunity::send(
&community,
&banned_person,
Expand Down Expand Up @@ -332,14 +332,14 @@ impl Perform for AddModToCommunity {
};
if data.added {
let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
if blocking(context.pool(), join).await?.is_err() {
return Err(ApiError::err("community_moderator_already_exists").into());
}
blocking(context.pool(), join)
.await?
.map_err(|e| ApiError::err("community_moderator_already_exists", e))?;
} else {
let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
if blocking(context.pool(), leave).await?.is_err() {
return Err(ApiError::err("community_moderator_already_exists").into());
}
blocking(context.pool(), leave)
.await?
.map_err(|e| ApiError::err("community_moderator_already_exists", e))?;
}

// Mod tables
Expand Down Expand Up @@ -433,7 +433,7 @@ impl Perform for TransferCommunity {
.map(|a| a.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.
Expand Down Expand Up @@ -461,9 +461,9 @@ impl Perform for TransferCommunity {
};

let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
if blocking(context.pool(), join).await?.is_err() {
return Err(ApiError::err("community_moderator_already_exists").into());
}
blocking(context.pool(), join)
.await?
.map_err(|e| ApiError::err("community_moderator_already_exists", e))?;
}

// Mod tables
Expand All @@ -484,14 +484,14 @@ impl Perform for TransferCommunity {
CommunityView::read(conn, community_id, Some(person_id))
})
.await?
.map_err(|_| ApiError::err("couldnt_find_community"))?;
.map_err(|e| ApiError::err("couldnt_find_community", e))?;

let community_id = data.community_id;
let moderators = blocking(context.pool(), move |conn| {
CommunityModeratorView::for_community(conn, community_id)
})
.await?
.map_err(|_| ApiError::err("couldnt_find_community"))?;
.map_err(|e| ApiError::err("couldnt_find_community", e))?;

// Return the jwt
Ok(GetCommunityResponse {
Expand Down
Loading

0 comments on commit 8067244

Please sign in to comment.