Skip to content

Commit

Permalink
Merge pull request #3 from morygonzalez/inject-custom-data
Browse files Browse the repository at this point in the history
Add :custom_fields option for injecting custom data from consumer app
  • Loading branch information
morygonzalez committed Dec 13, 2015
2 parents aa502c9 + b2c25fb commit c18c12d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 35 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ add below lines to config/initializers/exception_notification.rb.
color: :danger, # optional, default to :danger
additional_parameters: {
icon_emoji: ':warning:'
}
},
custom_fields: [
{
title: 'User Agent',
value: ->(req) { req.user_agent },
short: false,
after: 'IP Address'
}
]
}
```

Expand Down
82 changes: 50 additions & 32 deletions lib/exception_notifier/slacky_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def initialize(options)
webhook_url = options.fetch(:webhook_url)
@color = options.fetch(:color, :danger)
@message_opts = options.fetch(:additional_parameters, {})
@custom_fields = options.fetch(:custom_fields, [])
@notifier = Slack::Notifier.new(webhook_url, options)
rescue
@notifier = nil
Expand Down Expand Up @@ -40,39 +41,56 @@ def build_attachemnt(exception, options = {})
fallback: "#{exception.class} #{exception.message}",
color: @color.to_s,
title: "[ERROR] #{exception.class}",
fields: [
{
title: "Host",
value: (Socket.gethostname rescue nil),
short: true
},
{
title: "Request path",
value: @request.path_info,
short: true
},
{
title: "HTTP Method",
value: @request.request_method,
short: true
},
{
title: "IP Address",
value: @request.ip,
short: true
},
{
title: "Occurred on",
value: (exception.backtrace.first rescue nil),
short: false
},
{
title: "Error message",
value: exception.message,
short: false
}
]
fields: build_fields(exception)
}
end

def build_fields(exception)
fields = [
{
title: "Host",
value: (Socket.gethostname rescue nil),
short: true
},
{
title: "Request path",
value: @request.path_info,
short: true
},
{
title: "HTTP Method",
value: @request.request_method,
short: true
},
{
title: "IP Address",
value: @request.ip,
short: true
},
{
title: "Occurred on",
value: (exception.backtrace.first rescue nil),
short: false
},
{
title: "Error message",
value: exception.message,
short: false
}
]

@custom_fields.each do |custom_field|
field = {
title: custom_field[:title],
value: custom_field[:value].call(@request),
short: custom_field[:short]
}
i = fields.index {|f| f[:title] == custom_field[:after]}
i = i ? i.succ : -1
fields.insert(i, field)
end

fields
end
end
end
24 changes: 22 additions & 2 deletions spec/exception_notification/slacky_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
end
end

let(:user_agent) do
'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25'
end

let(:fake_notification) do
[
'Exception Occured!',
Expand All @@ -28,6 +32,7 @@
{ title: 'Request path', value: '/foo/bar', short: true },
{ title: 'HTTP Method', value: 'POST', short: true },
{ title: 'IP Address', value: '127.0.0.1', short: true },
{ title: 'User Agent', value: user_agent, short: false },
{ title: 'Occurred on', value: exception.backtrace.first, short: false },
{ title: 'Error message', value: exception.message, short: false }
]
Expand All @@ -48,12 +53,27 @@
channel: '#general',
additional_parameters: {
icon_emoji: ':warning:'
}
},
custom_fields: [
{
title: 'User Agent',
value: ->(req) { req.user_agent },
short: false,
after: 'IP Address'
}
]
}
end

let(:rack_options) do
{ env: { 'PATH_INFO' => '/foo/bar', 'REQUEST_METHOD' => 'POST', 'REMOTE_ADDR' => '127.0.0.1' } }
{
env: {
'PATH_INFO' => '/foo/bar',
'REQUEST_METHOD' => 'POST',
'REMOTE_ADDR' => '127.0.0.1',
'HTTP_USER_AGENT' => user_agent
}
}
end

describe 'Notification format' do
Expand Down

0 comments on commit c18c12d

Please sign in to comment.