File: catch.rb

package info (click to toggle)
ruby-byebug 12.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,304 kB
  • sloc: ruby: 9,013; ansic: 1,678; sh: 5; makefile: 4
file content (78 lines) | stat: -rw-r--r-- 1,982 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true

require_relative "../command"
require_relative "../helpers/eval"

module Byebug
  #
  # Implements exception catching.
  #
  # Enables the user to catch unhandled assertion when they happen.
  #
  class CatchCommand < Command
    include Helpers::EvalHelper

    self.allow_in_post_mortem = true

    def self.regexp
      /^\s* cat(?:ch)? (?:\s+(\S+))? (?:\s+(off))? \s*$/x
    end

    def self.description
      <<-DESCRIPTION
        cat[ch][ (off|<exception>[ off])]

        #{short_description}

        catch                 -- lists catchpoints
        catch off             -- deletes all catchpoints
        catch <exception>     -- enables handling <exception>
        catch <exception> off -- disables handling <exception>
      DESCRIPTION
    end

    def self.short_description
      "Handles exception catchpoints"
    end

    def execute
      return info unless @match[1]

      return @match[1] == "off" ? clear : add(@match[1]) unless @match[2]

      return errmsg pr("catch.errors.off", off: cmd) unless @match[2] == "off"

      remove(@match[1])
    end

    private

    def remove(exception)
      return errmsg pr("catch.errors.not_found", exception: exception) unless Byebug.catchpoints.member?(exception)

      puts pr("catch.removed", exception: exception)
      Byebug.catchpoints.delete(exception)
    end

    def add(exception)
      errmsg pr("catch.errors.not_class", class: exception) if warning_eval(exception.is_a?(Class).to_s)

      puts pr("catch.added", exception: exception)
      Byebug.add_catchpoint(exception)
    end

    def clear
      Byebug.catchpoints.clear if confirm(pr("catch.confirmations.delete_all"))
    end

    def info
      if Byebug.catchpoints && !Byebug.catchpoints.empty?
        Byebug.catchpoints.each_key do |exception|
          puts("#{exception}: #{exception.is_a?(Class)}")
        end
      else
        puts "No exceptions set to be caught."
      end
    end
  end
end