diff options
author | Daniel Friesel <derf@finalrewind.org> | 2011-06-28 10:44:22 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2011-06-28 10:44:22 +0200 |
commit | ed8f38f2ae20a6f794aa6ce322df47ce92c46555 (patch) | |
tree | 0b340fe10a4e162f84d9f727bf7a59fa90f86018 /bin/efa | |
parent | 7e214984c064709e59a74a784f5776493f763c50 (diff) |
Try Exception::Class
Diffstat (limited to 'bin/efa')
-rwxr-xr-x | bin/efa | 108 |
1 files changed, 89 insertions, 19 deletions
@@ -7,6 +7,7 @@ use warnings; use 5.010; use Travel::Routing::DE::VRR; +use Exception::Class; use Getopt::Long qw/:config no_ignore_case/; our $VERSION = '1.3'; @@ -25,6 +26,71 @@ my $opt = { binmode( STDOUT, ':encoding(utf-8)' ); binmode( STDERR, ':encoding(utf-8)' ); +sub handle_efa_exception { + my ($e) = @_; + + if ( $e->isa('Travel::Routing::DE::VRR::Exception::Setup') ) { + if ( $e->message ) { + printf STDERR ( "Error: %s (option '%s'): %s\n", $e->description, + $e->message ); + } + else { + printf STDERR ( + "Error: %s (option '%s', got '%s', want '%s')\n", + $e->description, $e->option, $e->have, $e->want + ); + } + + exit 1; + } + if ( $e->isa('Travel::Routing::DE::VRR::Exception::Net') ) { + printf STDERR ( "Error: %s: %s\n", $e->description, + $e->http_errstr->as_string ); + exit 2; + } + if ( $e->isa('Travel::Routing::DE::VRR::Exception::NoData') ) { + printf STDERR ( 'Error: %s', $e->description ); + exit 3; + } + if ( $e->isa('Travel::Routing::DE::VRR::Exception::Ambiguous') ) { + printf STDERR ( + "Error: %s for key %s. Specify one of %s\n", + $e->description, $e->post_key, $e->possibilities + ); + exit 4; + } + if ( $e->isa('Travel::Routing::DE::VRR::Exception::NoConnections') ) { + printf STDERR ( "Error: %s: %s\n", $e->description, $e->error ); + exit 5; + } + + printf STDERR ( "Uncatched exception: %s\n%s", ref($e), $e->trace ); + exit 10; +} + +sub check_for_error { + my ($eval_error) = @_; + + if ( not defined $efa ) { + if ( $eval_error + and ref($eval_error) =~ m{^Travel::Routing::DE::VRR::Exception}x ) + { + handle_efa_exception($eval_error); + } + elsif ($eval_error) { + printf STDERR + "Unknown Travel::Routing::DE::VRR error:\n${eval_error}"; + exit 10; + } + else { + say STDERR 'Travel::Routing::DE::VRR failed to return an object'; + exit 10; + } + } + + return; +} + #<<< GetOptions( $opt, @@ -80,25 +146,29 @@ if ( defined $opt->{'ignore-info'} and length( $opt->{'ignore-info'} ) == 0 ) { $opt->{'ignore-info'} = undef; } -$efa = Travel::Routing::DE::VRR->new( - origin => [ @from, $from_type ], - destination => [ @to, $to_type ], - via => ( @via ? [ @via, $via_type ] : undef ), - - arrival_time => $opt->{arrive}, - departure_time => $opt->{depart} // $opt->{time}, - date => $opt->{date}, - exclude => $opt->{exclude}, - train_type => $opt->{include}, - with_bike => $opt->{bike}, - - select_interchange_by => $opt->{prefer}, - use_near_stops => $opt->{proximity}, - walk_speed => $opt->{'walk-speed'}, - max_interchanges => $opt->{'max-change'}, -); - -$efa->submit( timeout => $opt->{'timeout'} ); +$efa = eval { + Travel::Routing::DE::VRR->new( + origin => [ @from, $from_type ], + destination => [ @to, $to_type ], + via => ( @via ? [ @via, $via_type ] : undef ), + + arrival_time => $opt->{arrive}, + departure_time => $opt->{depart} // $opt->{time}, + date => $opt->{date}, + exclude => $opt->{exclude}, + train_type => $opt->{include}, + with_bike => $opt->{bike}, + + select_interchange_by => $opt->{prefer}, + use_near_stops => $opt->{proximity}, + walk_speed => $opt->{'walk-speed'}, + max_interchanges => $opt->{'max-change'}, + + lwp_options => { timeout => $opt->{timeout} }, + ); +}; + +check_for_error($@); my @routes = $efa->routes(); |