Skip to content

Commit

Permalink
Merge branch 'dont-raise-errors-if-we-said-dont-raise-errors' into 'm…
Browse files Browse the repository at this point in the history
…ain'

Only instantiate OAuth2::Error if raise_errors option is true

See merge request oauth-xx/oauth2!639
  • Loading branch information
pboling committed Jan 24, 2024
2 parents d5d10c9 + 84b0678 commit f050833
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/oauth2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,10 @@ def request(verb, url, opts = {}, &block)
# on non-redirecting 3xx statuses, just return the response
response
when 400..599
error = Error.new(response)
raise(error) if opts.fetch(:raise_errors, options[:raise_errors])
if opts.fetch(:raise_errors, options[:raise_errors])
error = Error.new(response)
raise(error)
end

response
else
Expand Down
8 changes: 8 additions & 0 deletions spec/oauth2/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
stub.get('/redirect_no_loc') { |_env| [302, {'Content-Type' => 'text/plain'}, ''] }
stub.post('/redirect') { |_env| [303, {'Content-Type' => 'text/plain', 'location' => '/reflect'}, ''] }
stub.get('/error') { |_env| [500, {'Content-Type' => 'text/plain'}, 'unknown error'] }
stub.get('/unparsable_error') { |_env| [500, {'Content-Type' => 'application/json'}, 'unknown error'] }
stub.get('/empty_get') { |_env| [204, {}, nil] }
stub.get('/different_encoding') { |_env| [500, {'Content-Type' => 'application/json'}, NKF.nkf('-We', JSON.dump(error: error_value, error_description: '∞'))] }
stub.get('/ascii_8bit_encoding') { |_env| [500, {'Content-Type' => 'application/json'}, JSON.dump(error: 'invalid_request', error_description: 'é').force_encoding('ASCII-8BIT')] }
Expand Down Expand Up @@ -95,6 +96,13 @@
described_class.new 'abc', 'def', opts
expect(opts).to eq(opts2)
end

it 'raises exception if JSON is expected, but server returns invalid JSON' do
client = instance
expect { client.request(:get, '/unparsable_error') }.to raise_error(JSON::ParserError)
response = client.request(:get, '/unparsable_error', raise_errors: false)
expect(response.status).to eq(500)
end
end

describe '#site=(val)' do
Expand Down

0 comments on commit f050833

Please sign in to comment.