Module: WorkerTools::SlackErrorNotifier
- Defined in:
- lib/worker_tools/slack_error_notifier.rb
Instance Method Summary collapse
- #slack_error_notifiable?(error) ⇒ Boolean
- #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_notifiable?(error) ⇒ Boolean
16 17 18 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 16 def slack_error_notifiable?(error) error.is_a?(StandardError) end |
#slack_error_notifier ⇒ Object
95 96 97 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 95 def slack_error_notifier Slack::Notifier.new(slack_error_notifier_webhook) end |
#slack_error_notifier_attachments(error) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 77 def (error) [ { color: , fields: }, { title: [error.class, error.].join(' : '), color: , text: slack_error_notifier_error_details(error) } ] end |
#slack_error_notifier_attachments_color ⇒ Object
44 45 46 47 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 44 def # good, warning, danger, hex color 'danger' end |
#slack_error_notifier_attachments_fields ⇒ Object
88 89 90 91 92 93 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 88 def [ { title: 'Application', value: Rails.application.class.module_parent_name, short: true }, { title: 'Environment', value: Rails.env, short: true } ] end |
#slack_error_notifier_channel ⇒ Object
24 25 26 27 28 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 24 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
20 21 22 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 20 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
66 67 68 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 66 def slack_error_notifier_error_details(error) error.backtrace[0..2].join("\n") end |
#slack_error_notifier_message ⇒ Object
70 71 72 73 74 75 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 70 def = [] << slack_error_notifier_receivers << slack_error_notifier_title .compact.join(' - ') end |
#slack_error_notifier_receivers ⇒ Object
40 41 42 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 40 def slack_error_notifier_receivers # Ex: '@all' end |
#slack_error_notifier_title ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 49 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
36 37 38 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 36 def slack_error_notifier_username 'Notifier' end |
#slack_error_notifier_webhook ⇒ Object
30 31 32 33 34 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 30 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
99 100 101 102 103 104 105 106 107 |
# File 'lib/worker_tools/slack_error_notifier.rb', line 99 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: "*#{}*", 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 && slack_error_notifiable?(e) raise end |