Match error class using case instead of if/elsif/else (#16952)
* prefer case to multiple if branches case klass works fine on inheritance chains (we don't need to match on inheritance explicitly). * Verify case on error class behaves as before * Fix tests I don't know how I committed tests that were failing, but don't do that
This commit is contained in:
parent
161e18e5df
commit
7e099db0f4
2 changed files with 35 additions and 2 deletions
|
|
@ -44,9 +44,10 @@ module TwitterClient
|
|||
error_class = "::TwitterClient::Errors::#{class_name}".safe_constantize
|
||||
raise error_class, exception.message if error_class
|
||||
|
||||
error_class = if exception.class < Twitter::Error::ClientError
|
||||
error_class = case exception
|
||||
when Twitter::Error::ClientError
|
||||
TwitterClient::Errors::ClientError
|
||||
elsif exception.class < Twitter::Error::ServerError
|
||||
when Twitter::Error::ServerError
|
||||
TwitterClient::Errors::ServerError
|
||||
else
|
||||
TwitterClient::Errors::Error
|
||||
|
|
|
|||
|
|
@ -27,4 +27,36 @@ RSpec.describe TwitterClient::Client, type: :service, vcr: true do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "handling underlying errors" do
|
||||
let(:client) { instance_double(Twitter::REST::Client) }
|
||||
|
||||
before do
|
||||
allow(Twitter::REST::Client).to receive(:new).and_return(client)
|
||||
end
|
||||
|
||||
it "matches defined errors by name" do
|
||||
allow(client).to receive(:status).and_raise(Twitter::Error::NotFound)
|
||||
|
||||
expect { described_class.status(1) }.to raise_error(TwitterClient::Errors::NotFound)
|
||||
end
|
||||
|
||||
it "matches client errors" do
|
||||
allow(client).to receive(:status).and_raise(Twitter::Error::BadRequest)
|
||||
|
||||
expect { described_class.status(1) }.to raise_error(TwitterClient::Errors::ClientError)
|
||||
end
|
||||
|
||||
it "matches server errors" do
|
||||
allow(client).to receive(:status).and_raise(Twitter::Error::ServiceUnavailable)
|
||||
|
||||
expect { described_class.status(1) }.to raise_error(TwitterClient::Errors::ServerError)
|
||||
end
|
||||
|
||||
it "defaults to a generic error" do
|
||||
allow(client).to receive(:status).and_raise(Twitter::Error)
|
||||
|
||||
expect { described_class.status(1) }.to raise_error(TwitterClient::Errors::Error)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue