Skip to content

Commit 2092168

Browse files
authored
Fixes TypeError being raised instead of InvalidParameterError. (#54)
1 parent 5ecd472 commit 2092168

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/rails_param/param.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def coerce(param, type, options = {})
126126
return BigDecimal(param, (options[:precision] || DEFAULT_PRECISION))
127127
end
128128
return nil
129-
rescue ArgumentError
129+
rescue ArgumentError, TypeError
130130
raise InvalidParameterError, "'#{param}' is not a valid #{type}"
131131
end
132132
end

spec/rails_param/param_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ def params;
8989
allow(controller).to receive(:params).and_return({ "foo" => "notInteger" })
9090
expect { controller.param! :foo, Integer }.to raise_error(RailsParam::Param::InvalidParameterError)
9191
end
92+
93+
it "will raise InvalidParameterError if the value is a boolean" do
94+
allow(controller).to receive(:params).and_return({ "foo" => true })
95+
expect { controller.param! :foo, Integer }.to raise_error(RailsParam::Param::InvalidParameterError)
96+
end
9297
end
9398

9499
describe "Float" do
@@ -102,6 +107,11 @@ def params;
102107
allow(controller).to receive(:params).and_return({ "foo" => "notFloat" })
103108
expect { controller.param! :foo, Float }.to raise_error(RailsParam::Param::InvalidParameterError)
104109
end
110+
111+
it "will raise InvalidParameterError if the value is a boolean" do
112+
allow(controller).to receive(:params).and_return({ "foo" => true })
113+
expect { controller.param! :foo, Float }.to raise_error(RailsParam::Param::InvalidParameterError)
114+
end
105115
end
106116

107117
describe "Array" do
@@ -295,6 +305,7 @@ def params;
295305
allow(controller).to receive(:params).and_return({ "foo" => "1111" })
296306
expect { controller.param! :foo, :boolean }.to raise_error(RailsParam::Param::InvalidParameterError)
297307
end
308+
298309
it "set default boolean" do
299310
allow(controller).to receive(:params).and_return({})
300311
controller.param! :foo, :boolean, default: false

0 commit comments

Comments
 (0)