Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JSON (result.body) for exception messages in requests #67

Merged
merged 3 commits into from
Jun 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---
language: ruby
sudo: false
cache: bundler
rvm:
- 2.1.0
- 2.2.1
- 2.1.9
- 2.2.5
- 2.3.1
script: "./build_travis.sh"
after_success: "./deploy_documentation.sh"
deploy:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ source 'http://rubygems.org'
gemspec

group :development do
gem 'terminal-notifier-guard', require: false
gem 'terminal-notifier-guard', require: false unless ENV['TRAVIS']
gem 'coveralls', require: false
gem 'rubocop', require: false
gem 'yard', require: false
Expand Down
2 changes: 1 addition & 1 deletion auth0.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |s|

s.add_development_dependency 'rake', '~> 10.4'
s.add_development_dependency 'fuubar', '~> 2.0'
s.add_development_dependency 'guard-rspec', '~> 4.5'
s.add_development_dependency 'guard-rspec', '~> 4.5' unless ENV['TRAVIS']
s.add_development_dependency 'dotenv-rails', '~> 2.0'
s.add_development_dependency 'pry', '~> 0.10'
s.add_development_dependency 'pry-nav', '~> 0.2.4'
Expand Down
28 changes: 14 additions & 14 deletions lib/auth0/mixins/httparty_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ module HTTPartyProxy
else
result = self.class.send(method, safe_path, body: body.to_json)
end
response_body =
begin
JSON.parse(result.body.to_s)
rescue JSON::ParserError
result.body
end

case result.code
when 200...226 then response_body
when 400 then fail Auth0::BadRequest, response_body
when 401 then fail Auth0::Unauthorized, response_body
when 403 then fail Auth0::AccessDenied, response_body
when 404 then fail Auth0::NotFound, response_body
when 500 then fail Auth0::ServerError, response_body
else
fail Auth0::Unsupported, response_body
when 200...226 then safe_parse_json(result.body)
when 400 then fail Auth0::BadRequest, result.body
when 401 then fail Auth0::Unauthorized, result.body
when 403 then fail Auth0::AccessDenied, result.body
when 404 then fail Auth0::NotFound, result.body
when 500 then fail Auth0::ServerError, result.body
else fail Auth0::Unsupported, result.body
end
end
end

def safe_parse_json(body)
JSON.parse(body.to_s)
rescue JSON::ParserError
body
end
end
end
end
15 changes: 15 additions & 0 deletions spec/lib/auth0/mixins/httparty_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'json'
require 'spec_helper'

describe Auth0::Mixins::HTTPartyProxy do
before :all do
dummy_instance = DummyClassForProxy.new
Expand Down Expand Up @@ -143,6 +145,19 @@
.and_return(StubResponse.new('{}', true, 200))
expect { @instance.send(http_method, '/te st') }.not_to raise_error
end

it "should give the JSON representation of the error as the error message" do
allow(DummyClassForProxy).to receive(http_method).with('http://login.auth0.com/test', body: '{}')
res = JSON.generate({
"statusCode"=>404,
"error"=>"Bad Request",
"message"=>"Path validation error: 'String does not match pattern ^.+\\|.+$: 3241312' on property id (The user_id of the user to retrieve).",
"errorCode"=>"invalid_uri"
})
expect(DummyClassForProxy).to receive(http_method).with('/test', body: '{}')
.and_return(StubResponse.new(res, false, 404))
expect { @instance.send(http_method, '/test') }.to raise_error(Auth0::NotFound, res)
end
end
end
end