summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyrylo Silin <silin@kyrylo.org>2019-05-11 15:27:03 +0300
committerKyrylo Silin <silin@kyrylo.org>2019-05-11 16:32:43 +0300
commit4924cecd8171b86cd9bd5062a7a73f17daa7734d (patch)
treeabe5a6303a4420299b1da5c2a0125e5d6b00de28
parentaa173f41146bdc97c33fd0dc4a823c9a8b6a3ea4 (diff)
downloadpry-4924cecd8171b86cd9bd5062a7a73f17daa7734d.tar.gz
commands/wtf: refactor to avoid duplication
-rw-r--r--lib/pry/commands/wtf.rb53
1 files changed, 27 insertions, 26 deletions
diff --git a/lib/pry/commands/wtf.rb b/lib/pry/commands/wtf.rb
index 3e25dca3..a14d3976 100644
--- a/lib/pry/commands/wtf.rb
+++ b/lib/pry/commands/wtf.rb
@@ -27,45 +27,46 @@ class Pry
end
def process
- raise Pry::CommandError, "No most-recent exception" unless exception
-
- output.puts "#{bold('Exception:')} #{exception.class}: #{exception}\n--"
- if opts.verbose?
- output.puts with_line_numbers(backtrace)
- else
- output.puts with_line_numbers(backtrace.first(size_of_backtrace))
+ unless pry_instance.last_exception
+ raise Pry::CommandError, "No most-recent exception"
end
- return unless exception.respond_to?(:cause)
-
- cause = exception.cause
- while cause
- output.puts "#{bold('Caused by:')} #{cause.class}: #{cause}\n--"
- if opts.verbose?
- output.puts with_line_numbers(cause.backtrace)
- else
- output.puts with_line_numbers(cause.backtrace.first(size_of_backtrace))
- end
- cause = cause.cause
+ text = ''.dup
+ unwind_exceptions.each_with_index do |exception, i|
+ title = (i == 0 ? 'Exception' : 'Caused by')
+ text << format_header(title, exception)
+ text << format_backtrace(exception.backtrace)
end
+ output.puts(text)
end
private
- def exception
- pry_instance.last_exception
+ def unwind_exceptions
+ exception_list = []
+ exception = pry_instance.last_exception
+
+ while exception
+ exception_list << exception
+ exception = (exception.cause if exception.respond_to?(:cause))
+ end
+
+ exception_list
end
- def with_line_numbers(backtrace)
- Pry::Code.new(backtrace, 0, :text).with_line_numbers.to_s
+ def format_header(title, exception)
+ "#{bold(title + ':')} #{exception.class}: #{exception}\n--\n"
end
- def backtrace
- exception.backtrace
+ def format_backtrace(backtrace)
+ return with_line_numbers(backtrace) if opts.verbose?
+
+ size_of_backtrace = [captures[0].size, 0.5].max * 10
+ with_line_numbers(backtrace.first(size_of_backtrace))
end
- def size_of_backtrace
- [captures[0].size, 0.5].max * 10
+ def with_line_numbers(backtrace)
+ Pry::Code.new(backtrace, 0, :text).with_line_numbers.to_s
end
end