diff options
Diffstat (limited to 'lib/pry')
-rw-r--r-- | lib/pry/config.rb | 23 | ||||
-rw-r--r-- | lib/pry/exception_handler.rb | 41 |
2 files changed, 42 insertions, 22 deletions
diff --git a/lib/pry/config.rb b/lib/pry/config.rb index 6b7e4fcf..6fcf9920 100644 --- a/lib/pry/config.rb +++ b/lib/pry/config.rb @@ -7,7 +7,6 @@ class Pry # @return [Pry::Config] # An object who implements the default configuration for all # Pry sessions. - # rubocop:disable Metrics/AbcSize def self.defaults defaults = from_hash( input: Pry.lazy { lazy_readline(defaults) }, @@ -18,26 +17,7 @@ class Pry prompt_safe_contexts: Pry::Prompt::SAFE_CONTEXTS, print: Pry::ColorPrinter.method(:default), quiet: false, - - # Will only show the first line of the backtrace - exception_handler: proc do |output, exception, _| - if exception.is_a?(UserError) && exception.is_a?(SyntaxError) - output.puts "SyntaxError: #{exception.message.sub(/.*syntax error, */m, '')}" - else - output.puts "#{exception.class}: #{exception.message}" - output.puts "from #{exception.backtrace.first}" - - if exception.respond_to? :cause - cause = exception.cause - while cause - output.puts "Caused by #{cause.class}: #{cause}\n" - output.puts "from #{cause.backtrace.first}" - cause = cause.cause - end - end - end - end, - + exception_handler: Pry::ExceptionHandler.method(:handle_exception), unrescued_exceptions: [ ::SystemExit, ::SignalException, Pry::TooSafeException ], @@ -113,7 +93,6 @@ class Pry exec_string: "" ) end - # rubocop:enable Metrics/AbcSize def self.shortcuts Convenience::SHORTCUTS 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 |