FIX: Rescue errors in job. (#6745)

This commit is contained in:
Bianca Nenciu 2018-12-17 16:28:57 +02:00 committed by Régis Hanol
parent f0027961c7
commit 5bda4d26b4
2 changed files with 13 additions and 6 deletions

View File

@ -14,7 +14,8 @@ module Jobs
args[:post_id], args[:post_id],
args[:poll_name], args[:poll_name],
"closed", "closed",
Discourse.system_user Discourse.system_user,
false
) )
end end

View File

@ -108,28 +108,34 @@ after_initialize do
end end
end end
def toggle_status(post_id, poll_name, status, user) def toggle_status(post_id, poll_name, status, user, raise_errors = true)
Poll.transaction do Poll.transaction do
post = Post.find_by(id: post_id) post = Post.find_by(id: post_id)
# post must not be deleted # post must not be deleted
if post.nil? || post.trashed? if post.nil? || post.trashed?
raise StandardError.new I18n.t("poll.post_is_deleted") raise StandardError.new I18n.t("poll.post_is_deleted") if raise_errors
return
end end
# topic must not be archived # topic must not be archived
if post.topic&.archived if post.topic&.archived
raise StandardError.new I18n.t("poll.topic_must_be_open_to_toggle_status") raise StandardError.new I18n.t("poll.topic_must_be_open_to_toggle_status") if raise_errors
return
end end
# either staff member or OP # either staff member or OP
unless post.user_id == user&.id || user&.staff? unless post.user_id == user&.id || user&.staff?
raise StandardError.new I18n.t("poll.only_staff_or_op_can_toggle_status") raise StandardError.new I18n.t("poll.only_staff_or_op_can_toggle_status") if raise_errors
return
end end
poll = Poll.find_by(post_id: post_id, name: poll_name) poll = Poll.find_by(post_id: post_id, name: poll_name)
raise StandardError.new I18n.t("poll.no_poll_with_this_name", name: poll_name) unless poll if !poll
raise StandardError.new I18n.t("poll.no_poll_with_this_name", name: poll_name) if raise_errors
return
end
poll.status = status poll.status = status
poll.save! poll.save!