Skip to content

Commit

Permalink
Merge be2f0a3 into f9dee5b
Browse files Browse the repository at this point in the history
  • Loading branch information
hoahm committed Dec 28, 2014
2 parents f9dee5b + be2f0a3 commit 06c3759
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
20 changes: 10 additions & 10 deletions README.md
Expand Up @@ -565,17 +565,17 @@ To configure it, you need to set at least the 'team' and 'token' options, like t

```ruby
Whatever::Application.config.middleware.use ExceptionNotification::Rack,
:email => {
:email_prefix => "[Whatever] ",
:sender_address => %{"notifier" <notifier@example.com>},
:exception_recipients => %w{exceptions@example.com}
email: {
email_prefix: "[Whatever] ",
sender_address: %{"notifier" <notifier@example.com>},
exception_recipients: %w{exceptions@example.com}
},
:slack => {
:webhook_url => "[Your webhook url]",
:channel => "#exceptions",
:additional_parameters => {
:icon_url => "http://image.jpg"
}
slack: {
webhook_url: "[Your webhook url]",
channel: '#channel-name',
username: 'Exception Notification',
icon_emoji: ':ghost:',
color: 'danger'
}
```

Expand Down
46 changes: 31 additions & 15 deletions lib/exception_notifier/slack_notifier.rb
@@ -1,27 +1,43 @@
module ExceptionNotifier
class SlackNotifier

attr_accessor :notifier
attr_accessor :slack_options

def initialize(options)
begin
webhook_url = options.fetch(:webhook_url)
@message_opts = options.fetch(:additional_parameters, {})
@notifier = Slack::Notifier.new webhook_url, options
rescue
@notifier = nil
end
self.slack_options = options
end

def call(exception, options={})
message = "An exception occurred: '#{exception.message}' on '#{exception.backtrace.first}'"
@notifier.ping(message, @message_opts) if valid?
end
env = options[:env]

protected
link = env['HTTP_HOST'] + env['REQUEST_URI']
title = "#{env['REQUEST_METHOD']} <http://#{link}|http://#{link}>\n"

message = "------------------------------------------------------------------------------------------\n"
message += "*Project:* #{Rails.application.class.parent_name}\n"
message += "*Environment:* #{Rails.env}\n"
message += "*Time:* #{Time.zone.now.strftime('%Y-%m-%d %H:%M:%S')}\n"
message += "*Exception:* `#{exception.message}`\n"

req = Rack::Request.new(env)
unless req.params.empty?
message += "*Parameters:*\n"
message += req.params.map { |k, v| ">#{k}=#{v}" }.join("\n")
message += "\n"
end
message += "*Backtrace*: \n"
message += "`#{exception.backtrace.first}`"

def valid?
!@notifier.nil?
notifier = Slack::Notifier.new slack_options.fetch(:webhook_url),
channel: slack_options.fetch(:channel),
username: slack_options.fetch(:username),
icon_emoji: slack_options.fetch(:icon_emoji),
attachments: [{
color: 'danger',
title: title,
text: message,
mrkdwn_in: %w(text title fallback)
}]
notifier.ping ''
end
end
end

0 comments on commit 06c3759

Please sign in to comment.