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

JSON parseable error messages #1140

Merged
merged 2 commits into from
Apr 12, 2023
Merged
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
@@ -4,6 +4,8 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api

## Unreleased

- [#1140](https://github.com/Shopify/shopify-api-ruby/pull/1140) ⚠️ [Breaking] Reformat Http error messages to be JSON parsable.

## 12.5.0

- [#1113](https://github.com/Shopify/shopify-api-ruby/pull/1113) Handle JSON::ParserError when http response is HTML and raise ShopifyAPI::Errors::HttpResponseError
22 changes: 13 additions & 9 deletions lib/shopify_api/clients/http_client.rb
Original file line number Diff line number Diff line change
@@ -65,15 +65,7 @@ def request(request)

break if response.ok?

error_messages = []
error_messages << response.body["errors"] if response.body["errors"]

if response.headers["x-request-id"]
id = T.must(response.headers["x-request-id"])[0]
error_messages << "If you report this error, please include this id: #{id}."
end

error_message = error_messages.join("\n")
error_message = serialized_error(response)

unless [429, 500].include?(response.code)
raise ShopifyAPI::Errors::HttpResponseError.new(response: response), error_message
@@ -102,6 +94,18 @@ def request(request)
def request_url(request)
"#{@base_uri_and_path}/#{request.path}"
end

sig { params(response: HttpResponse).returns(String) }
def serialized_error(response)
body = {}
body["errors"] = response.body["errors"] if response.body["errors"]

if response.headers["x-request-id"]
id = T.must(response.headers["x-request-id"])[0]
body["error_reference"] = "If you report this error, please include this id: #{id}."
end
body.to_json
end
end
end
end
19 changes: 19 additions & 0 deletions test/clients/http_client_test.rb
Original file line number Diff line number Diff line change
@@ -123,6 +123,25 @@ def test_request_with_invalid_request
assert_raises(ShopifyAPI::Errors::InvalidHttpRequestError) { @client.request(@request) }
end

def test_error_message_structure
error_response_body = {
"errors": { "line_items" => ["must have at least one line item"] },
}.to_json
response_headers = {
"x-request-id": 1234,
}
stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(body: @request.body.to_json, query: @request.query, headers: @expected_headers)
.to_return(body: error_response_body, status: 422, headers: response_headers)

response = assert_raises(ShopifyAPI::Errors::HttpResponseError) do
@client.request(@request)
end
parsed_error = JSON.parse(response.message)
assert(parsed_error["errors"].present?)
assert(parsed_error["error_reference"].present?)
end

def test_non_retriable_error_code
stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(body: @request.body.to_json, query: @request.query, headers: @expected_headers)