1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Surface exact job class name in complex argument error message (#5235)

* Surface exact job class name in complex argument error message

* Update job_util.rb

* Update param for ActiveJob arg serialization failure

Co-authored-by: Mike Perham <mperham@gmail.com>
This commit is contained in:
Rahul Agrawal 2022-03-09 11:36:32 -08:00 committed by GitHub
parent 261368db6a
commit 10598b5206
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View file

@ -13,15 +13,16 @@ module Sidekiq
raise(ArgumentError, "Job 'at' must be a Numeric timestamp: `#{item}`") if item.key?("at") && !item["at"].is_a?(Numeric)
raise(ArgumentError, "Job tags must be an Array: `#{item}`") if item["tags"] && !item["tags"].is_a?(Array)
job_class = item["wrapped"] || item["class"]
if Sidekiq.options[:on_complex_arguments] == :raise
msg = <<~EOM
Job arguments to #{item["class"]} must be native JSON types, see https://github.com/mperham/sidekiq/wiki/Best-Practices.
Job arguments to #{job_class} must be native JSON types, see https://github.com/mperham/sidekiq/wiki/Best-Practices.
To disable this error, remove `Sidekiq.strict_args!` from your initializer.
EOM
raise(ArgumentError, msg) unless json_safe?(item)
elsif Sidekiq.options[:on_complex_arguments] == :warn
Sidekiq.logger.warn <<~EOM unless json_safe?(item)
Job arguments to #{item["class"]} do not serialize to JSON safely. This will raise an error in
Job arguments to #{job_class} do not serialize to JSON safely. This will raise an error in
Sidekiq 7.0. See https://github.com/mperham/sidekiq/wiki/Best-Practices or raise an error today
by calling `Sidekiq.strict_args!` during Sidekiq initialization.
EOM

View file

@ -2,6 +2,7 @@
require_relative "helper"
require "sidekiq/api"
require "sidekiq/rails"
describe Sidekiq::Client do
describe "errors" do
@ -220,7 +221,7 @@ describe Sidekiq::Client do
describe "worker that takes deep, nested structures" do
it "raises an error on JSON-unfriendly structures" do
assert_raises ArgumentError do
error = assert_raises ArgumentError do
InterestingWorker.perform_async(
{
"foo" => [:a, :b, :c],
@ -228,6 +229,26 @@ describe Sidekiq::Client do
}
)
end
assert_match /Job arguments to InterestingWorker/, error.message
end
end
describe 'ActiveJob with non-native json types' do
before do
ActiveJob::Base.queue_adapter = :sidekiq
ActiveJob::Base.logger = nil
end
class TestActiveJob < ActiveJob::Base
def perform(arg)
end
end
it 'raises error with correct class name' do
error = assert_raises ArgumentError do
TestActiveJob.perform_later(1.1212.to_d)
end
assert_match /Job arguments to TestActiveJob/, error.message
end
end
end