From 10598b5206a10776e69b6c62c7c487c09322290f Mon Sep 17 00:00:00 2001 From: Rahul Agrawal Date: Wed, 9 Mar 2022 11:36:32 -0800 Subject: [PATCH] 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 --- lib/sidekiq/job_util.rb | 5 +++-- test/test_client.rb | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/sidekiq/job_util.rb b/lib/sidekiq/job_util.rb index 5955b8a9..3190edee 100644 --- a/lib/sidekiq/job_util.rb +++ b/lib/sidekiq/job_util.rb @@ -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 diff --git a/test/test_client.rb b/test/test_client.rb index 2d87759c..bea76d2a 100644 --- a/test/test_client.rb +++ b/test/test_client.rb @@ -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