Skip to content

Commit 818090e

Browse files
hsbtclaude
andcommitted
Use TracePoint(:raise) for lazy loading error enhancer gems
Load error enhancer gems on first exception raise using TracePoint(:raise). The TracePoint is disabled after first invocation, so there is zero overhead after loading. Rescue ThreadError since require can't be called from trap context (e.g., exit inside a signal handler). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 538c28b commit 818090e

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?
41+
# Load error enhancer gems on first exception raise.
42+
# TracePoint is disabled after first invocation, so there
43+
# is zero overhead after loading.
44+
trace = TracePoint.new(:raise) do
45+
trace.disable
46+
begin
4247
Ruby::DetailedError.load
43-
# Re-dispatch to pick up the newly prepended methods from the gems
44-
detailed_message(...)
48+
rescue ThreadError
49+
# require can't be called from trap context
4550
end
4651
end
47-
48-
Exception.prepend(Ruby::DetailedError)
52+
trace.enable
4953
end

0 commit comments

Comments
 (0)