Skip to content

Commit

Permalink
Merge pull request #37834 from Shopify/handle-unrelated-name-error-in…
Browse files Browse the repository at this point in the history
…-router

Distinguish missing controller exceptions from unrelated NameError
  • Loading branch information
rafaelfranca committed Dec 4, 2019
2 parents 5bbb2a6 + 54878cd commit 7775952
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Expand Up @@ -74,7 +74,7 @@ PATH
i18n (>= 1.6, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2)
zeitwerk (~> 2.2, >= 2.2.2)
rails (6.1.0.alpha)
actioncable (= 6.1.0.alpha)
actionmailbox (= 6.1.0.alpha)
Expand Down Expand Up @@ -529,7 +529,7 @@ GEM
websocket-extensions (0.1.4)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.2.0)
zeitwerk (2.2.2)

PLATFORMS
java
Expand Down
3 changes: 3 additions & 0 deletions actionpack/lib/action_dispatch.rb
Expand Up @@ -40,6 +40,9 @@ module ActionDispatch
class IllegalStateError < StandardError
end

class MissingController < NameError
end

eager_autoload do
autoload_under "http" do
autoload :ContentSecurityPolicy
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/http/parameters.rb
Expand Up @@ -98,7 +98,7 @@ def set_binary_encoding(params, controller, action)

def binary_params_for?(controller, action)
controller_class_for(controller).binary_params_for?(action)
rescue NameError
rescue MissingController
false
end

Expand Down
10 changes: 9 additions & 1 deletion actionpack/lib/action_dispatch/http/request.rb
Expand Up @@ -86,7 +86,15 @@ def controller_class_for(name)
if name
controller_param = name.underscore
const_name = controller_param.camelize << "Controller"
ActiveSupport::Dependencies.constantize(const_name)
begin
ActiveSupport::Dependencies.constantize(const_name)
rescue NameError => error
if error.missing_name == const_name || const_name.start_with?("#{error.missing_name}::")
raise MissingController.new(error.message, error.name)
else
raise
end
end
else
PASS_NOT_FOUND
end
Expand Down
2 changes: 1 addition & 1 deletion activesupport/activesupport.gemspec
Expand Up @@ -37,5 +37,5 @@ Gem::Specification.new do |s|
s.add_dependency "tzinfo", "~> 1.1"
s.add_dependency "minitest", "~> 5.1"
s.add_dependency "concurrent-ruby", "~> 1.0", ">= 1.0.2"
s.add_dependency "zeitwerk", "~> 2.2"
s.add_dependency "zeitwerk", "~> 2.2", ">= 2.2.2"
end

0 comments on commit 7775952

Please sign in to comment.