Created
May 25, 2012 15:19
-
-
Save mfojtik/2788730 to your computer and use it in GitHub Desktop.
This piece of code will report the chunk of code that caused exception
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StandardError | |
alias :original_backtrace :backtrace | |
def backtrace | |
unless original_backtrace.nil? | |
file, number = original_backtrace.first.split(':') | |
$stdout.print "\n# %s\n\n" % file | |
__get_script_line(file, number.to_i) do |line| | |
$stdout.print line | |
end | |
$stdout.print "\n" | |
end | |
original_backtrace | |
end | |
private | |
def __get_script_line(file, line) | |
__SCRIPT_LINES = open(file) { |f| f.readlines } | |
3.downto(-1) do |i| | |
yield __print_line(line-i, __SCRIPT_LINES[line-i], (i==1)) | |
end | |
end | |
def __print_line(number, line, color=false) | |
if color | |
sprintf("\033[1;37m>%i: %s\033[0m", number, line) | |
else | |
sprintf(" %i: %s", number, line) | |
end | |
end | |
end | |
# | |
# | |
# def test1 | |
# b += 1 | |
# end | |
# | |
# $ ruby test.rb | |
# | |
# test.rb | |
# | |
# 37: def test1 | |
# >38: b += 1 | |
# 39: end | |
# 40: | |
# | |
# test.rb:38:in `test1': undefined method `+' for nil:NilClass (NoMethodError) | |
# from test.rb:37 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment