From 7bc376b21cc8773230591373792e6e1c2192a87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20G=C3=B6kdemir?= Date: Mon, 12 Oct 2026 22:06:21 +0300 Subject: [PATCH] fix(rest): separate resource errors from base errors --- CHANGELOG.md | 1 + lib/shopify_api/rest/base.rb | 11 +++++++---- test/rest/base_errors_test.rb | 25 ++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56f20cf2f..8fafb03bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Note: For changes to the API, see https://shopify.dev/changelog?filter=api ## Unreleased +- Keep REST fields named `errors` separate from internal HTTP error tracking. ## 16.3.0 (2026-08-04) - [#1443](https://github.com/Shopify/shopify-api-ruby/pull/1443) Add `ShopifyAPI::Utils::ShopValidator` with `sanitize_shop_domain` and `sanitize!`. diff --git a/lib/shopify_api/rest/base.rb b/lib/shopify_api/rest/base.rb index 3cc4ebd18..78d269fc1 100644 --- a/lib/shopify_api/rest/base.rb +++ b/lib/shopify_api/rest/base.rb @@ -24,7 +24,9 @@ class Base attr_accessor :original_state sig { returns(T.any(Rest::BaseErrors, T.nilable(T::Hash[T.untyped, T.untyped]))) } - attr_reader :errors + def errors + @base_errors + end sig do params( @@ -44,7 +46,7 @@ def initialize(session: nil, from_hash: nil) @session = T.let(T.must(session), Auth::Session) @client = T.let(client, Clients::Rest::Admin) - @errors = T.let(Rest::BaseErrors.new, Rest::BaseErrors) + @base_errors = T.let(Rest::BaseErrors.new, Rest::BaseErrors) from_hash&.each do |key, value| set_property(key, value) @@ -337,6 +339,7 @@ def to_hash(saving = false) :"@client", :"@forced_nils", :"@errors", + :"@base_errors", :"@aliased_properties", ].include?(var) next if saving && self.class.read_only_attributes&.include?(var) @@ -376,7 +379,7 @@ def delete(params: {}) headers: self.class.headers, ) rescue ShopifyAPI::Errors::HttpResponseError => e - @errors.errors << e + @base_errors.errors << e raise end @@ -405,7 +408,7 @@ def save(update_object: false) self.class.create_instance(data: response.body[response_name.first], session: @session, instance: self) end rescue ShopifyAPI::Errors::HttpResponseError => e - @errors.errors << e + @base_errors.errors << e raise end diff --git a/test/rest/base_errors_test.rb b/test/rest/base_errors_test.rb index c7cc75006..6e4cca6b9 100644 --- a/test/rest/base_errors_test.rb +++ b/test/rest/base_errors_test.rb @@ -3,7 +3,30 @@ require_relative "../test_helper" module ShopifyAPITest - class BaseErrorsTest < Minitest::Test + class BaseErrorsTest < Test::Unit::TestCase + def test_discount_code_errors_attribute_does_not_replace_base_errors + session = ShopifyAPI::Auth::Session.new(id: "id", shop: "test-shop.myshopify.io", access_token: "token") + ShopifyAPI::Context.activate_session(session) + stub_request(:post, "https://test-shop.myshopify.io/admin/api/2023-10/price_rules/507328175/discount_codes.json") + .to_return(status: 500, body: { errors: "Something went wrong" }.to_json, headers: {}) + + discount_code = ShopifyAPI::DiscountCode.new + discount_code.price_rule_id = 507328175 + discount_code.code = "SUMMERSALE10OFF" + + error = assert_raises(ShopifyAPI::Errors::HttpResponseError) { discount_code.save! } + + assert_nil(discount_code.errors) + assert_equal([error], discount_code.instance_variable_get(:@base_errors).errors) + ensure + ShopifyAPI::Context.deactivate_session + end + + def test_resource_errors_returns_base_errors + session = ShopifyAPI::Auth::Session.new(id: "id", shop: "test-shop.myshopify.io", access_token: "token") + + assert_instance_of(ShopifyAPI::Rest::BaseErrors, ShopifyAPI::Fulfillment.new(session: session).errors) + end def test_outputs_error_message errors = ShopifyAPI::Rest::BaseErrors.new