Skip to content

Commit

Permalink
Merge 41c55f0 into 2443af1
Browse files Browse the repository at this point in the history
  • Loading branch information
FLarra committed Dec 9, 2018
2 parents 2443af1 + 41c55f0 commit 4eb2053
Show file tree
Hide file tree
Showing 37 changed files with 558 additions and 547 deletions.
520 changes: 263 additions & 257 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -5,7 +5,7 @@ require 'appraisal'

require 'rake/testtask'

task :default => [:test]
task default: [:test]

Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
Expand Down
17 changes: 10 additions & 7 deletions examples/sinatra/sinatra_app.rb
Expand Up @@ -5,15 +5,18 @@

class SinatraApp < Sinatra::Base
use Rack::Config do |env|
env["action_dispatch.parameter_filter"] = [:password] # This is highly recommended. It will prevent the ExceptionNotification email from including your users' passwords
env['action_dispatch.parameter_filter'] = [:password] # This is highly recommended. It will prevent the ExceptionNotification email from including your users' passwords
end

use ExceptionNotification::Rack,
:email => {
:email_prefix => "[Example] ",
:sender_address => %{"notifier" <notifier@example.com>},
:exception_recipients => %w{exceptions@example.com},
:smtp_settings => { :address => "localhost", :port => 1025 }
email: {
email_prefix: '[Example] ',
sender_address: %{"notifier" <notifier@example.com>},
exception_recipients: %w{exceptions@example.com},
smtp_settings: {
address: 'localhost',
port: 1025
}
}

get '/' do
Expand All @@ -25,7 +28,7 @@ class SinatraApp < Sinatra::Base
begin
1/0
rescue Exception => e
ExceptionNotifier.notify_exception(e, :data => {:msg => "Cannot divide by zero!"})
ExceptionNotifier.notify_exception(e, data: { msg: 'Cannot divide by zero!' })
end
'Check email at <a href="http://localhost:1080">mailcatcher</a>.'
end
Expand Down
2 changes: 1 addition & 1 deletion lib/exception_notification/rack.rb
Expand Up @@ -48,7 +48,7 @@ def call(env)

response
rescue Exception => exception
if ExceptionNotifier.notify_exception(exception, :env => env)
if ExceptionNotifier.notify_exception(exception, env: env)
env['exception_notifier.delivered'] = true
end

Expand Down
16 changes: 7 additions & 9 deletions lib/exception_notification/resque.rb
Expand Up @@ -2,23 +2,21 @@

module ExceptionNotification
class Resque < Resque::Failure::Base

def self.count
Stat[:failed]
end

def save
data = {
:failed_at => Time.now.to_s,
:queue => queue,
:worker => worker.to_s,
:payload => payload,
:error_class => exception.class.name,
:error_message => exception.message
error_class: exception.class.name,
error_message: exception.message
failed_at: Time.now.to_s,
payload: payload,
queue: queue,
worker: worker.to_s,
}

ExceptionNotifier.notify_exception(exception, :data => { :resque => data })
ExceptionNotifier.notify_exception(exception, data: { resque: data })
end

end
end
5 changes: 2 additions & 3 deletions lib/exception_notification/sidekiq.rb
Expand Up @@ -3,12 +3,11 @@
# Note: this class is only needed for Sidekiq version < 3.
module ExceptionNotification
class Sidekiq

def call(worker, msg, queue)
begin
yield
rescue Exception => exception
ExceptionNotifier.notify_exception(exception, :data => { :sidekiq => msg })
ExceptionNotifier.notify_exception(exception, data: { sidekiq: msg })
raise exception
end
end
Expand All @@ -25,7 +24,7 @@ def call(worker, msg, queue)
else
::Sidekiq.configure_server do |config|
config.error_handlers << Proc.new { |ex, context|
ExceptionNotifier.notify_exception(ex, :data => { :sidekiq => context })
ExceptionNotifier.notify_exception(ex, data: { sidekiq: context })
}
end
end
1 change: 0 additions & 1 deletion lib/exception_notifier/base_notifier.rb
Expand Up @@ -20,6 +20,5 @@ def _pre_callback(exception, options, message, message_opts)
def _post_callback(exception, options, message, message_opts)
@base_options[:post_callback].call(options, self, exception.backtrace, message, message_opts) if @base_options[:post_callback].respond_to?(:call)
end

end
end
40 changes: 20 additions & 20 deletions lib/exception_notifier/email_notifier.rb
Expand Up @@ -106,11 +106,11 @@ def compose_email
exception_recipients = maybe_call(@options[:exception_recipients])

headers = {
:delivery_method => @options[:delivery_method],
:to => exception_recipients,
:from => @options[:sender_address],
:subject => subject,
:template_name => name
delivery_method: @options[:delivery_method],
to: exception_recipients,
from: @options[:sender_address],
subject: subject,
template_name: name
}.merge(@options[:email_headers])

mail = mail(headers) do |format|
Expand Down Expand Up @@ -194,21 +194,21 @@ def create_email(exception, options={})

def self.default_options
{
:sender_address => %("Exception Notifier" <exception.notifier@example.com>),
:exception_recipients => [],
:email_prefix => "[ERROR] ",
:email_format => :text,
:sections => %w(request session environment backtrace),
:background_sections => %w(backtrace data),
:verbose_subject => true,
:normalize_subject => false,
:include_controller_and_action_names_in_subject => true,
:delivery_method => nil,
:mailer_settings => nil,
:email_headers => {},
:mailer_parent => 'ActionMailer::Base',
:template_path => 'exception_notifier',
:deliver_with => :default
sender_address: %("Exception Notifier" <exception.notifier@example.com>),
exception_recipients: [],
email_prefix: '[ERROR] ',
email_format: :text,
sections: %w(request session environment backtrace),
background_sections: %w(backtrace data),
verbose_subject: true,
normalize_subject: false,
include_controller_and_action_names_in_subject: true,
delivery_method: nil,
mailer_settings: nil,
email_headers: {},
mailer_parent: 'ActionMailer::Base',
template_path: 'exception_notifier',
deliver_with: :default
}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/exception_notifier/hipchat_notifier.rb
Expand Up @@ -11,7 +11,7 @@ def initialize(options)
api_token = options.delete(:api_token)
room_name = options.delete(:room_name)
opts = {
:api_version => options.delete(:api_version) || 'v1'
api_version: options.delete(:api_version) || 'v1'
}
opts[:server_url] = options.delete(:server_url) if options[:server_url]
@from = options.delete(:from) || 'Exception'
Expand Down
2 changes: 0 additions & 2 deletions lib/exception_notifier/modules/backtrace_cleaner.rb
@@ -1,13 +1,11 @@
module ExceptionNotifier
module BacktraceCleaner

def clean_backtrace(exception)
if defined?(Rails) && Rails.respond_to?(:backtrace_cleaner)
Rails.backtrace_cleaner.send(:filter, exception.backtrace)
else
exception.backtrace
end
end

end
end
4 changes: 2 additions & 2 deletions lib/exception_notifier/notifier.rb
Expand Up @@ -4,8 +4,8 @@ module ExceptionNotifier
class Notifier

def self.exception_notification(env, exception, options={})
ActiveSupport::Deprecation.warn "Please use ExceptionNotifier.notify_exception(exception, options.merge(:env => env))."
ExceptionNotifier.registered_exception_notifier(:email).create_email(exception, options.merge(:env => env))
ActiveSupport::Deprecation.warn "Please use ExceptionNotifier.notify_exception(exception, options.merge(env: env))."
ExceptionNotifier.registered_exception_notifier(:email).create_email(exception, options.merge(env: env))
end

def self.background_exception_notification(exception, options={})
Expand Down
@@ -1,3 +1,3 @@
<pre style="font-size: 12px; padding: 10px; border: 1px solid #e1e1e8; background-color:#f5f5f5">
<%= @backtrace.join("\n") %>
<%= @backtrace.join("\n") %>
</pre>
@@ -1,5 +1,5 @@
<% filtered_env = @request.filtered_env -%>
<% max = filtered_env.keys.map(&:to_s).max { |a, b| a.length <=> b.length } -%>
<% filtered_env.keys.map(&:to_s).sort.each do |key| -%>
* <%= raw safe_encode("%-*s: %s" % [max.length, key, inspect_object(filtered_env[key])]) %>
* <%= raw safe_encode("%-*s: %s" % [max.length, key, inspect_object(filtered_env[key])]) %>
<% end -%>
Expand Up @@ -5,6 +5,6 @@
* Timestamp : <%= raw @timestamp %>
* Server : <%= raw Socket.gethostname %>
<% if defined?(Rails) && Rails.respond_to?(:root) %>
* Rails root : <%= raw Rails.root %>
* Rails root : <%= raw Rails.root %>
<% end %>
* Process: <%= raw $$ %>
Expand Up @@ -11,11 +11,11 @@
begin
summary = render(section).strip
unless summary.blank?
title = render("title", :title => section).strip
title = render("title", title: section).strip
[title, summary]
end
rescue Exception => e
title = render("title", :title => section).strip
title = render("title", title: section).strip
summary = ["ERROR: Failed to generate exception summary:", [e.class.to_s, e.message].join(": "), e.backtrace && e.backtrace.join("\n")].compact.join("\n\n")
[title, summary]
end
Expand Down
Expand Up @@ -8,12 +8,12 @@
begin
summary = render(section).strip
unless summary.blank?
title = render("title", :title => section).strip
title = render("title", title: section).strip
"#{title}\n\n#{summary.gsub(/^/, " ")}\n\n"
end

rescue Exception => e
title = render("title", :title => section).strip
title = render("title", title: section).strip
summary = ["ERROR: Failed to generate exception summary:", [e.class.to_s, e.message].join(": "), e.backtrace && e.backtrace.join("\n")].compact.join("\n\n")

[title, summary.gsub(/^/, " "), nil].join("\n\n")
Expand Down
20 changes: 12 additions & 8 deletions lib/exception_notifier/webhook_notifier.rb
Expand Up @@ -22,19 +22,23 @@ def call(exception, options={})
if defined?(Rails) && Rails.respond_to?(:root)
options[:body][:rails_root] = Rails.root
end
options[:body][:exception] = {:error_class => exception.class.to_s,
:message => exception.message.inspect,
:backtrace => exception.backtrace}
options[:body][:exception] = {
error_class: exception.class.to_s,
message: exception.message.inspect,
backtrace: exception.backtrace
}
options[:body][:data] = (env && env['exception_notifier.exception_data'] || {}).merge(options[:data] || {})

unless env.nil?
request = ActionDispatch::Request.new(env)

request_items = {:url => request.original_url,
:http_method => request.method,
:ip_address => request.remote_ip,
:parameters => request.filtered_parameters,
:timestamp => Time.current }
request_items = {
url: request.original_url,
http_method: request.method,
ip_address: request.remote_ip,
parameters: request.filtered_parameters,
timestamp: Time.current
}

options[:body][:request] = request_items
options[:body][:session] = request.session
Expand Down
6 changes: 3 additions & 3 deletions lib/generators/exception_notification/install_generator.rb
@@ -1,11 +1,11 @@
module ExceptionNotification
module Generators
class InstallGenerator < Rails::Generators::Base
desc "Creates a ExceptionNotification initializer."
desc 'Creates a ExceptionNotification initializer.'

source_root File.expand_path('../templates', __FILE__)
class_option :resque, :type => :boolean, :desc => 'Add support for sending notifications when errors occur in Resque jobs.'
class_option :sidekiq, :type => :boolean, :desc => 'Add support for sending notifications when errors occur in Sidekiq jobs.'
class_option :resque, type: :boolean, desc: 'Add support for sending notifications when errors occur in Resque jobs.'
class_option :sidekiq, type: :boolean, desc: 'Add support for sending notifications when errors occur in Sidekiq jobs.'

def copy_initializer
template 'exception_notification.rb', 'config/initializers/exception_notification.rb'
Expand Down
Expand Up @@ -26,28 +26,27 @@

# Email notifier sends notifications by email.
config.add_notifier :email, {
:email_prefix => "[ERROR] ",
:sender_address => %{"Notifier" <notifier@example.com>},
:exception_recipients => %w{exceptions@example.com}
email_prefix: '[ERROR] ',
sender_address: %{"Notifier" <notifier@example.com>},
exception_recipients: %w{exceptions@example.com}
}

# Campfire notifier sends notifications to your Campfire room. Requires 'tinder' gem.
# config.add_notifier :campfire, {
# :subdomain => 'my_subdomain',
# :token => 'my_token',
# :room_name => 'my_room'
# subdomain: 'my_subdomain',
# token: 'my_token',
# room_name: 'my_room'
# }

# HipChat notifier sends notifications to your HipChat room. Requires 'hipchat' gem.
# config.add_notifier :hipchat, {
# :api_token => 'my_token',
# :room_name => 'my_room'
# api_token: 'my_token',
# room_name: 'my_room'
# }

# Webhook notifier sends notifications over HTTP protocol. Requires 'httparty' gem.
# config.add_notifier :webhook, {
# :url => 'http://example.com:5555/hubot/path',
# :http_method => :post
# url: 'http://example.com:5555/hubot/path',
# http_method: :post
# }

end
10 changes: 5 additions & 5 deletions test/dummy/app/controllers/posts_controller.rb
Expand Up @@ -6,7 +6,7 @@ def show

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
format.xml { render xml: @post }
end
end

Expand All @@ -19,11 +19,11 @@ def create

respond_to do |format|
if @post.save
format.html { redirect_to(post_path(@post), :notice => 'Post was successfully created.') }
format.xml { render :xml => @post, :status => :created, :location => @post }
format.html { redirect_to(post_path(@post), notice: 'Post was successfully created.') }
format.xml { render xml: @post, status: :created, location: @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
format.html { render action: 'new' }
format.xml { render xml: @post.errors, status: :unprocessable_entity }
end
end
end
Expand Down
20 changes: 10 additions & 10 deletions test/dummy/config/environment.rb
Expand Up @@ -2,16 +2,16 @@
require File.expand_path('../application', __FILE__)

Dummy::Application.config.middleware.use ExceptionNotification::Rack,
:email => {
:email_prefix => "[Dummy ERROR] ",
:sender_address => %{"Dummy Notifier" <dummynotifier@example.com>},
:exception_recipients => %w{dummyexceptions@example.com},
:email_headers => { "X-Custom-Header" => "foobar" },
:sections => ['new_section', 'request', 'session', 'environment', 'backtrace'],
:background_sections => %w(new_bkg_section backtrace data),
:pre_callback => proc { |opts, notifier, backtrace, message, message_opts| message_opts[:pre_callback_called] = 1 },
:post_callback => proc { |opts, notifier, backtrace, message, message_opts| message_opts[:post_callback_called] = 1 }
}
email: {
email_prefix: '[Dummy ERROR] ',
sender_address: %{"Dummy Notifier" <dummynotifier@example.com>},
exception_recipients: %w{dummyexceptions@example.com},
email_headers: { 'X-Custom-Header' => 'foobar' },
sections: ['new_section', 'request', 'session', 'environment', 'backtrace'],
background_sections: %w(new_bkg_section backtrace data),
pre_callback: proc { |opts, notifier, backtrace, message, message_opts| message_opts[:pre_callback_called] = 1 },
post_callback: proc { |opts, notifier, backtrace, message, message_opts| message_opts[:post_callback_called] = 1 }
}

# Initialize the rails application
Dummy::Application.initialize!
2 changes: 1 addition & 1 deletion test/dummy/config/initializers/session_store.rb
@@ -1,6 +1,6 @@
# Be sure to restart your server when you modify this file.

Dummy::Application.config.session_store :cookie_store, :key => '_dummy_session'
Dummy::Application.config.session_store :cookie_store, key: '_dummy_session'

# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
Expand Down

0 comments on commit 4eb2053

Please sign in to comment.