Skip to content

Commit

Permalink
Merge d5b2312 into f825b9f
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilioCristalli committed Jan 24, 2019
2 parents f825b9f + d5b2312 commit 5754e40
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 138 deletions.
132 changes: 30 additions & 102 deletions .rubocop_todo.yml
@@ -1,77 +1,28 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-12-18 20:02:43 -0300 using RuboCop version 0.59.2.
# on 2019-01-23 23:57:48 -0300 using RuboCop version 0.50.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Configuration parameters: Include.
# Include: **/*.gemspec
Gemspec/RequiredRubyVersion:
Exclude:
- 'exception_notification.gemspec'

# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyleAlignWith, AutoCorrect, Severity.
# SupportedStylesAlignWith: keyword, variable, start_of_line
Layout/EndAlignment:
Exclude:
- 'lib/exception_notifier/campfire_notifier.rb'
- 'lib/exception_notifier/hipchat_notifier.rb'

# Offense count: 2
# Cop supports --auto-correct.
Layout/RescueEnsureAlignment:
Exclude:
- 'lib/exception_notifier/modules/error_grouping.rb'
- 'test/exception_notifier/webhook_notifier_test.rb'

# Offense count: 2
# Configuration parameters: AllowSafeAssignment.
Lint/AssignmentInCondition:
Exclude:
- 'lib/exception_notifier/modules/error_grouping.rb'

# Offense count: 12
# Offense count: 4
Lint/RescueException:
Exclude:
- 'examples/sinatra/sinatra_app.rb'
- 'lib/exception_notification/rack.rb'
- 'lib/exception_notification/sidekiq.rb'
- 'lib/exception_notifier.rb'
- 'test/exception_notifier/campfire_notifier_test.rb'
- 'test/exception_notifier/hipchat_notifier_test.rb'
- 'test/exception_notifier/irc_notifier_test.rb'
- 'test/exception_notifier/slack_notifier_test.rb'
- 'test/exception_notifier/sns_notifier_test.rb'
- 'test/exception_notifier/webhook_notifier_test.rb'

# Offense count: 2
# Configuration parameters: ContextCreatingMethods, MethodCreatingMethods.
Lint/UselessAccessModifier:
Exclude:
- 'lib/exception_notifier/datadog_notifier.rb'
- 'test/exception_notifier/datadog_notifier_test.rb'

# Offense count: 1
Lint/UselessAssignment:
Exclude:
- 'lib/exception_notifier/sns_notifier.rb'

# Offense count: 18
Metrics/AbcSize:
Max: 97
Max: 98

# Offense count: 3
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 88

# Offense count: 10
# Offense count: 11
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 186
Expand All @@ -80,8 +31,14 @@ Metrics/ClassLength:
Metrics/CyclomaticComplexity:
Max: 24

# Offense count: 28
# Configuration parameters: CountComments, ExcludedMethods.
# Offense count: 253
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 226

# Offense count: 29
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 90

Expand All @@ -90,8 +47,12 @@ Metrics/PerceivedComplexity:
Max: 24

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, EnforcedStyle.
Style/CaseEquality:
Exclude:
- 'test/exception_notification/resque_test.rb'

# Offense count: 1
# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: nested, compact
Style/ClassAndModuleChildren:
Exclude:
Expand All @@ -103,7 +64,7 @@ Style/ClassVars:
- 'lib/exception_notifier.rb'
- 'test/exception_notifier/modules/error_grouping_test.rb'

# Offense count: 28
# Offense count: 29
Style/Documentation:
Enabled: false

Expand All @@ -112,11 +73,6 @@ Style/DoubleNegation:
Exclude:
- 'lib/exception_notifier/irc_notifier.rb'

# Offense count: 1
Style/EvalWithLocation:
Exclude:
- 'test/exception_notifier_test.rb'

# Offense count: 6
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Expand All @@ -128,53 +84,25 @@ Style/GuardClause:
- 'lib/exception_notifier/slack_notifier.rb'
- 'lib/exception_notifier/sns_notifier.rb'

# Offense count: 7
# Offense count: 1
# Cop supports --auto-correct.
Style/IfUnlessModifier:
Exclude:
- 'lib/exception_notification/rack.rb'
- 'lib/exception_notifier/datadog_notifier.rb'
- 'lib/exception_notifier/google_chat_notifier.rb'
- 'lib/exception_notifier/webhook_notifier.rb'
- 'test/dummy/test/functional/posts_controller_test.rb'
- 'test/exception_notifier/email_notifier_test.rb'

# Offense count: 3
Style/MethodMissingSuper:
Style/MethodCallWithoutArgsParentheses:
Exclude:
- 'lib/exception_notifier/email_notifier.rb'
- 'lib/exception_notifier/mattermost_notifier.rb'
- 'lib/exception_notifier/teams_notifier.rb'
- 'test/exception_notification/resque_test.rb'

