Skip to content

Commit

Permalink
Merge pull request #101 from gregory/fix_issue_100
Browse files Browse the repository at this point in the history
fix #100 bug when response is a string
  • Loading branch information
mattolson committed Apr 29, 2015
2 parents 8e61935 + 5ef9721 commit 9e61c3c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Next Release

* Your contribution here.
* [#101](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/101): Fix #100 when exception and env.body is a string. - [@gregory](https://github.com/gregory).

## 1.0.0
Please note that this is the start of a new major release which breaks all backward compatibility.
Expand Down
4 changes: 2 additions & 2 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
guard 'rspec', all_on_start: false, cmd: 'bundle exec rspec' do
# Watch spec directory
watch(/^spec\/.+_spec\.rb/)
watch(%r{^spec\/.+_spec\.rb})

# Watch lib/*.rb
watch(/^lib\/(.+)\.rb/) do |m|
watch(%r{^lib\/(.+)\.rb}) do |m|
"spec/bigcommerce/#{m[1]}_spec.rb"
end

Expand Down
39 changes: 23 additions & 16 deletions lib/bigcommerce/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ def initialize(headers)
end
end

class BadRequest < HttpError; end
class Unauthorized < HttpError; end
class Forbidden < HttpError; end
class NotFound < HttpError; end
class MethodNotAllowed < HttpError; end
class NotAccepted < HttpError; end
class TimeOut < HttpError; end
class ResourceConflict < HttpError; end
class TooManyRequests < HttpError; end
class InternalServerError < HttpError; end
class BadGateway < HttpError; end
class ServiceUnavailable < HttpError; end
class GatewayTimeout < HttpError; end
class BadRequest < HttpError; end
class Unauthorized < HttpError; end
class Forbidden < HttpError; end
class NotFound < HttpError; end
class MethodNotAllowed < HttpError; end
class NotAccepted < HttpError; end
class TimeOut < HttpError; end
class ResourceConflict < HttpError; end
class TooManyRequests < HttpError; end
class InternalServerError < HttpError; end
class BadGateway < HttpError; end
class ServiceUnavailable < HttpError; end
class GatewayTimeout < HttpError; end
class BandwidthLimitExceeded < HttpError; end

module HttpErrors
Expand All @@ -41,11 +41,18 @@ module HttpErrors

def throw_http_exception!(code, env)
return unless ERRORS.keys.include? code
error_hash = env.body.empty? ? {} : JSON.parse(env.body, symbolize_names: true)
response_headers = {}
unless env.body.empty?
response_headers = begin
JSON.parse(env.body, symbolize_names: true)
rescue
{}
end
end
unless env[:response_headers]['X-Retry-After'].nil?
error_hash[:retry_after] = env[:response_headers]['X-Retry-After'].to_i
response_headers[:retry_after] = env[:response_headers]['X-Retry-After'].to_i
end
fail ERRORS[code].new(error_hash), env.body
fail ERRORS[code].new(response_headers), env.body
end
end
end
13 changes: 13 additions & 0 deletions spec/bigcommerce/unit/exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@
expect(e.response_headers[:retry_after]).to eq 1
end
end

it 'handle string in body' do
message = 'Unauthorized'
code = 401
env = double(:env)
allow(env).to receive(:body) { message }
allow(env).to receive(:[]) { {} }
begin
dummy_class.throw_http_exception!(code, env)
rescue Bigcommerce::Unauthorized => e
expect(e.message).to eq message
end
end
end

context 'valid response status' do
Expand Down

0 comments on commit 9e61c3c

Please sign in to comment.