Skip to content

Commit 24c114b

Browse files
committed
Update error types
1 parent a7bb69c commit 24c114b

File tree

2 files changed

+88
-113
lines changed

2 files changed

+88
-113
lines changed

lib/shopify_app/managers/script_tags_manager.rb

+7-113
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,7 @@ def fetch_active_theme
159159
# Check for errors in the response
160160
if response.body["errors"].present?
161161
error_message = response.body["errors"].map { |e| e["message"] }.join(", ")
162-
raise "GraphQL error: #{error_message}"
163-
end
164-
165-
# Check if the response has the expected structure
166-
unless response.body["data"] && response.body["data"]["themes"] && response.body["data"]["themes"]["nodes"]
167-
raise "Invalid response structure"
162+
raise ShopifyAPI::Errors::InvalidGraphqlRequestError, error_message
168163
end
169164

170165
themes = response.body["data"]["themes"]["nodes"]
@@ -211,7 +206,7 @@ def fetch_json_templates(client, theme_id, template_filenames)
211206
# Check for errors in the response
212207
if files_response.body["errors"].present?
213208
error_message = files_response.body["errors"].map { |e| e["message"] }.join(", ")
214-
raise "GraphQL error: #{error_message}"
209+
raise ShopifyAPI::Errors::InvalidGraphqlRequestError, error_message
215210
end
216211

217212
files_response.body["data"]["theme"]["files"]["nodes"]
@@ -253,7 +248,7 @@ def all_sections_support_app_blocks?(client, theme_id, section_filenames)
253248
# Check for errors in the section response
254249
if section_response.body["errors"].present?
255250
error_message = section_response.body["errors"].map { |e| e["message"] }.join(", ")
256-
raise "GraphQL error: #{error_message}"
251+
raise ShopifyAPI::Errors::InvalidGraphqlRequestError, error_message
257252
end
258253

259254
section_files = section_response.body["data"]["theme"]["files"]["nodes"]
@@ -275,89 +270,6 @@ def all_sections_support_app_blocks?(client, theme_id, section_filenames)
275270
false
276271
end
277272

278-
# Keep these methods for backward compatibility
279-
def template_supports_app_blocks?(theme_id, template_type)
280-
client = graphql_client
281-
282-
# First, check if the JSON template exists and find the main section
283-
main_section = find_main_section(client, theme_id, template_type)
284-
return false unless main_section
285-
286-
# Now check if the main section supports app blocks
287-
schema_supports_app_blocks?(client, theme_id, main_section)
288-
end
289-
290-
def find_main_section(client, theme_id, template_type)
291-
filename = "templates/#{template_type}.json"
292-
files_variables = {
293-
themeId: theme_id,
294-
filenames: [filename],
295-
}
296-
297-
files_response = client.query(query: FILES_QUERY, variables: files_variables)
298-
299-
# Check for errors in the response
300-
if files_response.body["errors"].present?
301-
error_message = files_response.body["errors"].map { |e| e["message"] }.join(", ")
302-
raise "GraphQL error: #{error_message}"
303-
end
304-
305-
template_files = files_response.body["data"]["theme"]["files"]["nodes"]
306-
307-
# If the JSON template doesn't exist, return nil
308-
return nil if template_files.empty?
309-
310-
# Parse the JSON template to find the main section
311-
template_content = template_files.first["body"]["content"]
312-
template_data = JSON.parse(template_content)
313-
314-
main_section = nil
315-
template_data["sections"].each do |id, section|
316-
if id == "main" || section["type"].to_s.start_with?("main-")
317-
main_section = "sections/#{section["type"]}.liquid"
318-
break
319-
end
320-
end
321-
322-
main_section
323-
rescue => e
324-
ShopifyApp::Logger.error("Error finding main section: #{e.message}")
325-
nil
326-
end
327-
328-
def schema_supports_app_blocks?(client, theme_id, section_filename)
329-
section_variables = {
330-
themeId: theme_id,
331-
filenames: [section_filename],
332-
}
333-
334-
section_response = client.query(query: FILES_QUERY, variables: section_variables)
335-
336-
# Check for errors in the section response
337-
if section_response.body["errors"].present?
338-
error_message = section_response.body["errors"].map { |e| e["message"] }.join(", ")
339-
raise "GraphQL error: #{error_message}"
340-
end
341-
342-
section_files = section_response.body["data"]["theme"]["files"]["nodes"]
343-
344-
return false if section_files.empty?
345-
346-
section_content = section_files.first["body"]["content"]
347-
348-
# Extract schema from the section content
349-
schema_match = section_content.match(/\{\%\s+schema\s+\%\}([\s\S]*?)\{\%\s+endschema\s+\%\}/m)
350-
return false unless schema_match
351-
352-
schema = JSON.parse(schema_match[1])
353-
354-
# Check if the schema has blocks that support app blocks
355-
schema["blocks"]&.any? { |block| block["type"] == "@app" } || false
356-
rescue => e
357-
ShopifyApp::Logger.error("Error checking schema support: #{e.message}")
358-
false
359-
end
360-
361273
def expanded_script_tags
362274
self.class.build_src(required_script_tags, shop_domain)
363275
end
@@ -379,25 +291,13 @@ def create_script_tag(attributes)
379291

380292
response = client.query(query: SCRIPT_TAG_CREATE_MUTATION, variables: variables)
381293

