Skip to content

Commit e60dd82

Browse files
committed
Don't output the whole Rails::Railtie object
- ### Summary The terminal output on a Rails application running ruby 3 will be cluttered with thousands of lines if one inadventarly call a unexisting method. This happen in various places (IntegrationTest, when running a db migration ...). This is related to a change in ruby 3 when a NoMethodError is raised. ### Simple reproduction ``` class A def initialize(session) @A = session end end test = A.new("*" * 36) test.dsad # undefined method `dsad' for #<A:0x00007f847d8494b0 @A="************************************"> (NoMethodError) # Note that the "#<A:0x00007f847d8494b0 @A="************************************">" part # is 65 chars long. test = test = A.new("*" * 37) test.dsad # undefined method `dsad' for #<A:0x00007fa8c38299c0> (NoMethodError) ``` On Ruby < 3, the NoMethodError message (everything starting from the "#" char) could only be 65 characters long. If it was above that ruby would only output the name of the class and its address. On Ruby >= 3, that limitation has been removed and the message can be any length long. ### On Rails Anytime a method is called on a object that holds the entire Rails::Application, the terminal would output the entire application which is annoying be can be dangerous because it will leak everything containing the credentials (stored inside the Application object).
1 parent 6719f02 commit e60dd82

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

railties/lib/rails/railtie.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,10 @@ def initialize # :nodoc:
247247
end
248248
end
249249

250+
def inspect # :nodoc:
251+
"#<#{self.class.name}>"
252+
end
253+
250254
def configure(&block) # :nodoc:
251255
instance_eval(&block)
252256
end

railties/test/railties/railtie_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,18 @@ class MyTie < Rails::Railtie
224224
Rails.env = original_env
225225
assert_equal(original_env, Rails.env)
226226
end
227+
228+
test "Railtie object isn't output when a NoMethodError is raised" do
229+
class Foo < Rails::Railtie
230+
config.foo = ActiveSupport::OrderedOptions.new
231+
config.foo.greetings = "hello"
232+
end
233+
234+
error = assert_raises(NoMethodError) do
235+
Foo.instance.abc
236+
end
237+
238+
assert_equal("undefined method `abc' for #<RailtiesTest::RailtieTest::Foo>", error.original_message)
239+
end
227240
end
228241
end

0 commit comments

Comments
 (0)