From 7dd06c5e87b46b037d2b1e393bd1f632bd30d509 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Thu, 18 Jan 2024 12:37:02 +0100 Subject: [PATCH] Fix error message when options method itself raises MethodError This would previously say that the option was unknown --- lib/httpx/options.rb | 11 +++++------ test/options_test.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/httpx/options.rb b/lib/httpx/options.rb index 8b888436..8ce2c453 100644 --- a/lib/httpx/options.rb +++ b/lib/httpx/options.rb @@ -342,12 +342,11 @@ module HTTPX defaults.each do |k, v| next if v.nil? - begin - value = __send__(:"option_#{k}", v) - instance_variable_set(:"@#{k}", value) - rescue NoMethodError - raise Error, "unknown option: #{k}" - end + option_method_name = :"option_#{k}" + raise Error, "unknown option: #{k}" unless respond_to?(option_method_name) + + value = __send__(option_method_name, v) + instance_variable_set(:"@#{k}", value) end end end diff --git a/test/options_test.rb b/test/options_test.rb index 50a477ac..db750062 100644 --- a/test/options_test.rb +++ b/test/options_test.rb @@ -10,6 +10,18 @@ class OptionsTest < Minitest::Test assert ex.message == "unknown option: foo", ex.message end + def test_options_no_method_error_during_validation + custom_opt_class = Class.new(Options) do + def option_foo(value) + raise TypeError, ":foo must be a Hash" unless value.is_a(Hash) + + value + end + end + ex = assert_raises(NoMethodError) { custom_opt_class.new(foo: "bar") } + assert_match("undefined method `is_a'", ex.message) + end + def test_options_body opt1 = Options.new assert opt1.body.nil?, "body shouldn't be set by default"