# Offense count: 3
Style/MissingRespondToMissing:
Style/MethodMissing:
Exclude:
- 'lib/exception_notifier/email_notifier.rb'
- 'lib/exception_notifier/mattermost_notifier.rb'
- 'lib/exception_notifier/teams_notifier.rb'

# Offense count: 1
Style/MultilineBlockChain:
Exclude:
- 'lib/exception_notifier/email_notifier.rb'

# Offense count: 2
Style/NestedTernaryOperator:
Exclude:
- 'lib/exception_notifier/slack_notifier.rb'
- 'lib/exception_notifier/sns_notifier.rb'

# Offense count: 1
# Offense count: 18
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, EnforcedStyle, IgnoredMethods.
# SupportedStyles: predicate, comparison
Style/NumericPredicate:
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Style/StringLiterals:
Exclude:
- 'spec/**/*'
- 'test/exception_notifier/modules/error_grouping_test.rb'

# Offense count: 253
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 226
- 'lib/exception_notifier/datadog_notifier.rb'
- 'test/exception_notification/resque_test.rb'
- 'test/exception_notifier/datadog_notifier_test.rb'
2 changes: 1 addition & 1 deletion examples/sinatra/sinatra_app.rb
Expand Up @@ -28,7 +28,7 @@ class SinatraApp < Sinatra::Base
get '/background_notification' do
begin
1 / 0
rescue Exception => e
rescue StandardError => e
ExceptionNotifier.notify_exception(e, data: { msg: 'Cannot divide by zero!' })
end
'Check email at <a href="http://localhost:1080">mailcatcher</a>.'
Expand Down
2 changes: 1 addition & 1 deletion lib/exception_notification/resque.rb
Expand Up @@ -13,7 +13,7 @@ def save
failed_at: Time.now.to_s,
payload: payload,
queue: queue,
worker: worker.to_s,
worker: worker.to_s
}

ExceptionNotifier.notify_exception(exception, data: { resque: data })
Expand Down
2 changes: 1 addition & 1 deletion lib/exception_notifier/campfire_notifier.rb
Expand Up @@ -22,7 +22,7 @@ def call(exception, options = {})
"The exception occurred #{options[:accumulated_errors_count]} times: '#{exception.message}'"
else
"A new exception occurred: '#{exception.message}'"
end
end
message += " on '#{exception.backtrace.first}'" if exception.backtrace
send_notice(exception, options, message) do |msg, _|
@room.paste msg
Expand Down
2 changes: 0 additions & 2 deletions lib/exception_notifier/datadog_notifier.rb
Expand Up @@ -22,8 +22,6 @@ def datadog_event(exception, options = {})
).event
end

private

class DatadogExceptionEvent
include ExceptionNotifier::BacktraceCleaner

Expand Down
9 changes: 6 additions & 3 deletions lib/exception_notifier/email_notifier.rb
Expand Up @@ -58,7 +58,7 @@ def background_exception_notification(exception, options = {}, default_options =
private

def compose_subject
subject = "#{@options[:email_prefix]}"
subject = @options[:email_prefix].to_s.dup
subject << "(#{@options[:accumulated_errors_count]} times)" if @options[:accumulated_errors_count].to_i > 1
subject << "#{@kontroller.controller_name} #{@kontroller.action_name}" if @kontroller && @options[:include_controller_and_action_names_in_subject]
subject << " (#{@exception.class})"
Expand Down Expand Up @@ -141,15 +141,18 @@ def initialize(options)
mailer_settings_key = "#{delivery_method}_settings".to_sym
options[:mailer_settings] = options.delete(mailer_settings_key)

options.reverse_merge(EmailNotifier.default_options).select do |k, _v|
merged_opts = options.reverse_merge(EmailNotifier.default_options)
filtered_opts = merged_opts.select do |k, _v|
%i[
sender_address exception_recipients pre_callback
post_callback email_prefix email_format
sections background_sections verbose_subject normalize_subject
include_controller_and_action_names_in_subject delivery_method mailer_settings
email_headers mailer_parent template_path deliver_with
].include?(k)
end .each { |k, v| send("#{k}=", v) }
end

filtered_opts.each { |k, v| send("#{k}=", v) }
end

def options
Expand Down
2 changes: 1 addition & 1 deletion lib/exception_notifier/hipchat_notifier.rb
Expand Up @@ -20,7 +20,7 @@ def initialize(options)
"The exception occurred #{errors_count} times: '#{Rack::Utils.escape_html(exception.message)}'"
else
"A new exception occurred: '#{Rack::Utils.escape_html(exception.message)}'"
end
end
msg += " on '#{exception.backtrace.first}'" if exception.backtrace
msg
}
Expand Down
4 changes: 2 additions & 2 deletions lib/exception_notifier/modules/error_grouping.rb
Expand Up @@ -46,13 +46,13 @@ def group_error!(exception, options)
message_based_key = "exception:#{Zlib.crc32("#{exception.class.name}\nmessage:#{exception.message}")}"
accumulated_errors_count = 1

