Skip to content

Commit

Permalink
Merge pull request #647 from vbyno/enrich_4xx_error_messages
Browse files Browse the repository at this point in the history
Enrich 4xx error message with error text from response.body
  • Loading branch information
Tim Anema committed Dec 5, 2019
2 parents 24d2640 + d2bd6d2 commit b458fb9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Enrich 4xx errors with error message from response body [#647](https://github.com/Shopify/shopify_api/pull/647)

== Version 8.0.0

* Api Version changes [#600](https://github.com/Shopify/shopify_api/pull/600)
Expand Down
1 change: 1 addition & 0 deletions lib/shopify_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module ShopifyAPI
require 'shopify_api/countable'
require 'shopify_api/resources'
require 'shopify_api/session'
require 'shopify_api/message_enricher'
require 'shopify_api/connection'
require 'shopify_api/pagination_link_headers'

Expand Down
2 changes: 1 addition & 1 deletion lib/shopify_api/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Connection < ActiveResource::Connection

module ResponseCapture
def handle_response(response)
@response = super
@response = super(ShopifyAPI::MessageEnricher.new(response))
end
end

Expand Down
17 changes: 17 additions & 0 deletions lib/shopify_api/message_enricher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module ShopifyAPI
class MessageEnricher < SimpleDelegator
def message
return super unless (400...500).include?(code.to_i)

@_cached_message ||= begin
detailed_error = begin
JSON.parse(body)['error'].to_s
rescue JSON::ParserError
nil
end

detailed_error.present? ? "#{super} (#{detailed_error})" : super
end
end
end
end
33 changes: 33 additions & 0 deletions test/message_enricher_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'test_helper'

class MessageEnricherTest < Test::Unit::TestCase

def test_enriches_initial_message_when_body_is_passed
response = enriched_response(422, 'InitialMessage', { error: 'My Error' })

assert_equal 'InitialMessage (My Error)', response.message
end

def test_returns_initial_message_when_code_is_200
response = enriched_response(200, 'InitialMessage', { result: 'Success' })

assert_equal 'InitialMessage', response.message
end

def test_returns_initial_message_when_body_cant_be_parsed
response = enriched_response(422, 'InitialMessage', 'not a json')

assert_equal 'InitialMessage', response.message
end

private

def enriched_response(code, message, body)
mock_response =
Struct
.new(:code, :message, :body)
.new(code.to_s, message.to_s, body.to_json)

ShopifyAPI::MessageEnricher.new(mock_response)
end
end

0 comments on commit b458fb9

Please sign in to comment.