diff --git a/lib/kiba/extend/error.rb b/lib/kiba/extend/error.rb index 15f4f54b..98e0e472 100644 --- a/lib/kiba/extend/error.rb +++ b/lib/kiba/extend/error.rb @@ -47,9 +47,33 @@ class IterativeCleanupSettingUndefinedError < StandardError include Kiba::Extend::ErrMod end + class JobCannotBeUsedAsLookupError < TypeError + include Kiba::Extend::ErrMod + def initialize(key, klass, for_job) + @key = key + @klass = klass + @for_job = for_job + @msg = ":#{key} cannot be used as a lookup in :#{for_job} because "\ + "its src_class (#{klass}) does not include "\ + "Kiba::Extend::Soures::Lookupable" + super(msg) + end + + def formatted + <<~STR + JOB FAILED: LOOKUP FILE SETUP ERROR FOR: #{for_job} + #{msg} + STR + end + + private + + attr_reader :key, :klass, :for_job, :type, :msg + end + # Exception raised if {Kiba::Extend::FileRegistry} contains no lookup # key for file - class NoLookupKeyError < NameError + class NoLookupOnError < NameError include Kiba::Extend::ErrMod # @param filekey [Symbol] key not found in # {Kiba::Extend::FileRegistry} @@ -74,6 +98,32 @@ def formatted attr_reader :filekey, :for_job end + # Exception raised if the lookup key value for the file is not a Symbol + class NonSymbolLookupOnError < TypeError + include Kiba::Extend::ErrMod + # @param filekey [Symbol] registry entry key having the non-symbol + # `lookup_on` value + def initialize(filekey, for_job) + @filekey = filekey + @for_job = for_job + @msg = "The `lookup_on` value in the registry entry hash for "\ + ":#{filekey} is not a Ruby Symbol. "\ + "Prepend a : to the field name to fix." + super(msg) + end + + def formatted + <<~STR + JOB FAILED: LOOKUP FILE SETUP ERROR FOR: #{for_job} + #{msg} + STR + end + + private + + attr_reader :filekey, :for_job, :msg + end + class ProjectSettingUndefinedError < StandardError include Kiba::Extend::ErrMod end diff --git a/lib/kiba/extend/registry/registered_lookup.rb b/lib/kiba/extend/registry/registered_lookup.rb index 7ad6fdb2..2f211d2b 100644 --- a/lib/kiba/extend/registry/registered_lookup.rb +++ b/lib/kiba/extend/registry/registered_lookup.rb @@ -16,26 +16,6 @@ module Registry class RegisteredLookup < RegisteredFile include RequirableFile - class CannotBeUsedAsLookupError < TypeError - include Kiba::Extend::ErrMod - def initialize(klass) - super("The result of a registry entry with a #{klass} "\ - "dest_class cannot be used as source file in a job") - end - end - - # Exception raised if the lookup key value for the file is not a Symbol - class NonSymbolLookupKeyError < TypeError - include Kiba::Extend::ErrMod - # @param filekey [Symbol] key not found in - # {Kiba::Extend::FileRegistry} - def initialize(filekey) - msg = "Lookup key found for :#{filekey} is not a Ruby Symbol. "\ - "Prepend a : to the field name to fix." - super(msg) - end - end - # @param key [Symbol] file key from {FileRegistry} data hash # @param data [Hash] file data from {FileRegistry} # @param for_job [Symbol] registry entry job key of the job for which @@ -43,13 +23,15 @@ def initialize(filekey) def initialize(key:, data:, for_job:) super unless src_class.respond_to?(:is_lookupable?) - fail CannotBeUsedAsLookupError.new(src_class) + fail Kiba::Extend::JobCannotBeUsedAsLookupError.new( + key, src_class, for_job + ) end unless lookup_on - fail Kiba::Extend::NoLookupKeyError.new(@key, for_job) + fail Kiba::Extend::NoLookupOnError.new(key, for_job) end unless lookup_on.is_a?(Symbol) - fail NonSymbolLookupKeyError, @key + fail Kiba::Extend::NonSymbolLookupOnError.new(key, for_job) end end diff --git a/spec/kiba/extend/registry/registered_lookup_spec.rb b/spec/kiba/extend/registry/registered_lookup_spec.rb index afcfa5f7..efe29515 100644 --- a/spec/kiba/extend/registry/registered_lookup_spec.rb +++ b/spec/kiba/extend/registry/registered_lookup_spec.rb @@ -14,17 +14,28 @@ Kiba::Extend::Registry::RegisteredLookup.new( key: filekey, data: Kiba::Extend::Registry::FileRegistryEntry.new(data), - for_job: :foo + for_job: :bar ) end - context "when called without lookup key" do + context "when called without lookup_on value" do let(:data) { {path: path} } - it "raises NoLookupKeyError" do + it "raises NoLookupOnError" do expect do lookup end.to raise_error( - Kiba::Extend::NoLookupKeyError + Kiba::Extend::NoLookupOnError + ) + end + end + + context "when called with non-Symbol lookup_on value" do + let(:data) { {path: path, lookup_on: "bar"} } + it "raises NonSymbolLookupOnError" do + expect do + lookup + end.to raise_error( + Kiba::Extend::NonSymbolLookupOnError ) end end @@ -39,9 +50,12 @@ } end - it "raises CannotBeUsedAsLookupError" do + it "raises JobCannotBeUsedAsLookupError" do expect { lookup }.to raise_error( - Kiba::Extend::Registry::RegisteredLookup::CannotBeUsedAsLookupError + Kiba::Extend::JobCannotBeUsedAsLookupError, + ":fkey cannot be used as a lookup in :bar because its src_class "\ + "(Kiba::Extend::Sources::Marc) does not include "\ + "Kiba::Extend::Soures::Lookupable" ) end end @@ -56,9 +70,12 @@ } end - it "raises CannotBeUsedAsLookupError" do + it "raises JobCannotBeUsedAsLookupError" do expect { lookup }.to raise_error( - Kiba::Extend::Registry::RegisteredLookup::CannotBeUsedAsLookupError + Kiba::Extend::JobCannotBeUsedAsLookupError, + ":fkey cannot be used as a lookup in :bar because its src_class "\ + "(Kiba::Extend::Sources::Marc) does not include "\ + "Kiba::Extend::Soures::Lookupable" ) end end