Skip to content

Commit 7b22a5e

Browse files
committed
Add standard 'unsafe' fixes
1 parent 7f605ca commit 7b22a5e

31 files changed

+41
-45
lines changed

lib/honeybadger/backtrace.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def get_source(file, number, radius = 2)
104104
f.gets
105105
l += 1
106106
}
107-
return Hash[duration.times.map { (line = f.gets) ? [(l += 1), line] : nil }.compact]
107+
return duration.times.map { (line = f.gets) ? [(l += 1), line] : nil }.compact.to_h
108108
end
109109
else
110110
{}

lib/honeybadger/breadcrumbs/active_support.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def self.default_notifications
1919
transform: lambda do |data|
2020
if data[:sql]
2121
connection = data.delete(:connection)
22-
adapter = (connection && connection.adapter_name.downcase) || active_record_connection_db_config[:adapter]
22+
adapter = connection&.adapter_name&.downcase || active_record_connection_db_config[:adapter]
2323
data[:sql] = Util::SQL.obfuscate(data[:sql], adapter)
2424
end
2525
data

lib/honeybadger/cli/heroku.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def detect_heroku_app(prompt_on_default = true)
7878
apps.values.first
7979
else
8080
say "We detected a Heroku app named #{apps.values.first}. Do you want to load the config? (y/yes or n/no)"
81-
if STDIN.gets.chomp =~ /(y|yes)/i
81+
if $stdin.gets.chomp =~ /(y|yes)/i
8282
apps.values.first
8383
end
8484
end
@@ -87,7 +87,7 @@ def detect_heroku_app(prompt_on_default = true)
8787
apps.each_with_index { |a, i| say "\s\s#{i + 1}. #{a[1]}" }
8888
say "\s\s#{apps.size + 1}. Use default"
8989
say "Please select an option (1-#{apps.size + 1}):"
90-
apps.values[STDIN.gets.chomp.to_i - 1]
90+
apps.values[$stdin.gets.chomp.to_i - 1]
9191
end
9292
end
9393
end
@@ -107,7 +107,7 @@ def read_heroku_env(app = nil)
107107
cmd << "--app #{app}" if app
108108
output = run(cmd.join("\s"))
109109
return false unless $?.to_i == 0
110-
Hash[output.scan(/(HONEYBADGER_[^:]+):\s*(\S.*)\s*$/)]
110+
output.scan(/(HONEYBADGER_[^:]+):\s*(\S.*)\s*$/).to_h
111111
end
112112

113113
def set_env_from_heroku(app = nil)

lib/honeybadger/cli/test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Test
1212
exception_name = ENV["EXCEPTION"] || "HoneybadgerTestingException"
1313
Object.const_get(exception_name)
1414
rescue
15-
Object.const_set(exception_name, Class.new(Exception))
15+
Object.const_set(exception_name, Class.new(RuntimeError))
1616
end.new('Testing honeybadger via "honeybadger test". If you can see this, it works.')
1717

1818
class TestBackend
@@ -90,7 +90,7 @@ def test_exception_class
9090
exception_name = ENV["EXCEPTION"] || "HoneybadgerTestingException"
9191
Object.const_get(exception_name)
9292
rescue
93-
Object.const_set(exception_name, Class.new(Exception))
93+
Object.const_set(exception_name, Class.new(RuntimeError))
9494
end
9595

9696
def run_standalone_test

lib/honeybadger/config.rb

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
require "honeybadger/config/defaults"
1111
require "honeybadger/util/http"
1212
require "honeybadger/util/revision"
13-
require "honeybadger/logging"
1413

1514
module Honeybadger
1615
# @api private

lib/honeybadger/config/defaults.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ class Boolean; end
291291
},
292292
"exceptions.rescue_rake": {
293293
description: "Enable reporting exceptions in rake tasks.",
294-
default: !STDOUT.tty?,
294+
default: !$stdout.tty?,
295295
type: Boolean
296296
},
297297
"exceptions.notify_at_exit": {
@@ -519,6 +519,6 @@ class Boolean; end
519519
}
520520
}.freeze
521521

