diff options
author | Daniel Friesel <derf@finalrewind.org> | 2015-05-13 16:48:56 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2015-05-13 16:48:56 +0200 |
commit | 4f89679f245cf42d6b9719271214290598ccc642 (patch) | |
tree | 30a5692530ece85a12c45d85191b426bd761b208 | |
parent | c79ef8e7f70b59eab82c4982b06e1c3c6f7910f5 (diff) |
error handling
-rw-r--r-- | index.pl | 47 |
1 files changed, 36 insertions, 11 deletions
@@ -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; |