From 37f4b78f6639736a5aa07e53aa1b35f6a1d91be5 Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Tue, 27 Dec 2022 17:53:02 +0100 Subject: [PATCH] Silently fail in share worker if user account is suspended --- app/workers/share_worker.rb | 4 ++++ spec/workers/share_worker_spec.rb | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/app/workers/share_worker.rb b/app/workers/share_worker.rb index 3c9ebcd5..60e314b1 100644 --- a/app/workers/share_worker.rb +++ b/app/workers/share_worker.rb @@ -18,6 +18,10 @@ class ShareWorker rescue Twitter::Error::DuplicateStatus logger.info "Tried to post answer ##{answer_id} from user ##{user_id} to Twitter but the status was already posted." return + rescue Twitter::Error::Forbidden + # User's Twitter account is suspended + logger.info "Tried to post answer ##{answer_id} from user ##{user_id} to Twitter but the account is suspended." + return rescue Twitter::Error::Unauthorized # User's Twitter token has expired or been revoked # TODO: Notify user if this happens (https://github.com/Retrospring/retrospring/issues/123) diff --git a/spec/workers/share_worker_spec.rb b/spec/workers/share_worker_spec.rb index 269fbfa5..e9c6d23e 100644 --- a/spec/workers/share_worker_spec.rb +++ b/spec/workers/share_worker_spec.rb @@ -59,6 +59,13 @@ describe ShareWorker do expect(Sidekiq.logger).to have_received(:info).with("Tried to post answer ##{answer.id} from user ##{user.id} to Twitter but the token has exired or been revoked.") end + it "handles Twitter::Error::Forbidden" do + allow_any_instance_of(Services::Twitter).to receive(:post).with(answer).and_raise(Twitter::Error::Forbidden) + subject + ShareWorker.drain + expect(Sidekiq.logger).to have_received(:info).with("Tried to post answer ##{answer.id} from user ##{user.id} to Twitter but the account is suspended.") + end + it "retries on unhandled exceptions" do expect { subject }.to change(ShareWorker.jobs, :size).by(1) expect { ShareWorker.drain }.to raise_error(Twitter::Error::BadRequest)