2019-05-02 18:33:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-09-03 04:04:25 -04:00
|
|
|
class Pry
|
|
|
|
# As a REPL, we often want to catch any unexpected exceptions that may have
|
|
|
|
# been raised; however we don't want to go overboard and prevent the user
|
|
|
|
# from exiting Pry when they want to.
|
|
|
|
module RescuableException
|
|
|
|
def self.===(exception)
|
|
|
|
case exception
|
|
|
|
# Catch when the user hits ^C (Interrupt < SignalException), and assume
|
|
|
|
# that they just wanted to stop the in-progress command (just like bash
|
|
|
|
# etc.)
|
|
|
|
when Interrupt
|
|
|
|
true
|
|
|
|
# Don't catch signals (particularly not SIGTERM) as these are unlikely
|
|
|
|
# to be intended for pry itself. We should also make sure that
|
|
|
|
# Kernel#exit works.
|
2018-11-10 11:34:09 -05:00
|
|
|
when *Pry.config.unrescued_exceptions
|
2013-09-03 04:04:25 -04:00
|
|
|
false
|
|
|
|
# All other exceptions will be caught.
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Catches SecurityErrors if $SAFE is set
|
2019-02-26 19:22:32 -05:00
|
|
|
module TooSafeException
|
2013-09-03 04:04:25 -04:00
|
|
|
def self.===(exception)
|
2019-03-24 08:30:00 -04:00
|
|
|
$SAFE > 0 && exception.is_a?(SecurityError)
|
2013-09-03 04:04:25 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
# When we try to get a binding for an object, we try to define a method on
|
|
|
|
# that Object's singleton class. This doesn't work for "frozen" Object's, and
|
|
|
|
# the exception is just a vanilla RuntimeError.
|
|
|
|
module FrozenObjectException
|
|
|
|
def self.===(exception)
|
2019-02-24 17:54:31 -05:00
|
|
|
[
|
|
|
|
"can't modify frozen class/module",
|
|
|
|
"can't modify frozen Class",
|
|
|
|
"can't modify frozen object"
|
2013-09-03 04:04:25 -04:00
|
|
|
].include?(exception.message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# CommandErrors are caught by the REPL loop and displayed to the user. They
|
|
|
|
# indicate an exceptional condition that's fatal to the current command.
|
|
|
|
class CommandError < StandardError; end
|
|
|
|
class MethodNotFound < CommandError; end
|
|
|
|
|
|
|
|
# indicates obsolete API
|
|
|
|
class ObsoleteError < StandardError; end
|
|
|
|
end
|