lib/chamber/settings.rb in chamber-3.0.0rc2 vs lib/chamber/settings.rb in chamber-3.0.0

- old
+ new

@@ -1,7 +1,9 @@ # frozen_string_literal: true +require 'chamber/errors/missing_index' +require 'chamber/errors/missing_setting' require 'chamber/namespace_set' require 'chamber/filters/namespace_filter' require 'chamber/filters/encryption_filter' require 'chamber/filters/decryption_filter' require 'chamber/filters/environment_filter' @@ -234,25 +236,39 @@ data == other.data && namespaces == other.namespaces end def [](key) - fail ::ArgumentError, 'Bracket access with anything other than a String is unsupported.' unless key.is_a?(::String) + fail ::Chamber::Errors::InvalidKeyType, 'Bracket access with anything other than a String is unsupported.' unless key.is_a?(::String) data.fetch(key) + rescue ::KeyError => error + missing_setting_name = error.message.gsub(/.*key not found: "([^"]+)".*/, '\1') + + raise ::Chamber::Errors::MissingSetting.new(missing_setting_name, []) end def dig!(*args) args.inject(data) do |data_value, bracket_value| key = bracket_value.is_a?(::Symbol) ? bracket_value.to_s : bracket_value data_value.fetch(key) end + rescue ::KeyError => error + missing_setting_name = error.message.gsub(/.*key not found: "([^"]+)".*/, '\1') + + raise ::Chamber::Errors::MissingSetting.new(missing_setting_name, args) + rescue ::IndexError => error + missing_index_number = error.message.gsub(/.*index (\d+) outside.*/, '\1') + + raise ::Chamber::Errors::MissingIndex.new(missing_index_number, args) end def dig(*args) dig!(*args) - rescue ::KeyError, ::IndexError # rubocop:disable Lint/ShadowedException + rescue ::Chamber::Errors::MissingSetting, + ::Chamber::Errors::MissingIndex + nil end def securable Settings.new(**metadata.merge(