Skip to content

Instantly share code, notes, and snippets.

@mikepfirrmann
Created October 2, 2012 16:24
Show Gist options
  • Save mikepfirrmann/3820663 to your computer and use it in GitHub Desktop.
Save mikepfirrmann/3820663 to your computer and use it in GitHub Desktop.
Print a prettier stack trace for Ruby exceptions
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