522-
DEFAULTS = Hash[OPTIONS.map { |k, v| [k, v[:default]] }].freeze
522+
DEFAULTS = OPTIONS.map { |k, v| [k, v[:default]] }.to_h.freeze
523523
end
524524
end

lib/honeybadger/config/env.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Honeybadger
44
class Config
55
module Env
66
CONFIG_KEY = /\AHONEYBADGER_(.+)\Z/.freeze
7-
CONFIG_MAPPING = Hash[DEFAULTS.keys.map { |k| [k.to_s.upcase.gsub(KEY_REPLACEMENT, "_"), k] }].freeze
7+
CONFIG_MAPPING = DEFAULTS.keys.map { |k| [k.to_s.upcase.gsub(KEY_REPLACEMENT, "_"), k] }.to_h.freeze
88
ARRAY_VALUES = Regexp.new('\s*,\s*').freeze
99
IGNORED_TYPES = Set[Hash]
1010

lib/honeybadger/config/ruby.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def mash?(method)
4646
end
4747

4848
def setter?(method_name)
49-
return false unless method_name.to_s =~ /=\z/
49+
return false unless method_name.to_s.end_with?("=")
5050
key = key(method_name)
5151
KEYS.any? { |k| k == key }
5252
end

lib/honeybadger/events_worker.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def shutdown(force = false)
8484
# Blocks until queue is processed up to this point in time.
8585
def flush
8686
mutex.synchronize do
87-
if thread && thread.alive?
87+
if thread&.alive?
8888
queue.push(FLUSH)
8989
queue.push(marker)
9090
marker.wait(mutex)

lib/honeybadger/instrumentation.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ def gauge(name, *args)
143143

144144
# @api private
145145
def extract_attributes(args)
146-
args.select { |a| a.is_a?(Hash) }.first || {}
146+
args.find { |a| a.is_a?(Hash) } || {}
147147
end
148148

149149
# @api private
150150
def extract_callable(args)
151-
args.select { |a| a.respond_to?(:call) }.first
151+
args.find { |a| a.respond_to?(:call) }
152152
end
153153
end
154154
end

lib/honeybadger/logging.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,15 @@ def format_message(msg)
116116
end
117117

118118
class ConfigLogger < StandardLogger
119-
LOCATE_CALLER_LOCATION = Regexp.new("#{Regexp.escape(__FILE__)}").freeze
119+
LOCATE_CALLER_LOCATION = Regexp.new(Regexp.escape(__FILE__).to_s).freeze
120120
CALLER_LOCATION = Regexp.new("#{Regexp.escape(File.expand_path("../../../", __FILE__))}/(.*)").freeze
121121

122122
INFO_SUPPLEMENT = " level=%s pid=%s".freeze
123123
DEBUG_SUPPLEMENT = " at=%s".freeze
124124

125125
def initialize(config, logger = Logger.new(nil))
126126
@config = config
127-
@tty = STDOUT.tty?
127+
@tty = $stdout.tty?
128128
@tty_level = @config.log_level(:"logging.tty_level")
129129
super(logger)
130130
end
@@ -169,7 +169,7 @@ def supplement(msg, severity)
169169
end
170170

171171
def caller_location
172-
if caller && caller.find { |l| l !~ LOCATE_CALLER_LOCATION && l =~ CALLER_LOCATION }
172+
if caller&.find { |l| l !~ LOCATE_CALLER_LOCATION && l =~ CALLER_LOCATION }
173173
Regexp.last_match(1)
174174
end
175175
end

lib/honeybadger/metrics_worker.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def shutdown(force = false)
5959
# Blocks until queue is processed up to this point in time.
6060
def flush
6161
mutex.synchronize do
62-
if thread && thread.alive?
62+
if thread&.alive?
6363
queue.push(marker)
6464
marker.wait(mutex)
6565
end

lib/honeybadger/notice.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,7 @@ def ignore_by_origin?
310310
end
311311

