Skip to content

Commit a48072f

Browse files
Add more tests for error handling
1 parent 2b77c15 commit a48072f

File tree

5 files changed

+79
-24
lines changed

5 files changed

+79
-24
lines changed

lib/shopify_graphql/client.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ def execute(query, **variables)
1111
Response.new(handle_response(e.response))
1212
rescue JSON::ParserError => e
1313
raise ServerError.new(e, "Invalid JSON response")
14-
rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNREFUSED, Errno::ENETUNREACH, Net::ReadTimeout, Net::OpenTimeout, OpenSSL::SSL::SSLError, EOFError, Socket::ResolutionError => e
14+
rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNREFUSED, Errno::ENETUNREACH, Net::ReadTimeout, Net::OpenTimeout, OpenSSL::SSL::SSLError, EOFError => e
1515
raise ServerError.new(e, "Network error")
16+
rescue => e
17+
if (defined?(Socket::ResolutionError) and e.is_a?(Socket::ResolutionError))
18+
raise ServerError.new(response: response), "Network error: #{e.message}"
19+
else
20+
raise e
21+
end
1622
end
1723

1824
def parsed_body(response)

test/error_handling_test.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require "test_helper"
2+
3+
class ErrorHandlingTest < ActiveSupport::TestCase
4+
SIMPLE_QUERY = <<~GRAPHQL
5+
query {
6+
shop {
7+
id
8+
name
9+
myshopifyDomain
10+
}
11+
}
12+
GRAPHQL
13+
14+
test "handles invalid JSON response" do
15+
fake("error_page.html", SIMPLE_QUERY)
16+
17+
assert_raises ShopifyGraphql::ServerError do
18+
ShopifyGraphql.execute(SIMPLE_QUERY)
19+
end
20+
end
21+
22+
test "handles network error" do
23+
fake_error(Errno::ECONNRESET)
24+
25+
assert_raises ShopifyGraphql::ServerError do
26+
ShopifyGraphql.execute(SIMPLE_QUERY)
27+
end
28+
end
29+
30+
test "invalid input error" do
31+
fake("mutations/invalid_input_error.json", SIMPLE_QUERY)
32+
33+
assert_raises ShopifyGraphql::ConnectionError do
34+
ShopifyGraphql.execute(SIMPLE_QUERY)
35+
end
36+
end
37+
end

test/exceptions_test.rb

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"errors": [
3+
{
4+
"message": "Variable $input of type ProductInput! was provided invalid value for title1 (Field is not defined on ProductInput)",
5+
"locations": [
6+
{
7+
"line": 1,
8+
"column": 10
9+
}
10+
],
11+
"extensions": {
12+
"value": {
13+
"id": "gid://shopify/Product/8077118406833",
14+
"title1": "Test Prod 1"
15+
},
16+
"problems": [
17+
{
18+
"path": [
19+
"title1"
20+
],
21+
"explanation": "Field is not defined on ProductInput"
22+
}
23+
]
24+
}
25+
}
26+
]
27+
}

test/test_helper.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
end
2020

2121
class ActiveSupport::TestCase
22+
API_PATH = "https://test-shop.myshopify.com/admin/api/2023-07/graphql.json"
23+
2224
setup do
2325
session = ShopifyAPI::Auth::Session.new(shop: "test-shop.myshopify.com", access_token: "test-token")
2426
ShopifyAPI::Context.activate_session(session)
@@ -29,12 +31,16 @@ class ActiveSupport::TestCase
2931
end
3032

3133
def fake(fixture_path, query, **variables)
32-
api_path = "https://test-shop.myshopify.com/admin/api/2023-07/graphql.json"
3334
fixture = File.read File.expand_path("fixtures/#{fixture_path}", __dir__)
3435
body = { query: query, variables: variables }
3536

36-
stub_request(:post, api_path)
37+
stub_request(:post, API_PATH)
3738
.with(body: body)
3839
.to_return(body: fixture)
3940
end
41+
42+
def fake_error(error_class)
43+
stub_request(:post, API_PATH)
44+
.to_raise(error_class)
45+
end
4046
end

0 commit comments

Comments
 (0)