From 8bfee41e214b015fe6eb8ef0c185859f5b6401a7 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 14 Aug 2010 05:18:31 +0200 Subject: Add remaining error classes, sort of --- lib/WWW/Efa.pm | 44 ++++++++++++++++++--------- lib/WWW/Efa/Error/Ambiguous.pm | 69 ++++++++++++++++++++++++++++++++++++++++++ lib/WWW/Efa/Error/Backend.pm | 64 +++++++++++++++++++++++---------------- lib/WWW/Efa/Error/NoData.pm | 50 ++++++++++++++++++++++++++++++ lib/WWW/Efa/Error/Setup.pm | 1 - 5 files changed, 186 insertions(+), 42 deletions(-) create mode 100644 lib/WWW/Efa/Error/Ambiguous.pm create mode 100644 lib/WWW/Efa/Error/NoData.pm (limited to 'lib') diff --git a/lib/WWW/Efa.pm b/lib/WWW/Efa.pm index 58d55bd..1d593a4 100644 --- a/lib/WWW/Efa.pm +++ b/lib/WWW/Efa.pm @@ -40,8 +40,10 @@ use 5.010; use base 'Exporter'; use XML::LibXML; -use WWW::Efa::Error::Setup; +use WWW::Efa::Error::Ambiguous; use WWW::Efa::Error::Backend; +use WWW::Efa::Error::NoData; +use WWW::Efa::Error::Setup; use WWW::Mechanize; our @EXPORT_OK = (); @@ -213,7 +215,7 @@ sub parse_initial { my $con_part = 0; my $con_no; - my $cons; + my $cons = []; my $xp_td = XML::LibXML::XPathExpression->new('//table//table/tr/td'); my $xp_img = XML::LibXML::XPathExpression->new('./img'); @@ -253,14 +255,7 @@ sub parse_initial { } } - if (defined $con_no) { - return $cons; - } - else { - return WWW::Efa::Error::Backend->new( - 'no data' - ); - } + return $cons; } sub parse_pretty { @@ -465,6 +460,7 @@ Parse the B reply sub parse { my ($self) = @_; + my $err; my $tree = XML::LibXML->load_html( string => $self->{'html_reply'}, @@ -472,10 +468,28 @@ sub parse { my $raw_cons = parse_initial($tree); + if (@{$raw_cons} == 0) { + $self->{'error'} = WWW::Efa::Error::NoData->new(); + } + for my $raw_con (@{$raw_cons}) { push(@{$self->{'connections'}}, parse_pretty($raw_con)); } $self->{'tree'} = $tree; + + if ($err = $self->check_ambiguous()) { + $self->{'error'} = $err; + return $err; + } + elsif ($err = $self->check_no_connections()) { + $self->{'error'} = $err; + return $err; + } + elsif ($self->{'error'}) { + return $self->{'error'}; + } + + return $self->{'error'}; } sub check_ambiguous { @@ -487,15 +501,16 @@ sub check_ambiguous { foreach my $select (@{$tree->findnodes($xp_select)}) { - my @possible = ($select->getAttribute('name')); + my $post_key = $select->getAttribute('name'); + my @possible; foreach my $val ($select->findnodes($xp_option)) { push(@possible, $val->textContent()); } - return WWW::Efa::Error::Backend->new( - 'ambiguous', - \@possible + return WWW::Efa::Error::Ambiguous->new( + $post_key, + @possible, ); } } @@ -511,7 +526,6 @@ sub check_no_connections { if ($err_node) { return WWW::Efa::Error::Backend->new( - 'error', $err_node->parentNode()->parentNode()->textContent() ); } diff --git a/lib/WWW/Efa/Error/Ambiguous.pm b/lib/WWW/Efa/Error/Ambiguous.pm new file mode 100644 index 0000000..e738a63 --- /dev/null +++ b/lib/WWW/Efa/Error/Ambiguous.pm @@ -0,0 +1,69 @@ +package WWW::Efa::Error::Ambiguous; + +=head1 NAME + +WWW::Efa::Error::Ambiguous - WWW::Efa error, ambiguous to/from/via input + +=head1 SYNOPSIS + + use WWW::Efa::Error::Ambiguous; + + my $error = WWW::Efa::Error::Ambiguous->new( + 'name_origin', 'Bredeney', 'Bredeney Friedhof' + ); + + die $error->as_string(); + # WWW::Efa error: ambiguous input for name_origin: + # Bredeney + # Bredeney Friedhof + +=head1 DESCRIPTION + +Class for all WWW::Efa-internal errors occuring during initialization. Usually +caused by missing or invalid setup arguments. + +=cut + +use strict; +use warnings; +use 5.010; + +use base 'Exporter'; + +our @EXPORT_OK = qw{}; +our @ISA = ('WWW::Efa::Error'); + +sub new { + my ($obj, $key, @possible) = @_; + my $ref = {}; + + $ref->{'key'} = $key; + $ref->{'possible'} = \@possible; + + return bless($ref, $obj); +} + +=head1 METHODS + +=head2 $error->as_string() + +Return the error as string, can directly be displayed to the user + +=cut + +sub as_string { + my ($self) = @_; + + my $ret = sprintf( + "WWW::Efa error: ambiguous input for %s:\n", + $self->{'key'}, + ); + + foreach my $value (@{$self->{'possible'}}) { + $ret .= "\t$value\n"; + } + + return $ret; +} + +1; diff --git a/lib/WWW/Efa/Error/Backend.pm b/lib/WWW/Efa/Error/Backend.pm index b6ffa75..930fed5 100644 --- a/lib/WWW/Efa/Error/Backend.pm +++ b/lib/WWW/Efa/Error/Backend.pm @@ -1,5 +1,27 @@ package WWW::Efa::Error::Backend; +=head1 NAME + +WWW::Efa::Error::Backend - WWW::Efa unknown error from efa.vrr.de + +=head1 SYNOPSIS + + use WWW::Efa::Error::Backend; + + my $error = WWW::Efa::Error::Backend->new( + 'Yadda Yadda' + ); + + die $error->as_string(); + # WWW::Efa error from efa.vrr.de: + # Yadda Yadda + +=head1 DESCRIPTION + +Received an unknown error from efa.vrr.de + +=cut + use strict; use warnings; use 5.010; @@ -7,42 +29,32 @@ use 5.010; use base 'Exporter'; our @EXPORT_OK = qw{}; +our @ISA = ('WWW::Efa::Error'); sub new { - my ($obj, $type, $data) = @_; + my ($obj, $msg) = @_; my $ref = {}; - $ref->{'type'} = $type; - $ref->{'data'} = $data; + $ref->{'message'} = $msg; return bless($ref, $obj); } +=head1 METHODS + +=head2 $error->as_string() + +Return the error as string, can directly be displayed to the user + +=cut + sub as_string { my ($self) = @_; - my $ret; - - given ($self->{'type'}) { - when ('no data') { - $ret = "WWW::Efa: efa.vrr.de returned no data\n"; - } - when ('ambiguous') { - $ret = sprintf( - "WWW::Efa: efa.vrr.de: Ambiguous input for %s:\n", - shift(@{$self->{'data'}}), - ); - foreach my $possible (@{$self->{'data'}}) { - $ret .= "\t${possible}\n"; - } - } - when ('error') { - $ret = sprintf( - "WWW::Efa: efa.vrr.de error:\n%s\n", - $self->{'data'}, - ); - } - } - return $ret; + + return sprintf( + "WWW::Efa error from efa.vrr.de:\n%s\n", + $self->{'message'}, + ); } 1; diff --git a/lib/WWW/Efa/Error/NoData.pm b/lib/WWW/Efa/Error/NoData.pm new file mode 100644 index 0000000..e74d220 --- /dev/null +++ b/lib/WWW/Efa/Error/NoData.pm @@ -0,0 +1,50 @@ +package WWW::Efa::Error::NoData; + +=head1 NAME + +WWW::Efa::Error::NoData - WWW::Efa error, efa.vrr.de returned no data + +=head1 SYNOPSIS + + use WWW::Efa::Error::Setup; + + my $error = WWW::Efa::Error::NoData->new(); + + die $error->as_string(); + # WWW::Efa error: No data returned by efa.vrr.de + +=head1 DESCRIPTION + +efa.vrr.de returned no parsable data + +=cut + +use strict; +use warnings; +use 5.010; + +use base 'Exporter'; + +our @EXPORT_OK = qw{}; +our @ISA = ('WWW::Efa::Error'); + +sub new { + my ($obj) = @_; + my $ref = {}; + + return bless($ref, $obj); +} + +=head1 METHODS + +=head2 $error->as_string() + +Return the error as string, can directly be displayed to the user + +=cut + +sub as_string { + return "WWW::Efa error: No data returned by efa.vrr.de\n"; +} + +1; diff --git a/lib/WWW/Efa/Error/Setup.pm b/lib/WWW/Efa/Error/Setup.pm index 386613b..9840687 100644 --- a/lib/WWW/Efa/Error/Setup.pm +++ b/lib/WWW/Efa/Error/Setup.pm @@ -52,7 +52,6 @@ Return the error as string, can directly be displayed to the user sub as_string { my ($self) = @_; - my $ret; return sprintf( "WWW::Efa setup error: Wrong arg for option %s: %s\n%s\n", -- cgit v1.2.3