Skip to content

Commit

Permalink
Move all registered lookup errors to main errors file
Browse files Browse the repository at this point in the history
  • Loading branch information
kspurgin committed Sep 19, 2023
1 parent b1eead8 commit a312492
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 32 deletions.
52 changes: 51 additions & 1 deletion lib/kiba/extend/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand Down
28 changes: 5 additions & 23 deletions lib/kiba/extend/registry/registered_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,22 @@ 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
# this registered file is being prepared
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

Expand Down
33 changes: 25 additions & 8 deletions spec/kiba/extend/registry/registered_lookup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a312492

Please sign in to comment.