312312
def ignore_by_callbacks?
313-
config.exception_filter &&
314-
config.exception_filter.call(self)
313+
config.exception_filter&.call(self)
315314
end
316315

317316
# Gets a property named "attribute" of an exception, either from
@@ -489,7 +488,7 @@ def local_variables_from_exception(exception, config)
489488
end
490489
}
491490

492-
result_hash = Hash[results]
491+
result_hash = results.to_h
493492
request_sanitizer.sanitize(result_hash)
494493
end
495494

@@ -532,11 +531,11 @@ def unwrap_exception(exception)
532531
# Returns the Exception cause.
533532
def exception_cause(exception)
534533
e = exception
535-
if e.respond_to?(:cause) && e.cause && e.cause.is_a?(Exception)
534+
if e.respond_to?(:cause) && e.cause&.is_a?(Exception)
536535
e.cause
537-
elsif e.respond_to?(:original_exception) && e.original_exception && e.original_exception.is_a?(Exception)
536+
elsif e.respond_to?(:original_exception) && e.original_exception&.is_a?(Exception)
538537
e.original_exception
539-
elsif e.respond_to?(:continued_exception) && e.continued_exception && e.continued_exception.is_a?(Exception)
538+
elsif e.respond_to?(:continued_exception) && e.continued_exception&.is_a?(Exception)
540539
e.continued_exception
541540
end
542541
end

lib/honeybadger/plugins/breadcrumbs.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class RailsBreadcrumbs
7373
# @option notification_config [Proc] :transform A proc that accepts the data payload. The return value will replace the current data hash (optional)
7474
#
7575
def self.send_breadcrumb_notification(name, duration, notification_config, data = {})
76-
return if notification_config[:exclude_when] && notification_config[:exclude_when].call(data)
76+
return if notification_config[:exclude_when]&.call(data)
7777

7878
message =
7979
case (m = notification_config[:message])

lib/honeybadger/plugins/delayed_job/plugin.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Plugin < ::Delayed::Plugin
99
lifecycle.around(:invoke_job) do |job, &block|
1010
begin
1111
begin
12-
if job.payload_object.class.name == "ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper"
12+
if job.payload_object.instance_of?(::ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper)
1313
# buildin support for Rails 4.2 ActiveJob
1414
component = job.payload_object.job_data["job_class"]
1515
action = "perform"

lib/honeybadger/plugins/sidekiq.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def call(worker, msg, queue, &block)
3737
status = "failure"
3838
raise
3939
ensure
40-
context.merge!(duration: duration, status: status)
40+
context[:duration] = duration
41+
context[:status] = status
4142
if Honeybadger.config.load_plugin_insights_events?(:sidekiq)
4243
Honeybadger.event("perform.sidekiq", context)
4344
end

lib/honeybadger/singleton.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def notify(exception_or_opts = nil, opts = {}, **kwargs)
6868

6969
# @api private
7070
def load_plugins!
71-
Dir[File.expand_path("../plugins/*.rb", __FILE__)].each do |plugin|
71+
Dir[File.expand_path("../plugins/*.rb", __FILE__)].sort.each do |plugin|
7272
require plugin
7373
end
7474
Plugin.load!(config)

lib/honeybadger/util/http.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def http_connection
6666
def http_headers(headers = nil)
6767
{}.tap do |hash|
6868
hash.merge!(HEADERS)
69-
hash.merge!({"X-API-Key" => config[:api_key].to_s})
69+
hash["X-API-Key"] = config[:api_key].to_s
7070
hash.merge!(headers) if headers
7171
end
7272
end

lib/honeybadger/util/sanitizer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def sanitize(data, depth = 0, stack = nil, parents = [])
5959
return BASIC_OBJECT if basic_object?(data)
6060

6161
if recursive?(data)
62-
return RECURSION if stack && stack.include?(data.object_id)
62+
return RECURSION if stack&.include?(data.object_id)
6363

6464
stack = stack ? stack.dup : Set.new
6565
stack << data.object_id

