Skip to content

Commit

Permalink
JSON parseable error messages (#1140)
Browse files Browse the repository at this point in the history
* JSON parseable error messages

* changelog
  • Loading branch information
nelsonwittwer committed Apr 12, 2023
1 parent 74620a4 commit a05d67e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions lib/shopify_api/clients/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Up @@ -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)
Expand Down

0 comments on commit a05d67e

Please sign in to comment.