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
110
111
112
113
114
115
|
# File 'lib/slack_500.rb', line 41
def self.post (request, exception, params = {})
url = self.webhook_url
begin
uri = URI.parse(url)
rescue
Rails.logger.error '** Slack500:: invalid Webhook URL.'
return
end
bullet = ':black_small_square:'
text = "#{exception.message}\n\n"
text += "#{bullet}*Request*\n*#{request.method}* #{request.url}\n\n"
text += "#{bullet}*User Agent*\n#{request.user_agent}\n\n"
text += "#{bullet}*IP*\n#{request.remote_ip}\n\n"
text += "#{bullet}*Query*\n#{request.query_parameters}\n\n" unless request.query_parameters.empty?
request.body.rewind
body_text = request.body.read
begin
if body_text.present?
body = JSON.parse(body_text)
end
rescue => e
if body_text.present?
body_params = {}
body_text.split('&').each do |param|
kv = param.split("=")
if kv.length == 2
if kv[0].downcase.index('token').present? || kv[0].downcase.index('password').present?
body_params[URI.decode(kv[0])] = '[** FILTERED **]'
elsif kv[0] != 'utf8'
body_params[URI.decode(kv[0])] = truncate(URI.decode(kv[1]).force_encoding('UTF-8'),100)
end
end
end
if body_params.empty?
body = body_text
else
body = body_params
end
end
end
text += "#{bullet}*Body*\n#{body}\n\n" unless body.nil?
text += "#{bullet}*Backtrace*\n#{exception.backtrace.map {|s| "`#{s.gsub('`', '').gsub("'", '').gsub(Rails.root.to_s, '')}`"}.join("\n")}"
text = text.force_encoding('UTF-8')
default_params = {
pretext: self.pretext,
title: self.title,
color: self.color,
footer: self.
}
attachments = default_params.merge(params)
attachments[:text] = text
attachments[:title] = "#{request.parameters[:controller]}##{request.parameters[:action]} - #{attachments[:title]}"
params = {
attachments: [attachments]
}
begin
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.start do
request = Net::HTTP::Post.new(uri.path)
request.set_form_data(payload: params.to_json)
http.request(request)
end
rescue => e
Rails.logger.error "** Slack500:: #{e.full_message}."
end
end
|