Skip to content

Commit d061344

Browse files
committed
chore(settings): improve convert_value error handling
1 parent 5ecf0b3 commit d061344

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

lib/tiny_admin/settings.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,18 @@ def convert_value(key, value)
109109
value.each_key do |key2|
110110
path = [key, key2]
111111
if (DEFAULTS[path].is_a?(Class) || DEFAULTS[path].is_a?(Module)) && self[key][key2].is_a?(String)
112-
self[key][key2] = Object.const_get(self[key][key2])
112+
self[key][key2] = resolve_class(self[key][key2], setting: "#{key}.#{key2}")
113113
end
114114
end
115115
elsif value.is_a?(String) && (DEFAULTS[[key]].is_a?(Class) || DEFAULTS[[key]].is_a?(Module))
116-
self[key] = Object.const_get(self[key])
116+
self[key] = resolve_class(self[key], setting: key.to_s)
117117
end
118118
end
119+
120+
def resolve_class(class_name, setting:)
121+
Object.const_get(class_name)
122+
rescue NameError => e
123+
raise NameError, "TinyAdmin: invalid class '#{class_name}' for setting '#{setting}' - #{e.message}"
124+
end
119125
end
120126
end

spec/lib/tiny_admin/settings_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@
5959
settings[:root_path] = "/admin"
6060
expect(settings[:root_path]).to eq("/admin")
6161
end
62+
63+
it "raises a descriptive error for invalid class names" do
64+
expect { settings[:helper_class] = "NonExistent::Klass" }
65+
.to raise_error(NameError, /TinyAdmin: invalid class 'NonExistent::Klass' for setting 'helper_class'/)
66+
end
67+
68+
it "raises a descriptive error for invalid nested class names" do
69+
expect { settings[:authentication] = { plugin: "NonExistent::Auth" } }
70+
.to raise_error(NameError, /TinyAdmin: invalid class 'NonExistent::Auth' for setting 'authentication.plugin'/)
71+
end
6272
end
6373

6474
describe "dynamic option methods" do

0 commit comments

Comments
 (0)