From 874de1129819d4d4559e88d75f8f30b51f66ba90 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 10 Sep 2020 21:20:26 +0200 Subject: handle rejected promises; do not accidentally duplicate rejections --- lib/Travelynx/Helper/DBDB.pm | 22 +++++++++++++--------- lib/Travelynx/Helper/HAFAS.pm | 22 ++++++++++++++-------- 2 files changed, 27 insertions(+), 17 deletions(-) (limited to 'lib/Travelynx/Helper') diff --git a/lib/Travelynx/Helper/DBDB.pm b/lib/Travelynx/Helper/DBDB.pm index 2161962..3f181f6 100644 --- a/lib/Travelynx/Helper/DBDB.pm +++ b/lib/Travelynx/Helper/DBDB.pm @@ -32,12 +32,10 @@ sub has_wagonorder_p { if ( my $content = $cache->get($url) ) { if ( $content eq 'y' ) { - $promise->resolve; - return $promise; + return $promise->resolve; } elsif ( $content eq 'n' ) { - $promise->reject; - return $promise; + return $promise->reject; } } @@ -53,11 +51,13 @@ sub has_wagonorder_p { $cache->set( $url, 'n' ); $promise->reject; } + return; } )->catch( sub { $cache->set( $url, 'n' ); $promise->reject; + return; } )->wait; return $promise; @@ -86,11 +86,13 @@ sub get_wagonorder_p { my $json = JSON->new->decode($body); $cache->freeze( $url, $json ); $promise->resolve($json); + return; } )->catch( sub { my ($err) = @_; $promise->reject($err); + return; } )->wait; return $promise; @@ -105,8 +107,7 @@ sub get_stationinfo_p { my $promise = Mojo::Promise->new; if ( my $content = $cache->thaw($url) ) { - $promise->resolve($content); - return $promise; + return $promise->resolve($content); } $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) @@ -115,17 +116,20 @@ sub get_stationinfo_p { my ($tx) = @_; if ( my $err = $tx->error ) { - return $promise->reject("HTTP $err->{code} $err->{message}"); + $promise->reject("HTTP $err->{code} $err->{message}"); + return; } my $json = $tx->result->json; $cache->freeze( $url, $json ); - return $promise->resolve($json); + $promise->resolve($json); + return; } )->catch( sub { my ($err) = @_; - return $promise->reject($err); + $promise->reject($err); + return; } )->wait; return $promise; diff --git a/lib/Travelynx/Helper/HAFAS.pm b/lib/Travelynx/Helper/HAFAS.pm index 572f8fa..1fd66b5 100644 --- a/lib/Travelynx/Helper/HAFAS.pm +++ b/lib/Travelynx/Helper/HAFAS.pm @@ -34,8 +34,7 @@ sub get_polyline_p { my $version = $self->{version}; if ( my $content = $cache->thaw($url) ) { - $promise->resolve($content); - return $promise; + return $promise->resolve($content); } $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) @@ -97,11 +96,13 @@ sub get_polyline_p { else { $promise->resolve($ret); } + return; } )->catch( sub { my ($err) = @_; $promise->reject($err); + return; } )->wait; @@ -144,11 +145,13 @@ sub get_tripid_p { } } $promise->reject; + return; } )->catch( sub { my ($err) = @_; $promise->reject($err); + return; } )->wait; @@ -162,8 +165,7 @@ sub get_rest_p { my $promise = Mojo::Promise->new; if ( my $content = $cache->thaw($url) ) { - $promise->resolve($content); - return $promise; + return $promise->resolve($content); } $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) @@ -180,12 +182,14 @@ sub get_rest_p { my $json = JSON->new->decode( $tx->res->body ); $cache->freeze( $url, $json ); $promise->resolve($json); + return; } )->catch( sub { my ($err) = @_; $self->{log}->warn("get($url): $err"); $promise->reject($err); + return; } )->wait; return $promise; @@ -198,8 +202,7 @@ sub get_json_p { my $promise = Mojo::Promise->new; if ( my $content = $cache->thaw($url) ) { - $promise->resolve($content); - return $promise; + return $promise->resolve($content); } $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) @@ -222,12 +225,14 @@ sub get_json_p { my $json = JSON->new->decode($body); $cache->freeze( $url, $json ); $promise->resolve($json); + return; } )->catch( sub { my ($err) = @_; $self->{log}->warn("get($url): $err"); $promise->reject($err); + return; } )->wait; return $promise; @@ -240,8 +245,7 @@ sub get_xml_p { my $promise = Mojo::Promise->new; if ( my $content = $cache->thaw($url) ) { - $promise->resolve($content); - return $promise; + return $promise->resolve($content); } $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) @@ -303,12 +307,14 @@ sub get_xml_p { $cache->freeze( $url, $traininfo ); $promise->resolve($traininfo); + return; } )->catch( sub { my ($err) = @_; $self->{log}->warn("get($url): $err"); $promise->reject($err); + return; } )->wait; return $promise; -- cgit v1.2.3