From 5ef97210f7f4f5c99deadaf3086cc4d57ed6aa35 Mon Sep 17 00:00:00 2001 From: gregory Date: Tue, 28 Apr 2015 16:04:30 -0700 Subject: [PATCH] fix #100 bug when response is a string We should rescue the JSON parsing fix rubocop --- CHANGELOG.md | 1 + Guardfile | 4 +-- lib/bigcommerce/exception.rb | 39 +++++++++++++++---------- spec/bigcommerce/unit/exception_spec.rb | 13 +++++++++ 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b911f8..bf2aa6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Guardfile b/Guardfile index 657ae38..0d29b09 100644 --- a/Guardfile +++ b/Guardfile @@ -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 diff --git a/lib/bigcommerce/exception.rb b/lib/bigcommerce/exception.rb index 8a461d4..f78cf23 100644 --- a/lib/bigcommerce/exception.rb +++ b/lib/bigcommerce/exception.rb @@ -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 @@ -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 diff --git a/spec/bigcommerce/unit/exception_spec.rb b/spec/bigcommerce/unit/exception_spec.rb index ed1272a..4746369 100644 --- a/spec/bigcommerce/unit/exception_spec.rb +++ b/spec/bigcommerce/unit/exception_spec.rb @@ -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