Skip to content

Commit

Permalink
Move text error codes to #error_code field (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillplatonov authored Oct 21, 2024
1 parent 62d8968 commit d33d439
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
32 changes: 17 additions & 15 deletions lib/shopify_graphql/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,33 @@ def handle_graphql_errors(response)
doc: error.extensions&.documentation
)

case error_code
when "THROTTLED"
raise TooManyRequests.new(response: response), error_message
when "INTERNAL_SERVER_ERROR"
raise ServerError.new(response: response), error_message
else
raise ConnectionError.new(response: response), error_message
end
exception = case error_code
when "THROTTLED"
TooManyRequests.new(response: response)
when "INTERNAL_SERVER_ERROR"
ServerError.new(response: response)
else
ConnectionError.new(response: response)
end
exception.error_code = error_code
raise exception, error_message
end

def handle_user_errors(response)
return response if response.userErrors.blank?

error = response.userErrors.first
error_code = error.code
error_message = generate_error_message(
message: error.message,
code: error.code,
code: error_code,
fields: error.field,
)
raise UserError.new(
response: response,
message: error.message,
code: error.code,
fields: error.field,
), error_message

exception = UserError.new(response: response)
exception.error_code = error_code
exception.fields = error.field
raise exception, error_message
end

def generate_error_message(message: nil, code: nil, doc: nil, fields: nil)
Expand Down
12 changes: 3 additions & 9 deletions lib/shopify_graphql/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module ShopifyGraphql
class ConnectionError < ShopifyAPI::Errors::HttpResponseError
attr_accessor :error_code, :fields

def initialize(response: nil)
unless response
empty_response = ShopifyAPI::Clients::HttpResponse.new(code: 200, headers: {}, body: "")
Expand Down Expand Up @@ -72,14 +74,6 @@ class ServerError < ConnectionError
end

# Custom error for Graphql userErrors handling
class UserError < StandardError
attr_reader :response, :message, :code, :fields

def initialize(response:, message:, code:, fields:)
@response = response
@message = message
@code = code
@fields = fields
end
class UserError < ConnectionError
end
end
17 changes: 16 additions & 1 deletion test/error_handling_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,22 @@ class ErrorHandlingTest < ActiveSupport::TestCase
response = ShopifyGraphql.execute(SIMPLE_QUERY)
ShopifyGraphql::Client.new.handle_user_errors(response.data.metafieldDefinitionCreate)
rescue ShopifyGraphql::UserError => error
assert_equal "TAKEN", error.code
assert_equal 200, error.code
assert_equal "TAKEN", error.error_code
assert_includes error.message, "Key is in use for Product metafields"
assert_equal ["definition", "key"], error.fields
end
end

test "server error" do
fake("mutations/server_error.json", SIMPLE_QUERY)

begin
ShopifyGraphql.execute(SIMPLE_QUERY)
rescue ShopifyGraphql::ServerError => error
assert_equal 200, error.code
assert_equal "INTERNAL_SERVER_ERROR", error.error_code
assert_includes error.message, "Looks like something went wrong on our end"
end
end
end
11 changes: 11 additions & 0 deletions test/fixtures/mutations/server_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"errors": [
{
"message": "Internal error. Looks like something went wrong on our end.\nRequest ID: XXXXX (include this in support requests).",
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"requestId": "XXXXX"
}
}
]
}

0 comments on commit d33d439

Please sign in to comment.