-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollbar_inside_rescue.rb
68 lines (60 loc) · 2.01 KB
/
rollbar_inside_rescue.rb
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
# frozen_string_literal: true
module RuboCop
module Cop
module Vendor
# This cop checks for Rollbar calls outside `rescue` blocks.
#
# The main reason for this suggestion is that Rollbar should not be used
# as a logger given it has a quota that is often multiple times smaller
# than the log quota. By reporting errors outside rescue blocks
# the developer is most likely in control of the exceptional flow and
# won't need a stack trace.
#
# @example
# # bad
# Rollbar.error("Unable to sync account")
#
# # good
# begin
# 1 / 0
# rescue StandardError => exception
# Rollbar.error(exception, "Unable to sync account")
# end
#
# # good
# class ApplicationController < ActionController::Base
# rescue_from InvalidRecord do |e|
# Rollbar.error(e)
# end
# end
#
class RollbarInsideRescue < Base
MSG = 'Only call Rollbar when handling errors inside a `rescue` block.'
# @!method rollbar?(node)
def_node_matcher :rollbar?, <<-PATTERN
(send
(const nil? :Rollbar) {:log :debug :info :warning :error :critical} ...)
PATTERN
# @!method active_support_rescuable_block?(node)
def_node_matcher :active_support_rescuable_block?, <<-PATTERN
(block
(send nil? :rescue_from ...) ...)
PATTERN
def on_send(node)
return unless rollbar?(node)
return if in_rescue_block?(node)
add_offense(node.children[0].loc.expression)
end
def in_rescue_block?(node)
current_node = node
while (current_node = current_node.parent)
return true if current_node.rescue_type?
return true if active_support_rescuable_block?(current_node)
break if current_node.def_type?
break if current_node.class_type?
end
end
end
end
end
end