Pass SyntaxErrors to error handler too. [Fixes #774]
This commit is contained in:
parent
18e3877651
commit
d3d29c4ac6
22
lib/pry.rb
22
lib/pry.rb
|
@ -66,8 +66,12 @@ class Pry
|
|||
|
||||
# Will only show the first line of the backtrace
|
||||
DEFAULT_EXCEPTION_HANDLER = proc do |output, exception, _|
|
||||
output.puts "#{exception.class}: #{exception.message}"
|
||||
output.puts "from #{exception.backtrace.first}"
|
||||
if UserError === exception && SyntaxError === exception
|
||||
output.puts "SyntaxError: #{exception.message.sub(/.*syntax error, */m, '')}"
|
||||
else
|
||||
output.puts "#{exception.class}: #{exception.message}"
|
||||
output.puts "from #{exception.backtrace.first}"
|
||||
end
|
||||
end
|
||||
|
||||
DEFAULT_PROMPT_NAME = 'pry'
|
||||
|
@ -155,6 +159,20 @@ class Pry
|
|||
end
|
||||
end
|
||||
|
||||
# An Exception Tag (cf. Exceptional Ruby) that instructs Pry to show the error in
|
||||
# a more user-friendly manner. This should be used when the exception happens within
|
||||
# Pry itself as a direct consequence of the user typing something wrong.
|
||||
#
|
||||
# This allows us to distinguish between the user typing:
|
||||
#
|
||||
# pry(main)> def )
|
||||
# SyntaxError: unexpected )
|
||||
#
|
||||
# pry(main)> method_that_evals("def )")
|
||||
# SyntaxError: (eval):1: syntax error, unexpected ')'
|
||||
# from ./a.rb:2 in `eval'
|
||||
module UserError; end
|
||||
|
||||
# Catches SecurityErrors if $SAFE is set
|
||||
module TooSafeException
|
||||
def self.===(exception)
|
||||
|
|
|
@ -306,7 +306,7 @@ class Pry
|
|||
begin
|
||||
break if Pry::Code.complete_expression?(eval_string)
|
||||
rescue SyntaxError => e
|
||||
output.puts "SyntaxError: #{e.message.sub(/.*syntax error, */m, '')}"
|
||||
exception_handler.call(output, e.extend(UserError), self)
|
||||
eval_string = ""
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue