From 3651e595befb121997b2755531861345a1aec6ca Mon Sep 17 00:00:00 2001 From: Henrique Aparecido Lavezzo Date: Thu, 5 Sep 2019 17:59:07 -0300 Subject: [PATCH] forwarding line number when coerced data raise error --- lib/estratto/data/base.rb | 1 - lib/estratto/data/coercer.rb | 10 +++++++--- lib/estratto/parser.rb | 4 ++-- lib/estratto/register.rb | 6 ++++-- spec/lib/estratto/data/coercer_spec.rb | 20 +++++++++++++++++--- spec/lib/estratto/register_spec.rb | 4 +++- 6 files changed, 33 insertions(+), 12 deletions(-) diff --git a/lib/estratto/data/base.rb b/lib/estratto/data/base.rb index 343fda4..1206ec8 100644 --- a/lib/estratto/data/base.rb +++ b/lib/estratto/data/base.rb @@ -13,7 +13,6 @@ def initialize(data, formats = {}) def coerce raise TypeCoercionNotFound end - end end end diff --git a/lib/estratto/data/coercer.rb b/lib/estratto/data/coercer.rb index 4405018..b8ac092 100644 --- a/lib/estratto/data/coercer.rb +++ b/lib/estratto/data/coercer.rb @@ -7,18 +7,22 @@ module Estratto module Data class InvalidCoercionType < StandardError; end - + class DataCoercionError < StandardError; end class Coercer - attr_reader :data, :type, :formats + attr_reader :data, :index, :type, :formats - def initialize(data:, type: 'String', formats: {}) + def initialize(data:, index:, type: 'String', formats: {}) @data = data + @index = index @type = type @formats = formats end def build target_coercer.coerce + rescue StandardError => error + raise DataCoercionError, + "Error when coercing #{data} on line #{index}, raising: #{error.message}" end def target_coercer diff --git a/lib/estratto/parser.rb b/lib/estratto/parser.rb index 81e10e6..bf6cdc2 100644 --- a/lib/estratto/parser.rb +++ b/lib/estratto/parser.rb @@ -11,10 +11,10 @@ def initialize(file_path, layout) end def perform - @data ||= raw_content.map do |line| + @data ||= raw_content.map.with_index do |line, index| register_layout = layout.register_fields_for(line[layout.prefix_range]) next if register_layout.nil? - Register.new(line, register_layout).refine + Register.new(line, index, register_layout).refine end.compact end diff --git a/lib/estratto/register.rb b/lib/estratto/register.rb index 7130569..e0c2d59 100644 --- a/lib/estratto/register.rb +++ b/lib/estratto/register.rb @@ -3,10 +3,11 @@ module Estratto class Register - attr_reader :line, :register_layout + attr_reader :line, :index, :register_layout - def initialize(line, register_layout) + def initialize(line, index, register_layout) @line = line + @index = index @register_layout = register_layout end @@ -22,6 +23,7 @@ def refine def coerced_data(range, type, formats) Estratto::Data::Coercer.new( + index: index, data: line[Estratto::Helpers::Range.for(range)], type: type, formats: formats || {} diff --git a/spec/lib/estratto/data/coercer_spec.rb b/spec/lib/estratto/data/coercer_spec.rb index f1697dc..e62272e 100644 --- a/spec/lib/estratto/data/coercer_spec.rb +++ b/spec/lib/estratto/data/coercer_spec.rb @@ -1,12 +1,26 @@ RSpec.describe Estratto::Data::Coercer do - subject { described_class.new(data: data, type: type, formats: formats) } + subject { described_class.new(data: data, index: index, type: type, formats: formats) } let(:data) { 'STAR PLATINUM' } + let(:index) { 1 } let(:type) { 'String' } let(:formats) { {} } describe '#build' do - it do - expect(subject.build).to eq('STAR PLATINUM') + context 'valid coercion build' do + it do + expect(subject.build).to eq('STAR PLATINUM') + end + end + + context 'invalid coercion build' do + let(:data) { '20190910' } + let(:type) { 'Date' } + it do + expect{ subject.build }.to raise_error( + Estratto::Data::DataCoercionError, + 'Error when coercing 20190910 on line 1, raising: invalid date' + ) + end end end diff --git a/spec/lib/estratto/register_spec.rb b/spec/lib/estratto/register_spec.rb index aa28e9b..b1781ac 100644 --- a/spec/lib/estratto/register_spec.rb +++ b/spec/lib/estratto/register_spec.rb @@ -1,5 +1,5 @@ RSpec.describe Estratto::Register do - subject { described_class.new(line, layout) } + subject { described_class.new(line, index, layout) } let(:line) { '42FIREFOX 57192 0.6' } let(:layout) do @@ -10,6 +10,8 @@ ] end + let(:index) { 1 } + describe '#refine' do it do expect(subject.refine).to eq(