Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShopifyAPI::Clients::HttpResponse as argument for ShopifyAPI::Errors::HttpResponseError #1040

Merged
merged 12 commits into from
Dec 2, 2022

Conversation

kaarelss
Copy link
Contributor

ShopifyAPI::Clients::HttpResponse as argument for ShopifyAPI::Errors::HttpResponseError because code can be obtained from response as well as headers for custom retry logic when call limit is exceeded and 429 error occurs.

Description

Fixes #1038

Please, include a summary of what the PR is for:

  • Ability to get headers from response when error occurs. Previously you can get only code from the error.

How has this been tested?

All tests passed as well as new test is included for this new functionality: test_throttle_error_with_retry_after_header_in_error_object in http_client_test.rb

Checklist:

  • My commit message follow the pattern described in here
  • I have performed a self-review of my own code.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have updated the project documentation.
  • I have added a changelog line.

…:HttpResponseError because code can be obtained from response as well as headers for custom retry logic
@kaarelss kaarelss requested a review from a team as a code owner October 14, 2022 06:26
Copy link

@jbourassa jbourassa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't normally maintain this repo but this looks good to me. I'd like to see this merged if you can sign the CLA and fix the linter.

@@ -179,6 +179,18 @@ def test_throttle_error_no_retry_after_header
verify_http_request
end


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Rubocop complains on this line. Care to fix it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

attr_reader :response

sig { params(response: ShopifyAPI::Clients::HttpResponse).void }
def initialize(response:)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This idea makes sense to provide the response instead of just the code!

I do think if we add the required keyword of response we'll need to update one more place to make sure we don't introduce a regression with this change:

MaxHttpRetriesExceededError which inherits from this class. We'll need to update any instantiations of this error to reflect this keyword change otherwise we'll see an ArgumentError anytime we raise that retries error.

@jbourassa
Copy link

Sorbet complains about a missing type annotation, this should fix it:

diff --git lib/shopify_api/errors/http_response_error.rb lib/shopify_api/errors/http_response_error.rb
index 1d536a4..e4d912f 100644
--- lib/shopify_api/errors/http_response_error.rb
+++ lib/shopify_api/errors/http_response_error.rb
@@ -15,7 +15,7 @@ module ShopifyAPI
       sig { params(response: ShopifyAPI::Clients::HttpResponse).void }
       def initialize(response:)
         super
-        @code = response.code.to_i
+        @code = T.let(response.code, Integer)
         @response = response
       end
     end

You can run run the Sorbet locally with bundle exec srb tc.

Make sure to sign the CLA too: https://cla.shopify.com/


raise ShopifyAPI::Errors::MaxHttpRetriesExceededError.new(code: response.code),
raise ShopifyAPI::Errors::MaxHttpRetriesExceededError.new(response: response),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This idea makes sense to provide the response instead of just the code!

I do think if we add the required keyword of response we'll need to update one more place to make sure we don't introduce a regression with this change:

MaxHttpRetriesExceededError which inherits from this class. We'll need to update any instantiations of this error to reflect this keyword change otherwise we'll see an ArgumentError anytime we raise that retries error.

Hi @nelsonwittwer. Thank you for your comment. Do you mean this place?

Copy link
Contributor Author

@kaarelss kaarelss Nov 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added assertion for that - 3312bc0

@kaarelss
Copy link
Contributor Author

kaarelss commented Nov 3, 2022

Sorbet complains about a missing type annotation, this should fix it:

diff --git lib/shopify_api/errors/http_response_error.rb lib/shopify_api/errors/http_response_error.rb
index 1d536a4..e4d912f 100644
--- lib/shopify_api/errors/http_response_error.rb
+++ lib/shopify_api/errors/http_response_error.rb
@@ -15,7 +15,7 @@ module ShopifyAPI
       sig { params(response: ShopifyAPI::Clients::HttpResponse).void }
       def initialize(response:)
         super
-        @code = response.code.to_i
+        @code = T.let(response.code, Integer)
         @response = response
       end
     end

You can run run the Sorbet locally with bundle exec srb tc.

Make sure to sign the CLA too: https://cla.shopify.com/

Thank you @jbourassa for mentioning this, fixed it. We do not have sorbet in our ruby on rails project, so I'm new to this. About the CLA, my employer has asked Shopify to add additional authorized contributor to the existing Corporate CLA. We are still waiting for a reply from Shopify.

@kaarelss kaarelss force-pushed the response_in_error_object branch from 3312bc0 to 533ec79 Compare November 10, 2022 07:27
@kaarelss
Copy link
Contributor Author

HI @jbourassa and @nelsonwittwer. I have CLA signed now. What are the next steps?

@teddyhwang
Copy link
Collaborator

HI @jbourassa and @nelsonwittwer. I have CLA signed now. What are the next steps?

@kaarelss can you rebase your branch with main? that should also get your tests to pass and we'll get this merged in

@kaarelss kaarelss force-pushed the response_in_error_object branch from 533ec79 to c83bf0d Compare November 28, 2022 08:30
@kaarelss
Copy link
Contributor Author

HI @jbourassa and @nelsonwittwer. I have CLA signed now. What are the next steps?

@kaarelss can you rebase your branch with main? that should also get your tests to pass and we'll get this merged in

Done

@nelsonwittwer
Copy link
Contributor

One last little 👶 step and we can get this merged in! Will you add a quick line in the change log describing your change?

…y-api-ruby into response_in_error_object

* 'response_in_error_object' of github.com:kaarelss/shopify-api-ruby:
  Added assertion that response is present in MaxHttpRetriesExceededError
  Added sorbet assertion
  Removed extra empty line
  ShopifyAPI::Clients::HttpResponse as argument for ShopifyAPI::Errors::HttpResponseError because code can be obtained from response as well as headers for custom retry logic
  Remove mentions of private apps (Shopify#1062)
  Fix ActiveSupport inflector dependency (Shopify#1063)
  Move Session Storage to `shopify_app` (Shopify#1055)
  Remove session argument from REST examples (Shopify#1064)
  Constrain Zeitwerk to 2.6.5 (Shopify#1059)
  Clarify statement about packages (Shopify#1057)
@kaarelss
Copy link
Contributor Author

One last little 👶 step and we can get this merged in! Will you add a quick line in the change log describing your change?

Added

@nelsonwittwer nelsonwittwer merged commit bdcffb6 into Shopify:main Dec 2, 2022
@hypogealgaol
Copy link

hypogealgaol commented Feb 10, 2023

Hi, not sure if this is related but I wanted to try to create my own error doing the following:

response = ShopifyAPI::Clients::HttpResponse.new(code: 401, body: {}, headers: {})
ShopifyAPI::Errors::HttpResponseError.new(response: response)
Traceback (most recent call last):
        3: from (irb):175
        2: from (irb):176:in `rescue in irb_binding'
        1: from (irb):176:in `new'
ArgumentError (missing keyword: :code)

Am I doing something wrong?

@kaarelss
Copy link
Contributor Author

Hi, not sure if this is related but I wanted to try to create my own error doing the following:

response = ShopifyAPI::Clients::HttpResponse.new(code: 401, body: {}, headers: {})
ShopifyAPI::Errors::HttpResponseError.new(response: response)
Traceback (most recent call last):
        3: from (irb):175
        2: from (irb):176:in `rescue in irb_binding'
        1: from (irb):176:in `new'
ArgumentError (missing keyword: :code)

Am I doing something wrong?

Which version of the gem are you using?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ShopifyAPI::Errors::HttpResponseError is missing response object with header "retry-after"
5 participants