Class: ExceptionNotifier::SlackNotifier
- Inherits:
-
BaseNotifier
- Object
- BaseNotifier
- ExceptionNotifier::SlackNotifier
- Includes:
- BacktraceCleaner
- Defined in:
- lib/exception_notifier/slack_notifier.rb
Instance Attribute Summary collapse
-
#notifier ⇒ Object
Returns the value of attribute notifier.
Attributes inherited from BaseNotifier
Instance Method Summary collapse
- #call(exception, options = {}) ⇒ Object
-
#initialize(options) ⇒ SlackNotifier
constructor
A new instance of SlackNotifier.
Methods included from BacktraceCleaner
Methods inherited from BaseNotifier
#_post_callback, #_pre_callback, #send_notice
Constructor Details
#initialize(options) ⇒ SlackNotifier
Returns a new instance of SlackNotifier.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/exception_notifier/slack_notifier.rb', line 7 def initialize() super begin @ignore_data_if = [:ignore_data_if] webhook_url = .fetch(:webhook_url) = .fetch(:additional_parameters, {}) @notifier = Slack::Notifier.new webhook_url, rescue @notifier = nil end end |
Instance Attribute Details
#notifier ⇒ Object
Returns the value of attribute notifier.
5 6 7 |
# File 'lib/exception_notifier/slack_notifier.rb', line 5 def notifier @notifier end |
Instance Method Details
#call(exception, options = {}) ⇒ Object
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 |
# File 'lib/exception_notifier/slack_notifier.rb', line 20 def call(exception, ={}) env = [:env] || {} title = "#{env['REQUEST_METHOD']} <#{env['REQUEST_URI']}>" data = (env['exception_notifier.exception_data'] || {}).merge([:data] || {}) text = "*An exception occurred while doing*: `#{title}`\n" = exception..gsub("`", "'") fields = [ { title: 'Exception', value: } ] fields.push({ title: 'Hostname', value: Socket.gethostname }) if exception.backtrace formatted_backtrace = "```#{exception.backtrace.join("\n")}```" fields.push({ title: 'Backtrace', value: formatted_backtrace }) end unless data.empty? deep_reject(data, @ignore_data_if) if @ignore_data_if.is_a?(Proc) data_string = data.map{|k,v| "#{k}: #{v}"}.join("\n") fields.push({ title: 'Data', value: "```#{data_string}```" }) end fields.push({title: 'Request Params'}, value: [:env]['action_dispatch.request.parameters'].to_s) fields.push({title: 'User Agent'}, value: [:env]['HTTP_USER_AGENT']) fields.push({title: 'Remote IP'}, value: [:env]['REMOTE_ADDR']) attchs = [color: 'danger', text: text, fields: fields, mrkdwn_in: %w(text fields)] if valid? send_notice(exception, , , .merge(attachments: attchs)) do |msg, | @notifier.ping '', end end end |