Skip to content

Commit

Permalink
changing condition for raising an error after retrying a query
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcam-src committed Mar 12, 2024
1 parent 1dcfb10 commit e3b0b7c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
19 changes: 11 additions & 8 deletions app/services/tasks/dimensions_query_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ def query_dimensions(with_doi: true, page_size: 100)
cursor += page_size

break if cursor >= total_count
elsif response.code == 403 && !retry_attempted
# If the token has expired, retrieve a new token and try the query again
Rails.logger.warn('Received 403 Forbidden error. Retrying after token refresh.')
token = retrieve_token
redo
elsif response.code != 200 && retry_attempted
# If the token has expired and retry has already been attempted, raise a specific error
raise DimensionsPublicationQueryError, 'Retry attempted after token refresh failed with 403 Forbidden error'
elsif response.code == 403
if !retry_attempted
# If the token has expired, retrieve a new token and try the query again
Rails.logger.warn('Received 403 Forbidden error. Retrying after token refresh.')
token = retrieve_token
retry_attempted = true
redo
else
# If the token has expired and retry has already been attempted, raise a specific error
raise DimensionsPublicationQueryError, 'Retry attempted after token refresh failed with 403 Forbidden error'
end
else
raise DimensionsPublicationQueryError, "Failed to retrieve UNC affiliated articles from dimensions. Status code #{response.code}, response body: #{response.body}"
end
Expand Down
7 changes: 4 additions & 3 deletions spec/services/tasks/dimensions_query_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,22 @@
end

# Checks that the function only retries once to prevent infinite loops
it 'raises and logs an error if the query returns a non 200 status code after a token refresh' do
it 'raises and logs an error if the query returns another 403 status code after a token refresh' do
allow(Rails.logger).to receive(:error)
allow(Rails.logger).to receive(:warn)

unauthorized_status = 403
server_error_status = 500
unauthorized_body = 'Unauthorized'
stub_request(:post, 'https://app.dimensions.ai/api/dsl')
.to_return({status: unauthorized_status, body: 'Unauthorized'}, {status: server_error_status, body: 'Internal Server Error'})
.to_return({status: unauthorized_status, body: unauthorized_body}, {status: unauthorized_status, body: unauthorized_body})

expect { service.query_dimensions }.to raise_error(Tasks::DimensionsQueryService::DimensionsPublicationQueryError)
expect(WebMock).to have_requested(:post, 'https://app.dimensions.ai/api/auth').times(2)
expect(Rails.logger).to have_received(:warn).with('Received 403 Forbidden error. Retrying after token refresh.').once

# Check if the error message has been logged
expect(Rails.logger).to have_received(:error).with("HTTParty error during Dimensions API query: Failed to retrieve UNC affiliated articles from dimensions. Status code #{server_error_status}, response body: Internal Server Error")
expect(Rails.logger).to have_received(:error).with('HTTParty error during Dimensions API query: Retry attempted after token refresh failed with 403 Forbidden error')
end

# Simulating token reretrieval and retry after expiration during query
Expand Down

0 comments on commit e3b0b7c

Please sign in to comment.