lib/honeybadger/worker.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def shutdown(force = false)
7676
# Blocks until queue is processed up to this point in time.
7777
def flush
7878
mutex.synchronize do
79-
if thread && thread.alive?
79+
if thread&.alive?
8080
queue.push(marker)
8181
marker.wait(mutex)
8282
end

spec/fixtures/Rakefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ end
1919

2020
def stub_tty_output(value)
2121
if value
22-
def STDOUT.tty?
22+
def $stdout.tty?
2323
true
2424
end
2525
else
26-
def STDOUT.tty?
26+
def $stdout.tty?
2727
false
2828
end
2929
end

spec/fixtures/ruby_custom.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
api_key: "asdf",
55
backend: "debug",
66
debug: true,
7-
logger: Logger.new(STDOUT)
7+
logger: Logger.new($stdout)
88
})
99

1010
agent.notify(error_class: "CustomHoneybadgerException", error_message: "Test message")

spec/spec_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
nil
2020
end
2121

22-
Dir[File.expand_path("../support/**/*.rb", __FILE__)].each { |f| require f }
22+
Dir[File.expand_path("../support/**/*.rb", __FILE__)].sort.each { |f| require f }
2323

2424
TMP_DIR = Pathname.new(File.expand_path("../../tmp", __FILE__))
2525
FIXTURES_PATH = Pathname.new(File.expand_path("../fixtures/", __FILE__))

spec/support/backtraced_exception.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class BacktracedException < Exception
1+
class BacktracedException < RuntimeError
22
attr_accessor :backtrace
33
def initialize(opts)
44
@backtrace = opts[:backtrace]

spec/unit/honeybadger/events_worker_spec.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require "timecop"
2-
require "thread"
32

43
require "honeybadger/events_worker"
54
require "honeybadger/config"

spec/unit/honeybadger/logging_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
it { should respond_to severity }
6464
end
6565

66-
context "when not attached to terminal", unless: STDOUT.tty? do
66+
context "when not attached to terminal", unless: $stdout.tty? do
6767
LOG_SEVERITIES.each do |severity|
6868
it "delegates ##{severity} to configured logger" do
6969
# Debug is logged at the info level.
@@ -74,7 +74,7 @@
7474
end
7575
end
7676

77-
context "when attached to terminal", if: STDOUT.tty? do
77+
context "when attached to terminal", if: $stdout.tty? do
7878
[:debug, :info, :warn].each do |severity|
7979
it "suppresses ##{severity} from configured logger" do
8080
expect(logger).not_to receive(:add)

spec/unit/honeybadger/metrics_worker_spec.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require "timecop"
2-
require "thread"
32

43
require "honeybadger/metrics_worker"
54
require "honeybadger/config"

spec/unit/honeybadger/plugins/local_variables_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
describe Honeybadger::Plugins::LocalVariables::ExceptionExtension do
5757
subject do
5858
# Test in isolation rather than installing the plugin globally.
59-
Class.new(Exception) do |klass|
59+
Class.new(RuntimeError) do |klass|
6060
klass.send(:include, Honeybadger::Plugins::LocalVariables::ExceptionExtension)
6161
end.new
6262
end

spec/unit/honeybadger/rack/error_notifier_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require "honeybadger/rack/error_notifier"
22

3-
class BacktracedException < Exception
3+
class BacktracedException < RuntimeError
44
attr_accessor :backtrace
55
def initialize(opts)
66
@backtrace = opts[:backtrace]

spec/unit/honeybadger/worker_spec.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require "timecop"
2-
require "thread"
32

43
require "honeybadger/worker"
54
require "honeybadger/config"

tools/profile.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
RubyProf.start and Honeybadger::Agent.at_exit do
66
result = RubyProf.stop
77
printer = RubyProf::FlatPrinter.new(result)
8-
printer.print(STDOUT, {})
8+
printer.print($stdout, {})
99
end
1010

1111
1000.times do

0 commit comments

Comments
 (0)