diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 566eec83..56b6555a 100644 --- a/.rubocop_todo.yml +++ b/.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 @@ -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 @@ -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: @@ -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 @@ -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: @@ -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' diff --git a/examples/sinatra/sinatra_app.rb b/examples/sinatra/sinatra_app.rb index 65d31bbf..6e0dec51 100644 --- a/examples/sinatra/sinatra_app.rb +++ b/examples/sinatra/sinatra_app.rb @@ -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 mailcatcher.' diff --git a/lib/exception_notification/resque.rb b/lib/exception_notification/resque.rb index 40b9743b..288f117d 100644 --- a/lib/exception_notification/resque.rb +++ b/lib/exception_notification/resque.rb @@ -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 }) diff --git a/lib/exception_notifier/campfire_notifier.rb b/lib/exception_notifier/campfire_notifier.rb index bf7c743f..881f8965 100644 --- a/lib/exception_notifier/campfire_notifier.rb +++ b/lib/exception_notifier/campfire_notifier.rb @@ -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 diff --git a/lib/exception_notifier/datadog_notifier.rb b/lib/exception_notifier/datadog_notifier.rb index 1697474d..9fc3dbf8 100644 --- a/lib/exception_notifier/datadog_notifier.rb +++ b/lib/exception_notifier/datadog_notifier.rb @@ -22,8 +22,6 @@ def datadog_event(exception, options = {}) ).event end - private - class DatadogExceptionEvent include ExceptionNotifier::BacktraceCleaner diff --git a/lib/exception_notifier/email_notifier.rb b/lib/exception_notifier/email_notifier.rb index c4eaeac3..ede9b1cb 100644 --- a/lib/exception_notifier/email_notifier.rb +++ b/lib/exception_notifier/email_notifier.rb @@ -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})" @@ -141,7 +141,8 @@ 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 @@ -149,7 +150,9 @@ def initialize(options) 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 diff --git a/lib/exception_notifier/hipchat_notifier.rb b/lib/exception_notifier/hipchat_notifier.rb index 9d851037..b45cc588 100644 --- a/lib/exception_notifier/hipchat_notifier.rb +++ b/lib/exception_notifier/hipchat_notifier.rb @@ -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 } diff --git a/lib/exception_notifier/modules/error_grouping.rb b/lib/exception_notifier/modules/error_grouping.rb index 4f6c2cc2..3aefff0e 100644 --- a/lib/exception_notifier/modules/error_grouping.rb +++ b/lib/exception_notifier/modules/error_grouping.rb @@ -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 diff --git a/lib/exception_notifier/slack_notifier.rb b/lib/exception_notifier/slack_notifier.rb index 8202ef52..fe9ab7a1 100644 --- a/lib/exception_notifier/slack_notifier.rb +++ b/lib/exception_notifier/slack_notifier.rb @@ -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 @@ -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? @@ -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 diff --git a/lib/exception_notifier/sns_notifier.rb b/lib/exception_notifier/sns_notifier.rb index b1d67332..fbd7f72e 100644 --- a/lib/exception_notifier/sns_notifier.rb +++ b/lib/exception_notifier/sns_notifier.rb @@ -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 diff --git a/lib/generators/exception_notification/install_generator.rb b/lib/generators/exception_notification/install_generator.rb index d4f78b38..52d5b2f0 100644 --- a/lib/generators/exception_notification/install_generator.rb +++ b/lib/generators/exception_notification/install_generator.rb @@ -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 diff --git a/lib/generators/exception_notification/templates/exception_notification.rb b/lib/generators/exception_notification/templates/exception_notification.rb.erb similarity index 100% rename from lib/generators/exception_notification/templates/exception_notification.rb rename to lib/generators/exception_notification/templates/exception_notification.rb.erb diff --git a/test/exception_notifier/campfire_notifier_test.rb b/test/exception_notifier/campfire_notifier_test.rb index 2594f7cc..e3065a97 100644 --- a/test/exception_notifier/campfire_notifier_test.rb +++ b/test/exception_notifier/campfire_notifier_test.rb @@ -101,7 +101,7 @@ def fake_notification def fake_exception 5 / 0 - rescue Exception => e + rescue StandardError => e e end diff --git a/test/exception_notifier/datadog_notifier_test.rb b/test/exception_notifier/datadog_notifier_test.rb index 78cc7804..556d1098 100644 --- a/test/exception_notifier/datadog_notifier_test.rb +++ b/test/exception_notifier/datadog_notifier_test.rb @@ -92,8 +92,6 @@ def setup assert_equal event.aggregation_key, [event.msg_title] end - private - class FakeDatadogClient def emit_event(event); end end diff --git a/test/exception_notifier/hipchat_notifier_test.rb b/test/exception_notifier/hipchat_notifier_test.rb index c69d7ab0..b5e944bf 100644 --- a/test/exception_notifier/hipchat_notifier_test.rb +++ b/test/exception_notifier/hipchat_notifier_test.rb @@ -197,13 +197,13 @@ def fake_body def fake_exception 5 / 0 - rescue Exception => e + rescue StandardError => e e end def fake_exception_with_html_characters raise StandardError, 'an error with characters' - rescue Exception => e + rescue StandardError => e e end diff --git a/test/exception_notifier/irc_notifier_test.rb b/test/exception_notifier/irc_notifier_test.rb index dacb989d..32d0095e 100644 --- a/test/exception_notifier/irc_notifier_test.rb +++ b/test/exception_notifier/irc_notifier_test.rb @@ -102,11 +102,13 @@ class IrcNotifierTest < ActiveSupport::TestCase prefix: '[test notification]' } - CarrierPigeon.expects(:send).with(has_entries( - ssl: true, - join: true, - notice: true - )) do |v| + entries = { + ssl: true, + join: true, + notice: true + } + + CarrierPigeon.expects(:send).with(has_entries(entries)) do |v| /\[test notification\]/.match(v[:message]) end @@ -125,7 +127,7 @@ class IrcNotifierTest < ActiveSupport::TestCase def fake_exception 5 / 0 - rescue Exception => e + rescue StandardError => e e end diff --git a/test/exception_notifier/modules/error_grouping_test.rb b/test/exception_notifier/modules/error_grouping_test.rb index 56270e07..1365cd0b 100644 --- a/test/exception_notifier/modules/error_grouping_test.rb +++ b/test/exception_notifier/modules/error_grouping_test.rb @@ -155,7 +155,7 @@ module TestModule end test 'use specified trigger in .send_notification?' do - trigger = proc { |_exception, count| count % 4 == 0 } + trigger = proc { |_exception, count| (count % 4).zero? } TestModule.stubs(:notification_trigger).returns(trigger) count = 16 diff --git a/test/exception_notifier/slack_notifier_test.rb b/test/exception_notifier/slack_notifier_test.rb index c4636674..69ca489c 100644 --- a/test/exception_notifier/slack_notifier_test.rb +++ b/test/exception_notifier/slack_notifier_test.rb @@ -173,7 +173,7 @@ def setup def fake_exception 5 / 0 - rescue Exception => e + rescue StandardError => e e end diff --git a/test/exception_notifier/sns_notifier_test.rb b/test/exception_notifier/sns_notifier_test.rb index a68813ab..8d3ae40a 100644 --- a/test/exception_notifier/sns_notifier_test.rb +++ b/test/exception_notifier/sns_notifier_test.rb @@ -100,7 +100,7 @@ class ExamplesController < ActionController::Base; end def fake_exception 1 / 0 - rescue Exception => e + rescue StandardError => e e end diff --git a/test/exception_notifier/webhook_notifier_test.rb b/test/exception_notifier/webhook_notifier_test.rb index 010f0a52..26c6903f 100644 --- a/test/exception_notifier/webhook_notifier_test.rb +++ b/test/exception_notifier/webhook_notifier_test.rb @@ -86,7 +86,7 @@ def fake_params def fake_exception @fake_exception ||= begin 5 / 0 - rescue Exception => e + rescue StandardError => e e end end