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

Enrich 4xx error message with error text from response.body #647

Merged
merged 1 commit into from
Dec 5, 2019
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
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