Browse files

Punt error accumulation for now; parses shall throw promptly

  • Loading branch information...
1 parent 0a9ff21 commit 3f3eb04ed71edab386eb13a3a772764959c7bc25 @sorear committed Mar 18, 2013
Showing with 9 additions and 49 deletions.
  1. +0 −9 lib/X12/Schema/SyntaxError.pm
  2. +3 −27 lib/X12/Schema/TokenSource.pm
  3. +6 −13 t/02-source.t
View
9 lib/X12/Schema/SyntaxError.pm
@@ -1,9 +0,0 @@
-package X12::Schema::SyntaxError;
-
-use Moose;
-use namespace::autoclean;
-
-has code => (is => 'ro', isa => 'Str', required => 1);
-has message => (is => 'ro', isa => 'Str', required => 1);
-
-__PACKAGE__->meta->make_immutable;
View
30 lib/X12/Schema/TokenSource.pm
@@ -3,8 +3,6 @@ package X12::Schema::TokenSource;
use Moose;
use namespace::autoclean;
-use X12::Schema::SyntaxError;
-
has buffer => (is => 'bare', isa => 'Str', default => '');
has filler => (is => 'ro', isa => 'CodeRef', default => sub { sub { 0 } });
@@ -15,14 +13,10 @@ has _suffix_re => (is => 'bare', isa =>'RegexpRef', init_arg => undef);
has [qw(_segment_term _component_sep _repeat_sep _segment_term_suffix _element_sep)] => (is => 'bare', isa => 'Str', init_arg => undef);
has segment_counter => (is => 'rw', isa => 'Int', default => 0);
-has errors => (is => 'ro', isa => 'ArrayRef[X12::Schema::SyntaxError]', default => sub { [ ] }, init_arg => undef);
-has _fatal_error => (is => 'bare', isa => 'Bool');
sub _parse {
my ($self) = @_;
- return if $self->{_fatal_error};
-
if (substr($self->{buffer},0,3) eq 'ISA') {
return if length($self->{buffer}) < 109; # ISA itself is 106 chars, but we need to see the beginning of GE to get the separator
@@ -46,9 +40,7 @@ sub _parse {
}
unless ($self->{_segment_re}) {
- $self->{_fatal_error} = 1;
- $self->logerror( 'isa_not_first' );
- return;
+ die "ISA segment must appear first\n";
}
# DIVERSITY: UNx, BIN, BDS segments, maybe X12.58 but I don't have a clear idea what that entails
@@ -75,7 +67,7 @@ sub set_delims {
my %u;
if (grep( defined $_ && $u{$_}++, @$self{ qw( _element_sep _repeat_sep _component_sep _segment_term ) } )) {
- $self->logerror( 'nonunique_delims' );
+ die "Delimiters are not unique\n";
}
my $t = $self->{_segment_term};
@@ -120,23 +112,7 @@ sub expect_eof {
1 until length($self->{buffer}) or !$self->filler->();
- $self->logerror('garbage') if length $self->{buffer};
-}
-
-my %errdefs = (
- garbage => { message => 'Trailing garbage at EOF' },
- isa_not_first => { message => 'EDI file must start with an ISA segment' },
- nonunique_delims => { message => 'Delimiters are not unique' },
-);
-
-sub logerror {
- my ($self, $code) = splice @_,0,2;
-
- my $def = $errdefs{$code} or Carp::croak "Undefined error $code";
-
- push @{ $self->errors }, X12::Schema::SyntaxError->new(
- code => $code, message => $def->{message}
- );
+ die "Unexpected data after transmission\n" if length($self->{buffer});
}
__PACKAGE__->meta->make_immutable;
View
19 t/02-source.t
@@ -1,6 +1,7 @@
use strict;
use warnings;
-use Test::More tests => 17;
+use Test::More tests => 16;
+use Test::Exception;
BEGIN { use_ok('X12::Schema::TokenSource') or die }
@@ -25,29 +26,21 @@ is_deeply $O->peek_code, '', 'peek_code on empty is ""';
is $O->segment_counter, 2, "segment_counter counts actually used segments";
-$O->expect_eof;
-is_deeply $O->errors, [], "no errors";
+lives_ok { $O->expect_eof } 'nothing trailing';
$O = X12::Schema::TokenSource->new(buffer => "${ISA2}\r\nFOO*BUR/S*A/B~\r\nK");
$O->get;
is_deeply $O->get, [ [['FOO']], [['B'],['R','S']], [['A','B']] ], "parsing test for 'advanced features'";
-is_deeply $O->errors, [], "no errors";
-$O->expect_eof;
-is_deeply [map $_->code, @{$O->errors}], ['garbage'], 'trailing garbage noticed by expect_eof';
+throws_ok { $O->expect_eof } qr/Unexpected/, 'trailing garbage noticed by expect_eof';
$O = X12::Schema::TokenSource->new(buffer => "${ISA3}\r\nFOO*BUR/S*A/B~\r\n");
-$O->get;
-is_deeply [map $_->code, @{$O->errors}], ['nonunique_delims'], 'delimiter collision noticed';
+throws_ok { $O->get } qr/unique/, 'delimiter collision noticed';
$O = X12::Schema::TokenSource->new(buffer => "FOO*BUR/S*A/B~\r\n");
-$O->get;
-$O->get;
-
-is_deeply [map $_->code, @{$O->errors}], ['isa_not_first'], 'missing ISA noticed';
-
+throws_ok { $O->get } qr/ISA/, 'missing ISA noticed';

0 comments on commit 3f3eb04

Please sign in to comment.