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

Avoid overly-general rescue in callback controller #1530

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Unreleased
----------
* Fixes a bug with `EnsureAuthenticatedLinks` causing deep links to not work [#1549](https://github.com/Shopify/shopify_app/pull/1549)
* Ensure online token is properly used when using `current_shopify_session` [#1566](https://github.com/Shopify/shopify_app/pull/1566)
* Emit a deprecation notice for wrongly-rescued exceptions [#1530](https://github.com/Shopify/shopify_app/pull/1530)
* Log a deprecation warning for the use of incompatible controller concerns [#1560](https://github.com/Shopify/shopify_app/pull/1560)

21.2.0 (Oct 25, 2022)
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/shopify_app/callback_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ def callback
},
auth_query: ShopifyAPI::Auth::Oauth::AuthQuery.new(**filtered_params),
)
rescue
rescue => e
unless e.class.module_parent == ShopifyAPI::Errors
ActiveSupport::Deprecation.warn(<<~EOS)
An error of type #{e.class} was rescued. This is not part of `ShopifyAPI::Errors`, which could indicate a
bug in your app, or a bug in the shopify_app gem. Future versions of the gem may re-raise this error rather
than rescuing it.
EOS
end
return respond_with_error
end

Expand Down
24 changes: 22 additions & 2 deletions test/controllers/callback_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,36 @@ class CallbackControllerTest < ActionController::TestCase
end

test "#callback flashes error when omniauth is not present" do
get :callback, params: { shop: "shop" }
Copy link
Contributor Author

@andyw8 andyw8 Oct 11, 2022

Choose a reason for hiding this comment

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

The rescue meant these tests were passing, but for the wrong reason. Without the rescue, they failed with:

Error:
ShopifyApp::CallbackControllerTest#test_#callback_flashes_error_in_Spanish:
ArgumentError: missing keywords: :code, :timestamp, :state, :host, :hmac
    test/controllers/callback_controller_test.rb:44:in `block in <class:CallbackControllerTest>'

get :callback,
params: { shop: "shop", code: "code", state: "state", timestamp: "timestamp", host: "host", hmac: "hmac" }
assert_equal flash[:error], "Could not log in to Shopify store"
end

test "#callback flashes error in Spanish" do
I18n.locale = :es
get :callback, params: { shop: "shop" }
get :callback,
params: { shop: "shop", code: "code", state: "state", timestamp: "timestamp", host: "host", hmac: "hmac" }
assert_match "sesión", flash[:error]
end

test "#callback rescued errors of ShopifyAPI::Error will not emit a deprecation notice" do
ShopifyAPI::Auth::Oauth.expects(:validate_auth_callback).raises(ShopifyAPI::Errors::MissingRequiredArgumentError)
assert_not_deprecated do
get :callback,
params: { shop: "shop", code: "code", state: "state", timestamp: "timestamp", host: "host", hmac: "hmac" }
end
assert_equal flash[:error], "Could not log in to Shopify store"
end

test "#callback rescued errors other than ShopifyAPI::Error will emit a deprecation notice" do
ShopifyAPI::Auth::Oauth.expects(:validate_auth_callback).raises(StandardError)
assert_deprecated(/An error of type StandardError was rescued/) do
get :callback,
params: { shop: "shop", code: "code", state: "state", timestamp: "timestamp", host: "host", hmac: "hmac" }
end
assert_equal flash[:error], "Could not log in to Shopify store"
end

test "#callback calls ShopifyAPI::Auth::Oauth.validate_auth_callback" do
mock_oauth

Expand Down