Skip to content

Commit ed219e7

Browse files
hsbtclaude
andcommitted
Use Exception#detailed_message hook for lazy loading error enhancer gems
Prepend an Exception::DetailedMessage module that loads error_highlight, did_you_mean, and syntax_suggest on first detailed_message call, then removes itself from the method chain to avoid double dispatch. Guard with defined?(Ruby::DetailedError) to handle cases where the constant may not be available in the current execution context. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 538c28b commit ed219e7

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

41+
module Exception::DetailedMessage # :nodoc:
4042
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(...)
43+
if defined?(Ruby::DetailedError)
44+
Ruby::DetailedError.load
45+
Exception::DetailedMessage.remove_method(:detailed_message)
46+
detailed_message(...)
47+
else
48+
super
49+
end
4550
end
4651
end
47-
48-
Exception.prepend(Ruby::DetailedError)
52+
Exception.prepend(Exception::DetailedMessage)
4953
end

0 commit comments

Comments
 (0)