Class: RuboCop::Cop::Lint::SwallowException

Inherits:
RuboCop::Cop show all
Defined in:
lib/rubocop/swallow_exception/cop/lint/swallow_exception.rb

Instance Method Summary collapse

Instance Method Details

#has_raise?(node) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
# File 'lib/rubocop/swallow_exception/cop/lint/swallow_exception.rb', line 33

def has_raise?(node)
  case node.type
    when :send
      raise?(node)
    when :begin
      node.children.any? { |c| raise?(c) }
  end
end

#has_raven_capture_exception?(node) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
# File 'lib/rubocop/swallow_exception/cop/lint/swallow_exception.rb', line 24

def has_raven_capture_exception?(node)
  case node.type
    when :send
      raven_capture_exception?(node)
    when :begin
      node.children.any? { |c| raven_capture_exception?(c) }
  end
end

#on_resbody(node) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubocop/swallow_exception/cop/lint/swallow_exception.rb', line 7

def on_resbody(node)
  # rescue の中身が空ならエラー
  unless node.children[2]
    add_offense(node, :expression, 'rescue body is empty!', :fatal)
    return
  end
  body = node.children[2]
  # トップレベルで条件なしに raise していれば OK
  return if has_raise?(body)
  # トップレベルで Raven.capture_exception 呼び出していれば OK
  return if has_raven_capture_exception?(body)
  # raise も Raven.capture_exception もなければエラー
  add_offense(node, :expression, ("    you have to raise exception or capture exception by Raven in rescue body.\n  MSG\nend\n").strip, :fatal)

#raise?(node) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/rubocop/swallow_exception/cop/lint/swallow_exception.rb', line 52

def raise?(node)
  node.type == :send &&
      node.children[0] == nil &&
      node.children[1] == :raise
end

#raven?(node) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/rubocop/swallow_exception/cop/lint/swallow_exception.rb', line 48

def raven?(node)
  node.type == :const && node.children[1] == :Raven
end

#raven_capture_exception?(node) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/rubocop/swallow_exception/cop/lint/swallow_exception.rb', line 42

def raven_capture_exception?(node)
  node.type == :send &&
      raven?(node.children[0]) &&
      node.children[1] == :capture_exception
end