382-
# Add proper nil checks for the response structure
383-
if response.body["data"] &&
384-
response.body["data"]["scriptTagCreate"] &&
385-
response.body["data"]["scriptTagCreate"]["userErrors"] &&
386-
response.body["data"]["scriptTagCreate"]["userErrors"].any?
294+
if response.body["data"]["scriptTagCreate"]["userErrors"].any?
387295
errors = response.body["data"]["scriptTagCreate"]["userErrors"]
388296
error_messages = errors.map { |e| "#{e["field"]}: #{e["message"]}" }.join(", ")
389297
raise ::ShopifyApp::CreationFailed, "ScriptTag creation failed: #{error_messages}"
390298
end
391299

392-
# Safely access the script tag data
393-
if response.body["data"] &&
394-
response.body["data"]["scriptTagCreate"] &&
395-
response.body["data"]["scriptTagCreate"]["scriptTag"]
396-
response.body["data"]["scriptTagCreate"]["scriptTag"]
397-
else
398-
ShopifyApp::Logger.warn("Script tag creation response missing expected data structure")
399-
nil
400-
end
300+
response.body["data"]["scriptTagCreate"]["scriptTag"]
401301
rescue ShopifyAPI::Errors::HttpResponseError => e
402302
raise ::ShopifyApp::CreationFailed, e.message
403303
end
@@ -427,17 +327,11 @@ def fetch_all_script_tags
427327

428328
# Check for errors in the response
429329
if response.body["errors"].present?
430-
ShopifyApp::Logger.warn("GraphQL error fetching script tags: #{response.body["errors"].map do |e|
431-
e["message"]
432-
end.join(", ")}")
330+
error_messages = response.body["errors"].map { |e| e["message"] }.join(", ")
331+
ShopifyApp::Logger.warn("GraphQL error fetching script tags: #{error_messages}")
433332
return []
434333
end
435334

436-
# Handle nil data or missing structure
437-
return [] unless response.body["data"] &&
438-
response.body["data"]["scriptTags"] &&
439-
response.body["data"]["scriptTags"]["edges"]
440-
441335
response.body["data"]["scriptTags"]["edges"].map { |edge| edge["node"] }
442336
rescue => e
443337
ShopifyApp::Logger.warn("Error fetching script tags: #{e.message}")

test/shopify_app/managers/script_tags_manager_test.rb

+81
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,87 @@ class ShopifyApp::ScriptTagsManagerTest < ActiveSupport::TestCase
488488
manager.create_script_tags(session: @session)
489489
end
490490

491+
test "#fetch_active_theme raises InvalidGraphqlRequestError when GraphQL errors are present" do
492+
manager = ShopifyApp::ScriptTagsManager.new(@script_tags, "example-app.com")
493+
manager.stubs(:graphql_client).returns(@mock_client)
494+
495+
# Mock GraphQL error response
496+
error_response = mock
497+
error_response.stubs(:body).returns({
498+
"errors" => [
499+
{
500+
"message" => "GraphQL error message",
501+
"locations" => [{ "line" => 2, "column" => 11 }],
502+
"path" => ["themes"],
503+
},
504+
],
505+
"data" => nil,
506+
})
507+
508+
# Set up the response for the GraphQL client
509+
@mock_client.expects(:query).returns(error_response).once
510+
511+
# Stub the rescue block to verify the error is raised before it's caught
512+
ShopifyApp::Logger.expects(:warn).with("Failed to fetch active theme: GraphQL error message")
513+
514+
# The method will return nil due to the rescue block
515+
assert_nil manager.send(:fetch_active_theme)
516+
end
517+
518+
test "#fetch_json_templates raises InvalidGraphqlRequestError when GraphQL errors are present" do
519+
manager = ShopifyApp::ScriptTagsManager.new(@script_tags, "example-app.com")
520+
manager.stubs(:graphql_client).returns(@mock_client)
521+
522+
# Mock GraphQL error response
523+
error_response = mock
524+
error_response.stubs(:body).returns({
525+
"errors" => [
526+
{
527+
"message" => "GraphQL error message",
528+
"locations" => [{ "line" => 2, "column" => 11 }],
529+
"path" => ["theme", "files"],
530+
},
531+
],
532+
"data" => nil,
533+
})
534+
535+
# Set up the response for the GraphQL client
536+
@mock_client.expects(:query).returns(error_response).once
537+
538+
# This method doesn't have a rescue block, so we can test for the exception directly
539+
assert_raises(ShopifyAPI::Errors::InvalidGraphqlRequestError) do
540+
manager.send(:fetch_json_templates, @mock_client, "theme_id", ["templates/product.json"])
541+
end
542+
end
543+
544+
test "#all_sections_support_app_blocks? raises InvalidGraphqlRequestError when GraphQL errors are present" do
545+
manager = ShopifyApp::ScriptTagsManager.new(@script_tags, "example-app.com")
546+
manager.stubs(:graphql_client).returns(@mock_client)
547+
548+
# Mock GraphQL error response
549+
error_response = mock
550+
error_response.stubs(:body).returns({
551+
"errors" => [
552+
{
553+
"message" => "GraphQL error message",
554+
"locations" => [{ "line" => 2, "column" => 11 }],
555+
"path" => ["theme", "files"],
556+
},
557+
],
558+
"data" => nil,
559+
})
560+
561+
# Set up the response for the GraphQL client
562+
@mock_client.expects(:query).returns(error_response).once
563+
564+
# Stub the rescue block to verify the error is raised before it's caught
565+
ShopifyApp::Logger.expects(:error).with("Error checking section support: GraphQL error message")
566+
567+
# The method will return false due to the rescue block
568+
assert_equal false,
569+
manager.send(:all_sections_support_app_blocks?, @mock_client, "theme_id", ["sections/main-product.liquid"])
570+
end
571+
491572
private
492573

493574
def mock_empty_script_tags_response

0 commit comments

Comments
 (0)