From 4f89679f245cf42d6b9719271214290598ccc642 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 13 May 2015 16:48:56 +0200 Subject: error handling --- index.pl | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) (limited to 'index.pl') diff --git a/index.pl b/index.pl index 0740837..76ab37e 100644 --- a/index.pl +++ b/index.pl @@ -49,7 +49,7 @@ sub get_results_for { serializable => 1, %opt ); - $results = [ $status->results ]; + $results = [ [ $status->results ], $status->errstr ]; $cache->freeze( $cache_str, $results ); } else { @@ -57,7 +57,7 @@ sub get_results_for { station => $station, %opt ); - $results = [ $status->results ]; + $results = [ [ $status->results ], $status->errstr ]; $cache->freeze( $cache_str, $results ); } } @@ -110,29 +110,52 @@ sub handle_request { } my @departures; - my @results = get_results_for( $backend, $station, %opt ); + my ( $results_ref, $errstr ) = get_results_for( $backend, $station, %opt ); + my @results = @{$results_ref}; if ( not @results and $template ~~ [qw[json marudor_v1 marudor]] ) { $self->res->headers->access_control_allow_origin('*'); my $json; - if ( $backend eq 'iris' ) { - my @candidates = map { { code => $_->[0], name => $_->[1] } } - Travel::Status::DE::IRIS::Stations::get_station($station); + if ($errstr) { $json = $self->render_to_string( json => { api_version => $api_version, version => $VERSION, - error => 'ambiguous station code/name', - candidates => \@candidates, + error => $errstr, } ); } + elsif ( $backend eq 'iris' ) { + my @candidates = map { { code => $_->[0], name => $_->[1] } } + Travel::Status::DE::IRIS::Stations::get_station($station); + if ( @candidates > 1 + or ( @candidates == 1 and $candidates[0]{code} ne $station ) ) + { + $json = $self->render_to_string( + json => { + api_version => $api_version, + version => $VERSION, + error => 'ambiguous station code/name', + candidates => \@candidates, + } + ); + } + else { + $json = $self->render_to_string( + json => { + api_version => $api_version, + version => $VERSION, + error => ( $errstr // "Got no results for '$station'" ) + } + ); + } + } else { $json = $self->render_to_string( json => { api_version => $api_version, version => $VERSION, - error => 'unknown station code/name', + error => ( $errstr // 'unknown station code/name' ) } ); } @@ -155,7 +178,9 @@ sub handle_request { if ( $backend eq 'iris' ) { my @candidates = map { [ "$_->[1] ($_->[0])", $_->[0] ] } Travel::Status::DE::IRIS::Stations::get_station($station); - if (@candidates) { + if ( @candidates > 1 + or ( @candidates == 1 and $candidates[0][1] ne $station ) ) + { $self->render( 'multi', stationlist => \@candidates, @@ -166,7 +191,7 @@ sub handle_request { } $self->render( 'multi', - error => "Got no results for '$station'", + error => ( $errstr // "Got no results for '$station'" ), hide_opts => 0 ); return; -- cgit v1.2.3