handle rejected promises; do not accidentally duplicate rejections

This commit is contained in:
Daniel Friesel 2020-09-10 21:20:26 +02:00
parent eb93ee40c9
commit 874de11298
3 changed files with 54 additions and 17 deletions

View file

@ -1260,6 +1260,13 @@ sub startup {
{ user_id => $uid } { user_id => $uid }
); );
} }
return;
}
)->catch(
sub {
my ($err) = @_;
$self->app->log->warn("add_route_timestamps: $err");
return;
} }
)->wait; )->wait;
} }
@ -1394,6 +1401,11 @@ sub startup {
{ user_id => $uid } { user_id => $uid }
); );
} }
)->catch(
sub {
my ($err) = @_;
$self->app->log->warn("add_route_timestamps: $err");
}
)->wait; )->wait;
if ( $train->sched_departure ) { if ( $train->sched_departure ) {
@ -1476,6 +1488,11 @@ sub startup {
); );
} }
} }
)->catch(
sub {
# no wagonorder? no problem.
return;
}
)->wait; )->wait;
} }
@ -1497,6 +1514,11 @@ sub startup {
{ user_id => $uid } { user_id => $uid }
); );
} }
)->catch(
sub {
# no stationinfo? no problem.
return;
}
)->wait; )->wait;
} }
@ -1518,6 +1540,11 @@ sub startup {
{ user_id => $uid } { user_id => $uid }
); );
} }
)->catch(
sub {
# no stationinfo? no problem.
return;
}
)->wait; )->wait;
} }
} }

View file

@ -32,12 +32,10 @@ sub has_wagonorder_p {
if ( my $content = $cache->get($url) ) { if ( my $content = $cache->get($url) ) {
if ( $content eq 'y' ) { if ( $content eq 'y' ) {
$promise->resolve; return $promise->resolve;
return $promise;
} }
elsif ( $content eq 'n' ) { elsif ( $content eq 'n' ) {
$promise->reject; return $promise->reject;
return $promise;
} }
} }
@ -53,11 +51,13 @@ sub has_wagonorder_p {
$cache->set( $url, 'n' ); $cache->set( $url, 'n' );
$promise->reject; $promise->reject;
} }
return;
} }
)->catch( )->catch(
sub { sub {
$cache->set( $url, 'n' ); $cache->set( $url, 'n' );
$promise->reject; $promise->reject;
return;
} }
)->wait; )->wait;
return $promise; return $promise;
@ -86,11 +86,13 @@ sub get_wagonorder_p {
my $json = JSON->new->decode($body); my $json = JSON->new->decode($body);
$cache->freeze( $url, $json ); $cache->freeze( $url, $json );
$promise->resolve($json); $promise->resolve($json);
return;
} }
)->catch( )->catch(
sub { sub {
my ($err) = @_; my ($err) = @_;
$promise->reject($err); $promise->reject($err);
return;
} }
)->wait; )->wait;
return $promise; return $promise;
@ -105,8 +107,7 @@ sub get_stationinfo_p {
my $promise = Mojo::Promise->new; my $promise = Mojo::Promise->new;
if ( my $content = $cache->thaw($url) ) { if ( my $content = $cache->thaw($url) ) {
$promise->resolve($content); return $promise->resolve($content);
return $promise;
} }
$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} )
@ -115,17 +116,20 @@ sub get_stationinfo_p {
my ($tx) = @_; my ($tx) = @_;
if ( my $err = $tx->error ) { 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; my $json = $tx->result->json;
$cache->freeze( $url, $json ); $cache->freeze( $url, $json );
return $promise->resolve($json); $promise->resolve($json);
return;
} }
)->catch( )->catch(
sub { sub {
my ($err) = @_; my ($err) = @_;
return $promise->reject($err); $promise->reject($err);
return;
} }
)->wait; )->wait;
return $promise; return $promise;

View file

@ -34,8 +34,7 @@ sub get_polyline_p {
my $version = $self->{version}; my $version = $self->{version};
if ( my $content = $cache->thaw($url) ) { if ( my $content = $cache->thaw($url) ) {
$promise->resolve($content); return $promise->resolve($content);
return $promise;
} }
$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} )
@ -97,11 +96,13 @@ sub get_polyline_p {
else { else {
$promise->resolve($ret); $promise->resolve($ret);
} }
return;
} }
)->catch( )->catch(
sub { sub {
my ($err) = @_; my ($err) = @_;
$promise->reject($err); $promise->reject($err);
return;
} }
)->wait; )->wait;
@ -144,11 +145,13 @@ sub get_tripid_p {
} }
} }
$promise->reject; $promise->reject;
return;
} }
)->catch( )->catch(
sub { sub {
my ($err) = @_; my ($err) = @_;
$promise->reject($err); $promise->reject($err);
return;
} }
)->wait; )->wait;
@ -162,8 +165,7 @@ sub get_rest_p {
my $promise = Mojo::Promise->new; my $promise = Mojo::Promise->new;
if ( my $content = $cache->thaw($url) ) { if ( my $content = $cache->thaw($url) ) {
$promise->resolve($content); return $promise->resolve($content);
return $promise;
} }
$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) $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 ); my $json = JSON->new->decode( $tx->res->body );
$cache->freeze( $url, $json ); $cache->freeze( $url, $json );
$promise->resolve($json); $promise->resolve($json);
return;
} }
)->catch( )->catch(
sub { sub {
my ($err) = @_; my ($err) = @_;
$self->{log}->warn("get($url): $err"); $self->{log}->warn("get($url): $err");
$promise->reject($err); $promise->reject($err);
return;
} }
)->wait; )->wait;
return $promise; return $promise;
@ -198,8 +202,7 @@ sub get_json_p {
my $promise = Mojo::Promise->new; my $promise = Mojo::Promise->new;
if ( my $content = $cache->thaw($url) ) { if ( my $content = $cache->thaw($url) ) {
$promise->resolve($content); return $promise->resolve($content);
return $promise;
} }
$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) $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); my $json = JSON->new->decode($body);
$cache->freeze( $url, $json ); $cache->freeze( $url, $json );
$promise->resolve($json); $promise->resolve($json);
return;
} }
)->catch( )->catch(
sub { sub {
my ($err) = @_; my ($err) = @_;
$self->{log}->warn("get($url): $err"); $self->{log}->warn("get($url): $err");
$promise->reject($err); $promise->reject($err);
return;
} }
)->wait; )->wait;
return $promise; return $promise;
@ -240,8 +245,7 @@ sub get_xml_p {
my $promise = Mojo::Promise->new; my $promise = Mojo::Promise->new;
if ( my $content = $cache->thaw($url) ) { if ( my $content = $cache->thaw($url) ) {
$promise->resolve($content); return $promise->resolve($content);
return $promise;
} }
$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} )
@ -303,12 +307,14 @@ sub get_xml_p {
$cache->freeze( $url, $traininfo ); $cache->freeze( $url, $traininfo );
$promise->resolve($traininfo); $promise->resolve($traininfo);
return;
} }
)->catch( )->catch(
sub { sub {
my ($err) = @_; my ($err) = @_;
$self->{log}->warn("get($url): $err"); $self->{log}->warn("get($url): $err");
$promise->reject($err); $promise->reject($err);
return;
} }
)->wait; )->wait;
return $promise; return $promise;