Module: WorkerTools::SlackErrorNotifier
- Defined in:
- lib/worker_tools/slack_error_notifier.rb
Instance Method Summary collapse
- #slack_error_notifier ⇒ Object
- #slack_error_notifier_attachments(error) ⇒ Object
- #slack_error_notifier_attachments_color ⇒ Object
- #slack_error_notifier_attachments_fields ⇒ Object
- #slack_error_notifier_channel ⇒ Object
- #slack_error_notifier_emoji ⇒ Object
- #slack_error_notifier_enabled ⇒ Object
- #slack_error_notifier_error_details(error) ⇒ Object
- #slack_error_notifier_message ⇒ Object
- #slack_error_notifier_receivers ⇒ Object
- #slack_error_notifier_title ⇒ Object
- #slack_error_notifier_username ⇒ Object
- #slack_error_notifier_webhook ⇒ Object
- #slack_error_notify(error) ⇒ Object
- #with_wrapper_slack_error_notifier(&block) ⇒ Object
Instance Method Details
#slack_error_notifier ⇒ Object
91 92 93 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 91 def slack_error_notifier Slack::Notifier.new(slack_error_notifier_webhook) end |
#slack_error_notifier_attachments(error) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 73 def (error) [ { color: , fields: }, { title: [error.class, error.].join(' : '), color: , text: slack_error_notifier_error_details(error) } ] end |
#slack_error_notifier_attachments_color ⇒ Object
40 41 42 43 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 40 def # good, warning, danger, hex color 'danger' end |
#slack_error_notifier_attachments_fields ⇒ Object
84 85 86 87 88 89 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 84 def [ { title: 'Application', value: Rails.application.class.parent_name, short: true }, { title: 'Environment', value: Rails.env, short: true } ] end |
#slack_error_notifier_channel ⇒ Object
20 21 22 23 24 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 20 def slack_error_notifier_channel return SLACK_NOTIFIER_CHANNEL if defined?(SLACK_NOTIFIER_CHANNEL) raise 'Define slack_error_notifier_channel or set SLACK_NOTIFIER_CHANNEL in an initializer' end |
#slack_error_notifier_emoji ⇒ Object
16 17 18 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 16 def slack_error_notifier_emoji ':red_circle:' end |
#slack_error_notifier_enabled ⇒ Object
12 13 14 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 12 def slack_error_notifier_enabled Rails.env.production? end |
#slack_error_notifier_error_details(error) ⇒ Object
62 63 64 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 62 def slack_error_notifier_error_details(error) error.backtrace[0..2].join("\n") end |
#slack_error_notifier_message ⇒ Object
66 67 68 69 70 71 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 66 def = [] << slack_error_notifier_receivers << slack_error_notifier_title .compact.join(' - ') end |
#slack_error_notifier_receivers ⇒ Object
36 37 38 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 36 def slack_error_notifier_receivers # Ex: '@all' end |
#slack_error_notifier_title ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 45 def slack_error_notifier_title # Example with a link: # # For urls a default_url_options[:host] might be necessary. # In this example I just copy it from existing action_mailer defaults. # # import = slack_error_notifier_model # host = Rails.application.config.action_mailer.default_url_options[:host] # url = Rails.application.routes.url_helpers.import_url(import, host: host, protocol: :https) # kind = I18n.t(import.kind, scope: 'import.kinds') # text = "##{import.id} *#{kind}*" # "[#{text}](#{url})" klass = model.class.model_name.human kind = I18n.t("activerecord.attributes.#{model.class.name.underscore}.kinds.#{model.kind}") "#{klass} #{kind} ##{model.id}" end |
#slack_error_notifier_username ⇒ Object
32 33 34 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 32 def slack_error_notifier_username 'Notifier' end |
#slack_error_notifier_webhook ⇒ Object
26 27 28 29 30 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 26 def slack_error_notifier_webhook return SLACK_NOTIFIER_WEBHOOK if defined?(SLACK_NOTIFIER_WEBHOOK) raise 'Define slack_error_notifier_webhook or set SLACK_NOTIFIER_WEBHOOK in an initializer' end |
#slack_error_notify(error) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 95 def slack_error_notify(error) slack_error_notifier.post( username: slack_error_notifier_username, channel: slack_error_notifier_channel, icon_emoji: slack_error_notifier_emoji, text: "*#{slack_error_notifier_message}*", attachments: (error) ) end |
#with_wrapper_slack_error_notifier(&block) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 5 def with_wrapper_slack_error_notifier(&block) block.yield rescue StandardError => e slack_error_notify(e) if slack_error_notifier_enabled raise end |