From 95d3a74e9b22c44ea48ce870171640f0471db09b Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sun, 14 Apr 2019 19:14:16 +0300 Subject: config: factor out exception_handler hook to a separate class --- lib/pry/exception_handler.rb | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 lib/pry/exception_handler.rb (limited to 'lib/pry/exception_handler.rb') diff --git a/lib/pry/exception_handler.rb b/lib/pry/exception_handler.rb new file mode 100644 index 00000000..9b50fe80 --- /dev/null +++ b/lib/pry/exception_handler.rb @@ -0,0 +1,41 @@ +class Pry + # @api private + # @since ?.?.? + module ExceptionHandler + class << self + # Will only show the first line of the backtrace. + def handle_exception(output, exception, _pry_instance) + if exception.is_a?(UserError) && exception.is_a?(SyntaxError) + output.puts "SyntaxError: #{exception.message.sub(/.*syntax error, */m, '')}" + else + output.puts standard_error_text_for(exception) + end + end + + private + + def standard_error_text_for(exception) + text = exception_text(exception) + return text unless exception.respond_to?(:cause) + + cause = exception.cause + while cause + text += cause_text(cause) + cause = cause.cause + end + + text + end + + def exception_text(exception) + "#{exception.class}: #{exception.message}\n" \ + "from #{exception.backtrace.first}\n" + end + + def cause_text(cause) + "Caused by #{cause.class}: #{cause}\n" \ + "from #{cause.backtrace.first}\n" + end + end + end +end -- cgit v1.2.1