-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathslack_error_notifier.rb
109 lines (91 loc) · 3.21 KB
/
slack_error_notifier.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'slack-notifier'
module WorkerTools
module SlackErrorNotifier
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
def slack_error_notifier_enabled
Rails.env.production?
end
def slack_error_notifiable?(error)
error.is_a?(StandardError)
end
def slack_error_notifier_emoji
':red_circle:'
end
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
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
def slack_error_notifier_username
'Notifier'
end
def slack_error_notifier_receivers
# Ex: '@all'
end
def slack_error_notifier_attachments_color
# good, warning, danger, hex color
'danger'
end
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
def slack_error_notifier_error_details(error)
error.backtrace[0..2].join("\n")
end
def slack_error_notifier_message
message = []
message << slack_error_notifier_receivers
message << slack_error_notifier_title
message.compact.join(' - ')
end
def slack_error_notifier_attachments(error)
[
{ color: slack_error_notifier_attachments_color, fields: slack_error_notifier_attachments_fields },
{
title: [error.class, error.message].join(' : '),
color: slack_error_notifier_attachments_color,
text: slack_error_notifier_error_details(error)
}
]
end
def slack_error_notifier_attachments_fields
[
{ title: 'Application', value: Rails.application.class.module_parent_name, short: true },
{ title: 'Environment', value: Rails.env, short: true }
]
end
def slack_error_notifier
Slack::Notifier.new(slack_error_notifier_webhook)
end
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: slack_error_notifier_attachments(error)
)
end
end
end