Skip to content

Instantly share code, notes, and snippets.

@techiferous
Created March 7, 2015 18:29
Show Gist options
  • Save techiferous/c5409009c78ec011ce79 to your computer and use it in GitHub Desktop.
Save techiferous/c5409009c78ec011ce79 to your computer and use it in GitHub Desktop.
Rewrite Ruby's backtrace to cover your tracks
this_file = Regexp.new(__FILE__)
# Capture every single time an exception is raised in this Ruby program.
# Use TracePoint for Ruby 2.0+, set_trace_func for Ruby 1.9-
trace = TracePoint.new(:raise) do |tp|
exception = tp.raised_exception
backtrace = exception.backtrace
# if the source of the error is in this file
if backtrace.any?{|b| b =~ this_file}
# Remove all lines in the backtrace that match this filename
# as well as lines with the word "require", which could help
# point to this filename. The result is a backtrace which hides
# the source of the error while still appearing like a credible
# backtrace.
filtered_backtrace = backtrace.select do |bt|
bt !~ this_file && bt !~ /require/
end
# This is what is referred to as "lying":
exception.set_backtrace(filtered_backtrace)
end
end
trace.enable
raise "good luck finding me!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment