Shopify / Shopify/shopify-api-ruby
DiscountCode REST resource can't handle error responses
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 1.1k
- Forks
- 484
- PR merge metrics
- No merged PRs in 30d
Description
Issue summary
If Shopify responds with a 4xx or 5xx status code while creating a discount code via the REST API, then a NoMethodError is raised.
shopify_apiversion: 13.3.1- Ruby version: 3.2.2
- Operating system: Ubuntu 22.04 LTS
Expected behavior
- The
DiscountCoderesource should not set@errorstonilon initialization. It should leave it set from the base class as an instance ofRest::BaseErrors - When a
ShopifyAPI::Errors::HttpResponseErroris encountered during a save, the error is appended onto@errors.errorsand the exception is re-raised
Actual behavior
- In the
ShopifyAPI::Rest::Baseclass, an@errorsvariable is initialized - In all versions of the
ShopifyAPI::DiscountCoderesource, the initializer resets@errorsto nil - If a
ShopifyAPI::Errors::HttpResponseErroris encountered while saving, we append the error onto@errors.errors - Since
@errorsisnilon theDiscountCoderesource, we get the following:
NoMethodError: undefined method `errors' for nil:NilClass
Steps to reproduce the problem
Add the following test case to test/rest/2023_10/discount_code_test.rb and run it. It will blow up with the no method error I described:
sig do
void
end
def test_10()
stub_request(:post, "https://test-shop.myshopify.io/admin/api/2023-10/price_rules/507328175/discount_codes.json")
.to_return(status: 500, body: JSON.generate({"errors":[{"message":"There was a problem loading this website. Please try again.","extensions":{"code":"SERVICE_UNAVAILABLE"}}],"error_reference":"If you report this error, please include this id: 9e6928cf-99cd-47e0-a257-b335115453dc."}), headers: {})
response = discount_code = ShopifyAPI::DiscountCode.new
discount_code.price_rule_id = 507328175
discount_code.code = "SUMMERSALE10OFF"
discount_code.save!
end
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with lib/shopify_api/rest/base.rb, the DiscountCode resource initializer under lib/shopify_api/rest/resources/2023_10/discount_code.rb, and test/rest/2023_10/discount_code_test.rb. Run the provided failing 500-response scenario, inspect how save! records HttpResponseError instances, and confirm DiscountCode retains Rest::BaseErrors and re-raises while the test passes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100