Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/smarter_csv/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ class HeaderSizeMismatch < SmarterCSVException; end
class IncorrectOption < SmarterCSVException; end
class ValidationError < SmarterCSVException; end
class DuplicateHeaders < SmarterCSVException; end
class MissingKeys < SmarterCSVException; end # previously known as MissingHeaders

class MissingKeys < SmarterCSVException # previously known as MissingHeaders
attr_reader :keys

def initialize(message, keys = [])
super(message)
@keys = keys
end
end

class NoColSepDetected < SmarterCSVException; end
class KeyMappingError < SmarterCSVException; end
class MalformedCSV < SmarterCSVException; end
Expand Down
2 changes: 1 addition & 1 deletion lib/smarter_csv/header_validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def check_required_headers(headers, options)
missing_keys = options[:required_keys].select { |k| !headers_set.include?(k) }

unless missing_keys.empty?
raise SmarterCSV::MissingKeys, "ERROR: missing attributes: #{missing_keys.join(',')}. Check `reader.headers` for original headers."
raise SmarterCSV::MissingKeys.new("ERROR: missing attributes: #{missing_keys.join(',')}. Check `reader.headers` for original headers.", missing_keys)
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions spec/features/header_handling/invalid_headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
SmarterCSV.process("#{fixture_path}/user_import.csv", options)
end.to raise_exception(
SmarterCSV::MissingKeys, "ERROR: missing attributes: employee_id. Check `reader.headers` for original headers."
)
) do |error|
expect(error.keys).to eq %i[employee_id]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add to its own test

end
end

it 'raises error on missing mapped headers' do
Expand All @@ -45,7 +47,9 @@
SmarterCSV.process("#{fixture_path}/user_import.csv", options)
end.to raise_exception(
SmarterCSV::MissingKeys, /ERROR: missing attributes: email/ # it was mapped, and is now missing
)
) do |error|
expect(error.keys).to eq %i[email]
end
end
end

Expand Down
Loading