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)