Skip to content

Commit d2b2283

Browse files
committed
Added better error messaging. Shows full path of invalid param
1 parent a904fcc commit d2b2283

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

lib/rails_param/param.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ module Param
44
DEFAULT_PRECISION = 14
55

66
class InvalidParameterError < StandardError
7-
attr_accessor :param, :options
7+
attr_accessor :param, :options, :full_path_array
8+
9+
def initialize(msg)
10+
super(msg)
11+
@full_path_array = []
12+
end
813
end
914

1015
class MockController
@@ -13,6 +18,8 @@ class MockController
1318
end
1419

1520
def param!(name, type, options = {}, &block)
21+
evaluating_index = nil # used to keep track of array indexes
22+
1623
# keep index for validating elements if integer
1724
name = name.to_s unless name.is_a? Integer
1825

@@ -46,6 +53,7 @@ def param!(name, type, options = {}, &block)
4653
if type == Array
4754
params[name].each_with_index do |element, i|
4855
if element.is_a?(Hash)
56+
evaluating_index = i
4957
recurse element, &block
5058
else
5159
# supply index as key unless value is hash
@@ -59,6 +67,10 @@ def param!(name, type, options = {}, &block)
5967
params[name]
6068

6169
rescue InvalidParameterError => exception
70+
unless evaluating_index.nil?
71+
exception.full_path_array.unshift(evaluating_index)
72+
end
73+
exception.full_path_array.unshift(name)
6274
exception.param ||= name
6375
exception.options ||= options
6476
raise exception

spec/rails_param/param_spec.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,5 +477,70 @@ def self.validate!(value)
477477
end
478478
end
479479
end
480+
481+
describe 'exception full_path' do
482+
it 'has the proper full path with single param check' do
483+
allow(controller).to receive(:params).and_return({"price" => "abc"})
484+
begin
485+
controller.param! :price, Integer, required: true
486+
rescue RailsParam::Param::InvalidParameterError => e
487+
expect(e.full_path_array).to eql ["price"]
488+
end
489+
end
490+
491+
it 'has the proper full path when using arrays with hashes' do
492+
params = {
493+
'array' => [
494+
{'object'=>{ 'num' => '1', 'float' => '1.4' }},
495+
{'object'=>{ 'num' => '2', 'float' => 'abc' }}
496+
]
497+
}
498+
allow(controller).to receive(:params).and_return(params)
499+
begin
500+
controller.param! :array, Array do |a|
501+
a.param! :object, Hash do |h|
502+
h.param! :num, Integer, required: true
503+
h.param! :float, Float, required: true
504+
end
505+
end
506+
rescue RailsParam::Param::InvalidParameterError => e
507+
expect(e.full_path_array).to eql ["array", 1, "object", "float"]
508+
end
509+
end
510+
511+
it 'has the proper full path when using arrays with primitive types' do
512+
params = {
513+
'array' => ["abc"]
514+
}
515+
allow(controller).to receive(:params).and_return(params)
516+
begin
517+
controller.param! :array, Array do |array, index|
518+
array.param! index, Integer, :required => true
519+
end
520+
rescue RailsParam::Param::InvalidParameterError => e
521+
expect(e.full_path_array).to eql ['array', 0]
522+
end
523+
end
524+
525+
it 'has the proper full path when using hashes' do
526+
params = {
527+
'hash' => {
528+
'hash2'=> {'integers' => ['123', 'abc'] }
529+
}
530+
}
531+
allow(controller).to receive(:params).and_return(params)
532+
begin
533+
controller.param! :hash, Hash do |a|
534+
a.param! :hash2, Hash do |h|
535+
h.param! :integers, Array do |array, index|
536+
array.param! index, Integer
537+
end
538+
end
539+
end
540+
rescue RailsParam::Param::InvalidParameterError => e
541+
expect(e.full_path_array).to eql ["hash", "hash2", "integers", 1]
542+
end
543+
end
544+
end
480545
end
481546
end

0 commit comments

Comments
 (0)