Skip to content

Commit 0032287

Browse files
hsbtclaude
andcommitted
Use Exception#initialize hook for lazy loading
Load error enhancer gems on first exception creation by hooking into Exception#initialize. This ensures gems are loaded before detailed_message is called, so their prepended modules are properly in the method chain. After loading, the overhead is a single boolean check per exception creation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 538c28b commit 0032287

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

gem_prelude.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ def self.load
3636
warn "`syntax_suggest' was not loaded."
3737
end if defined?(SyntaxSuggest)
3838
end
39+
end
3940

40-
def detailed_message(...)
41-
return super if Ruby::DetailedError.loaded?
42-
Ruby::DetailedError.load
43-
# Re-dispatch to pick up the newly prepended methods from the gems
44-
detailed_message(...)
41+
# Load error enhancer gems on first exception creation.
42+
# By hooking into initialize, gems are loaded before
43+
# detailed_message is called, so their prepended modules
44+
# are properly in the method chain.
45+
module Ruby::DetailedError::Hook # :nodoc:
46+
def initialize(...)
47+
Ruby::DetailedError.load unless Ruby::DetailedError.loaded?
48+
super
4549
end
4650
end
4751

48-
Exception.prepend(Ruby::DetailedError)
52+
Exception.prepend(Ruby::DetailedError::Hook)
4953
end

0 commit comments

Comments
 (0)