Skip to content

Commit

Permalink
Clear last_response on errors
Browse files Browse the repository at this point in the history
When an error occurs, the last_response does not get updated so it's
possible to start inspecting the last last response.

This change makes it so that if an error is raised, clear the
last_response cache to prevent leaky reads
  • Loading branch information
Jack Li committed May 21, 2020
1 parent 9560329 commit b0e85f2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/octokit/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ def request(method, path, data, options = {})

@last_response = response = agent.call(method, Addressable::URI.parse(path.to_s).normalize.to_s, data, options)
response.data
rescue Octokit::Error => error
@last_response = nil
raise error
end

# Executes the request, checking if it was successful
Expand All @@ -177,7 +180,7 @@ def sawyer_options
conn_opts[:proxy] = @proxy if @proxy
if conn_opts[:ssl].nil?
conn_opts[:ssl] = { :verify_mode => @ssl_verify_mode } if @ssl_verify_mode
else
else
if @connection_options[:ssl][:verify] == false
conn_opts[:ssl] = { :verify_mode => 0}
else
Expand Down
16 changes: 16 additions & 0 deletions spec/octokit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,22 @@
expect { Octokit.get('/user') }.to raise_error Octokit::ServerError
end

it "resets last_response on errors" do
stub_get('/booya').to_return(:status => 200)
stub_get('/user').to_return \
:status => 509,
:headers => {
:content_type => "application/json",
},
:body => {:message => "Bandwidth exceeded"}.to_json

client = Octokit.client
client.get('/booya')
expect(client.last_response).to_not be_nil
expect { client.get('/user') }.to raise_error Octokit::ServerError
expect(client.last_response).to be_nil
end

it "handles documentation URLs in error messages" do
stub_get('/user').to_return \
:status => 415,
Expand Down

0 comments on commit b0e85f2

Please sign in to comment.