if count = error_count(message_based_key)
if (count = error_count(message_based_key))
accumulated_errors_count = count + 1
save_error_count(message_based_key, accumulated_errors_count)
else
backtrace_based_key = "exception:#{Zlib.crc32("#{exception.class.name}\npath:#{exception.backtrace.try(:first)}")}"

if count = error_grouping_cache.read(backtrace_based_key)
if (count = error_grouping_cache.read(backtrace_based_key))
accumulated_errors_count = count + 1
save_error_count(backtrace_based_key, accumulated_errors_count)
else
Expand Down
20 changes: 13 additions & 7 deletions lib/exception_notifier/slack_notifier.rb
Expand Up @@ -20,13 +20,13 @@ def initialize(options)
end
end

def call(exception, options={})
clean_message = exception.message.gsub("`", "'")
def call(exception, options = {})
clean_message = exception.message.tr('`', "'")
attchs = attchs(exception, clean_message, options)

if valid?
args = [exception, options, clean_message, @message_opts.merge(attachments: attchs)]
send_notice(*args) do |msg, message_opts|
send_notice(*args) do |_msg, message_opts|
@notifier.ping '', message_opts
end
end
Expand All @@ -52,13 +52,19 @@ def attchs(exception, clean_message, options)
text, data = information_from_options(exception.class, options)
fields = fields(clean_message, exception.backtrace, data)

[color: @color, text: text, fields: fields, mrkdwn_in: %w(text fields)]
[color: @color, text: text, fields: fields, mrkdwn_in: %w[text fields]]
end

def information_from_options(exception_class, options)
errors_count = options[:accumulated_errors_count].to_i
measure_word = errors_count > 1 ? errors_count : (exception_class.to_s =~ /^[aeiou]/i ? 'An' : 'A')
exception_name = "*#{measure_word}* `#{exception_class.to_s}`"

measure_word = if errors_count > 1
errors_count
else
exception_class.to_s =~ /^[aeiou]/i ? 'An' : 'A'
end

exception_name = "*#{measure_word}* `#{exception_class}`"
env = options[:env]

if env.nil?
Expand Down Expand Up @@ -90,7 +96,7 @@ def fields(clean_message, backtrace, data)

unless data.empty?
deep_reject(data, @ignore_data_if) if @ignore_data_if.is_a?(Proc)
data_string = data.map{|k,v| "#{k}: #{v}"}.join("\n")
data_string = data.map { |k, v| "#{k}: #{v}" }.join("\n")
fields << { title: 'Data', value: "```#{data_string}```" }
end

Expand Down
10 changes: 8 additions & 2 deletions lib/exception_notifier/sns_notifier.rb
Expand Up @@ -59,13 +59,19 @@ def build_message(exception, options)

if exception.backtrace
formatted_backtrace = exception.backtrace.first(options[:backtrace_lines]).join("\n").to_s
text += "Backtrace:\n#{formatted_backtrace}\n"
text + "Backtrace:\n#{formatted_backtrace}\n"
end
end

def accumulated_exception_name(exception, options)
errors_count = options[:accumulated_errors_count].to_i
measure_word = errors_count > 1 ? errors_count : (exception.class.to_s =~ /^[aeiou]/i ? 'An' : 'A')

measure_word = if errors_count > 1
errors_count
else
exception.class.to_s =~ /^[aeiou]/i ? 'An' : 'A'
end

"#{measure_word} #{exception.class}"
end

Expand Down
2 changes: 1 addition & 1 deletion lib/generators/exception_notification/install_generator.rb
Expand Up @@ -8,7 +8,7 @@ class InstallGenerator < Rails::Generators::Base
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'
template 'exception_notification.rb.erb', 'config/initializers/exception_notification.rb'
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/exception_notifier/campfire_notifier_test.rb
Expand Up @@ -101,7 +101,7 @@ def fake_notification

def fake_exception
5 / 0
rescue Exception => e
rescue StandardError => e
e
end

Expand Down
2 changes: 0 additions & 2 deletions test/exception_notifier/datadog_notifier_test.rb
Expand Up @@ -92,8 +92,6 @@ def setup
assert_equal event.aggregation_key, [event.msg_title]
end

private

class FakeDatadogClient
def emit_event(event); end
end
Expand Down

0 comments on commit 5754e40

Please sign in to comment.