From 743dfad04cbcd9e4d451fe31d43d1bf2c02edf01 Mon Sep 17 00:00:00 2001 From: Vanessa Sant'Anna Date: Thu, 24 Nov 2016 10:51:55 -0500 Subject: [PATCH 1/5] Add API error description to error message. --- lib/yelp/error.rb | 3 ++- spec/yelp/error_spec.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/yelp/error.rb b/lib/yelp/error.rb index 9732cb6..c2ebb02 100644 --- a/lib/yelp/error.rb +++ b/lib/yelp/error.rb @@ -83,7 +83,8 @@ def initialize(msg='One or more parameters were invalid', error=nil) unless error.nil? @text = error['text'] @field = error['field'] - msg = msg + ': ' + @field + description = error.has_key?('description') ? '. Description: ' + error['description'] : '' + msg = msg + ': ' + @field + description end super(msg,error) end diff --git a/spec/yelp/error_spec.rb b/spec/yelp/error_spec.rb index bb5d2ff..2cc9118 100644 --- a/spec/yelp/error_spec.rb +++ b/spec/yelp/error_spec.rb @@ -40,5 +40,19 @@ end end + context 'when the API returns the error description' do + let(:response_body) { '{"error": {"text": "One or more parameters are invalid in request", "id": "INVALID_PARAMETER", "field": "limit", "description": "Limit maximum is 20"}}' } + + it 'should expose more details about the invalid parameter' do + begin + Yelp::Error.check_for_error(bad_response) + rescue Yelp::Error::InvalidParameter => e + # verifies that StandardError message attribute is available + expect(e.message).to eq('One or more parameters are invalid in request: limit. Description: Limit maximum is 20') + expect(e.field).to eq('limit') + + end + end + end end end From 22109178da31403c090ec16b252cd1407f297930 Mon Sep 17 00:00:00 2001 From: Vanessa Sant'Anna Date: Sun, 11 Dec 2016 11:50:02 -0500 Subject: [PATCH 2/5] Change code style when interpolating strings as per suggestion --- lib/yelp/error.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/yelp/error.rb b/lib/yelp/error.rb index c2ebb02..bf6fee4 100644 --- a/lib/yelp/error.rb +++ b/lib/yelp/error.rb @@ -83,8 +83,8 @@ def initialize(msg='One or more parameters were invalid', error=nil) unless error.nil? @text = error['text'] @field = error['field'] - description = error.has_key?('description') ? '. Description: ' + error['description'] : '' - msg = msg + ': ' + @field + description + msg += ": #{@field}" + msg += ". Description: #{error['description']}" if error.has_key?('description') end super(msg,error) end From f45c7130dae0a0dd9d6aeb6deecc9fca1e80e2c7 Mon Sep 17 00:00:00 2001 From: Vanessa Sant'Anna Date: Sun, 11 Dec 2016 11:51:05 -0500 Subject: [PATCH 3/5] Change new test code style to use raise_error as suggested. --- spec/yelp/error_spec.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/spec/yelp/error_spec.rb b/spec/yelp/error_spec.rb index 2cc9118..3a81d6a 100644 --- a/spec/yelp/error_spec.rb +++ b/spec/yelp/error_spec.rb @@ -44,14 +44,12 @@ let(:response_body) { '{"error": {"text": "One or more parameters are invalid in request", "id": "INVALID_PARAMETER", "field": "limit", "description": "Limit maximum is 20"}}' } it 'should expose more details about the invalid parameter' do - begin + expect { Yelp::Error.check_for_error(bad_response) - rescue Yelp::Error::InvalidParameter => e - # verifies that StandardError message attribute is available - expect(e.message).to eq('One or more parameters are invalid in request: limit. Description: Limit maximum is 20') - expect(e.field).to eq('limit') - - end + }.to raise_error { |error| + error.message.should eq('One or more parameters are invalid in request: limit. Description: Limit maximum is 20') + error.field.should eq('limit') + } end end end From 3de4a83cb0c4f176383cdfe231adadfe96288096 Mon Sep 17 00:00:00 2001 From: Vanessa Sant'Anna Date: Sun, 11 Dec 2016 11:55:39 -0500 Subject: [PATCH 4/5] Modernize remaining test to use raise_error --- spec/yelp/error_spec.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/spec/yelp/error_spec.rb b/spec/yelp/error_spec.rb index 3a81d6a..06a0726 100644 --- a/spec/yelp/error_spec.rb +++ b/spec/yelp/error_spec.rb @@ -30,14 +30,12 @@ end it 'should expose the field parameter' do - begin + expect{ Yelp::Error.check_for_error(bad_response) - rescue Yelp::Error::InvalidParameter => e - # verifies that StandardError message attribute is available - expect(e.message).to eq('One or more parameters are invalid in request: oauth_token') - # verifies that we can get access to the specific field that was invalid - expect(e.field).to eq('oauth_token') - end + }.to raise_error { |error| + error.message.should eq('One or more parameters are invalid in request: oauth_token') + error.field.should eq('oauth_token') + } end context 'when the API returns the error description' do From 48fa804ecca010df2a01f85235c7ac90fd890734 Mon Sep 17 00:00:00 2001 From: Vanessa Sant'Anna Date: Sun, 11 Dec 2016 16:28:43 -0500 Subject: [PATCH 5/5] Change style of raise_error usage * Prefer param vs block * Remove assertion for field --- spec/yelp/error_spec.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/spec/yelp/error_spec.rb b/spec/yelp/error_spec.rb index 06a0726..d51d4b0 100644 --- a/spec/yelp/error_spec.rb +++ b/spec/yelp/error_spec.rb @@ -29,13 +29,11 @@ }.to raise_error(Yelp::Error::InvalidParameter) end - it 'should expose the field parameter' do - expect{ + it 'should expose the field parameter' do + expect { Yelp::Error.check_for_error(bad_response) - }.to raise_error { |error| - error.message.should eq('One or more parameters are invalid in request: oauth_token') - error.field.should eq('oauth_token') - } + }.to raise_error ('One or more parameters are invalid in request: oauth_token') + end context 'when the API returns the error description' do @@ -44,10 +42,7 @@ it 'should expose more details about the invalid parameter' do expect { Yelp::Error.check_for_error(bad_response) - }.to raise_error { |error| - error.message.should eq('One or more parameters are invalid in request: limit. Description: Limit maximum is 20') - error.field.should eq('limit') - } + }.to raise_error ('One or more parameters are invalid in request: limit. Description: Limit maximum is 20') end end end