Skip to content

Commit

Permalink
Makes the retryable_request method also retry when a `RDStation::Erro…
Browse files Browse the repository at this point in the history
…r::Unauthorized` error happen (#78)

* fix: Also retry when a 'RDStation::Error::Unauthorized' error happen

* feat: Defines a new version and update changelog

Co-authored-by: Renan Porto <[email protected]>
  • Loading branch information
Hilderlan and tota1099 authored Dec 9, 2022
1 parent 24f6849 commit 7d706ac
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.8.1

- Makes the retryable_request method also retry when a `RDStation::Error::Unauthorized` error happen

## 2.8.0

### Additions
Expand Down
2 changes: 1 addition & 1 deletion lib/rdstation/retryable_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def retryable_request(authorization)
retries = 0
begin
yield authorization
rescue ::RDStation::Error::ExpiredAccessToken => e
rescue ::RDStation::Error::ExpiredAccessToken, ::RDStation::Error::Unauthorized => e
raise if !retry_possible?(authorization) || retries >= MAX_RETRIES

retries += 1
Expand Down
2 changes: 1 addition & 1 deletion lib/rdstation/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RDStation
VERSION = '2.8.0'
VERSION = '2.8.1'
end
20 changes: 19 additions & 1 deletion spec/lib/rdstation/retryable_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class DummyClass
and_return(new_credentials)
end

it 'refreshes the access_token and retries the request' do
it 'refreshes the access_token and retries the request when an ExpiredAccessToken error happen' do
dummy_request = double("dummy_request")
expect(dummy_request).to receive(:call).twice do |auth|
expired_token = ::RDStation::Error::ExpiredAccessToken.new({'error_message' => 'x'})
Expand All @@ -74,6 +74,24 @@ class DummyClass
end.not_to raise_error
end

it 'refreshes the access_token and retries the request an Unauthorized error happen' do
dummy_request = double("dummy_request")
expect(dummy_request).to receive(:call).twice do |auth|
expired_token = ::RDStation::Error::Unauthorized.new({'error_message' => 'x'})
raise expired_token unless auth.access_token == new_access_token
end

expect(RDStation.configuration.access_token_refresh_callback)
.to receive(:call)
.once do |authorization|
expect(authorization.access_token).to eq new_access_token
end

expect do
subject.retryable_request(auth) { |yielded_auth| dummy_request.call(yielded_auth) }
end.not_to raise_error
end

context 'and keeps raising retryable exception event after token refreshed' do
it 'retries only once' do
dummy_request = double("dummy_request")
Expand Down

0 comments on commit 7d706ac

Please sign in to comment.