-
-
Save cachrisman/2b72087810869e71b502080d2a2bcf30 to your computer and use it in GitHub Desktop.
Print a prettier stack trace for Ruby exceptions
This file contains 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
def colorize(text, color_code) | |
"\e[#{color_code}m#{text}\e[0m" | |
end | |
begin | |
# Here be dangerous things. | |
rescue => e | |
print "\r" << (' ' * 50) << "\n" | |
stacktrace = e.backtrace.map do |call| | |
if parts = call.match(/^(?<file>.+):(?<line>\d+):in `(?<code>.*)'$/) | |
file = parts[:file].sub /^#{Regexp.escape(File.join(Dir.getwd, ''))}/, '' | |
line = "#{colorize(file, 36)} #{colorize('(', 37)}#{colorize(parts[:line], 32)}#{colorize('): ', 37)} #{colorize(parts[:code], 31)}" | |
else | |
line = colorize(call, 31) | |
end | |
line | |
end | |
puts "Fatal error:\n" | |
stacktrace.each { |line| puts line } | |
raise "Failed" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment