Class: TgErrorNotifier::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/tg_error_notifier/notifier.rb

Constant Summary collapse

MAX_MESSAGE_LENGTH =
3800
MAX_FIELD_LENGTH =

Отдельные поля (сообщение исключения, значения контекста) режем заранее, чтобы гарантированно осталось место под бэктрейс и закрывающие теги

900
HTML_TAGS =
%w[pre code b i u s].freeze

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Notifier



15
16
17
# File 'lib/tg_error_notifier/notifier.rb', line 15

def initialize(config)
  @config = config
end

Instance Method Details

#notify(exception:, source:, context: {}) ⇒ Object



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
# File 'lib/tg_error_notifier/notifier.rb', line 19

def notify(exception:, source:, context: {})
  enabled_check = enabled_status
  unless enabled_check[:enabled]
    log("skipped: #{enabled_check[:reason]}")
    return { sent: false, status: :skipped, reason: enabled_check[:reason] }
  end

  if ignored_exception?(exception)
    return { sent: false, status: :skipped, reason: "ignored_exception" }
  end

  key = nil
  thread_id = nil
  suppressed_count = 0

  if config.topics_enabled || config.grouping_enabled || config.buttons
    key = grouper.grouping_key(exception)
  end

  if config.topics_enabled
    thread_id = topic_manager.thread_id_for(key, exception)
  end

  if config.grouping_enabled
    result = grouper.process(key: key, thread_id: thread_id)
    if result[:action] == :suppress
      return { sent: false, status: :suppressed }
    end
    suppressed_count = result[:count]
    thread_id = result[:thread_id] || thread_id
  end

  payload = build_payload(
    exception: exception,
    source: source,
    context: context,
    thread_id: thread_id,
    suppressed_count: suppressed_count,
    fingerprint: key
  )
  response = send_payload(payload)

  if !response[:sent] && config.grouping_enabled && key
    grouper.rollback(key)
  end

  response
rescue StandardError => e
  log("notify failed: #{e.class}: #{e.message}")
  { sent: false, status: :failed, reason: e.class.name, error: e.message }
end

#notify_message(message:, level:, source:, context: {}, topic: nil, thread_id: nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tg_error_notifier/notifier.rb', line 71

def notify_message(message:, level:, source:, context: {}, topic: nil, thread_id: nil)
  enabled_check = enabled_status
  unless enabled_check[:enabled]
    log("skipped: #{enabled_check[:reason]}")
    return { sent: false, status: :skipped, reason: enabled_check[:reason] }
  end

  thread_id ||= topic_manager.thread_id_for_name(topic) if topic

  payload = build_message_payload(
    message: message,
    level: level,
    source: source,
    context: context,
    thread_id: thread_id
  )
  send_payload(payload)
rescue StandardError => e
  log("notify_message failed: #{e.class}: #{e.message}")
  { sent: false, status: :failed, reason: e.class.name, error: e.message }
end