Fixing no errors bug.
This commit is contained in:
parent
1307566334
commit
e885958ea0
|
@ -63,6 +63,7 @@ sub from_text ( $self, $text ) {
|
|||
@cards = map { $self->_fill_scryfall_id($_) } @cards;
|
||||
};
|
||||
if ($@) {
|
||||
say $@;
|
||||
$promise->reject($@);
|
||||
next;
|
||||
}
|
||||
|
|
|
@ -39,59 +39,82 @@ sub run ($self) {
|
|||
}
|
||||
|
||||
sub _dispatch_updates ($self) {
|
||||
Mojo::IOLoop->recurring(0.5, sub {
|
||||
if ( $self->_used_cores >= $self->_cores ) {
|
||||
return;
|
||||
}
|
||||
for ( my $i = $self->_used_cores ; $i <= $self->_cores ; $i++ ) {
|
||||
if ( !defined $self->_updates
|
||||
|| scalar @{ $self->_updates } == 0 )
|
||||
{
|
||||
my $res = $self->_get_updates;
|
||||
my $updates = $res->json->{result};
|
||||
$self->_updates($updates);
|
||||
Mojo::IOLoop->recurring(
|
||||
0.5,
|
||||
sub {
|
||||
if ( $self->_used_cores >= $self->_cores ) {
|
||||
return;
|
||||
}
|
||||
my $update = shift $self->_updates->@*;
|
||||
if ( !defined $update ) {
|
||||
last;
|
||||
}
|
||||
$self->_used_cores( $self->_used_cores + 1 );
|
||||
Mojo::IOLoop->subprocess->run(
|
||||
sub {
|
||||
say "Proccess $i active";
|
||||
$self->_dispatch_update($update)->then(
|
||||
sub {
|
||||
my $message = $update->{message};
|
||||
say 'Generated pdf for '
|
||||
. (Data::Dumper::Dumper $message->{from}) . ' '
|
||||
. $message->{date};
|
||||
Mojo::IOLoop->stop;
|
||||
}
|
||||
);
|
||||
Mojo::IOLoop->start;
|
||||
return;
|
||||
},
|
||||
sub {
|
||||
say "Proccess $i ended";
|
||||
$self->_used_cores( $self->_used_cores - 1 );
|
||||
for ( my $i = $self->_used_cores ; $i <= $self->_cores ; $i++ ) {
|
||||
if ( !defined $self->_updates
|
||||
|| scalar @{ $self->_updates } == 0 )
|
||||
{
|
||||
my $res = $self->_get_updates;
|
||||
my $updates = $res->json->{result};
|
||||
$self->_updates($updates);
|
||||
}
|
||||
);
|
||||
say $i;
|
||||
my $update = shift $self->_updates->@*;
|
||||
if ( !defined $update ) {
|
||||
last;
|
||||
}
|
||||
$self->_used_cores( $self->_used_cores + 1 );
|
||||
Mojo::IOLoop->subprocess->run(
|
||||
sub {
|
||||
say "Proccess $i active";
|
||||
$self->_dispatch_update($update)->then(
|
||||
sub {
|
||||
my $message = $update->{message};
|
||||
say 'Generated pdf for '
|
||||
. ( Data::Dumper::Dumper $message->{from} )
|
||||
. ' '
|
||||
. $message->{date};
|
||||
Mojo::IOLoop->stop;
|
||||
}
|
||||
)->catch(
|
||||
sub ($err) {
|
||||
{
|
||||
if ( !defined $err ) {
|
||||
next;
|
||||
}
|
||||
my $message = $update->{message};
|
||||
say 'Could not generate anything for '
|
||||
. (
|
||||
Data::Dumper::Dumper $message->{from} )
|
||||
. ' '
|
||||
. $message->{date};
|
||||
}
|
||||
Mojo::IOLoop->stop;
|
||||
}
|
||||
);
|
||||
Mojo::IOLoop->start;
|
||||
return;
|
||||
},
|
||||
sub {
|
||||
say "Proccess $i ended";
|
||||
$self->_used_cores( $self->_used_cores - 1 );
|
||||
}
|
||||
);
|
||||
say $i;
|
||||
}
|
||||
}
|
||||
});
|
||||
);
|
||||
}
|
||||
|
||||
sub _dispatch_update ( $self, $update ) {
|
||||
my $promise = Mojo::Promise->new;
|
||||
{
|
||||
if ( !defined $update->{message} ) {
|
||||
$promise->resolve;
|
||||
$promise->reject;
|
||||
next;
|
||||
}
|
||||
$self->_handle_message( $update->{message} )->finally(
|
||||
$self->_handle_message( $update->{message} )->then(
|
||||
sub {
|
||||
$promise->resolve;
|
||||
}
|
||||
)->catch(
|
||||
sub ($err) {
|
||||
$promise->reject($err);
|
||||
}
|
||||
);
|
||||
}
|
||||
return $promise;
|
||||
|
@ -100,9 +123,6 @@ sub _dispatch_update ( $self, $update ) {
|
|||
sub sendMessage ( $self, $chat_id, $text ) {
|
||||
my $url = "@{[$self->_tg_root]}/sendMessage";
|
||||
my $ua = $self->_ua;
|
||||
if ( defined $self->_ioloop ) {
|
||||
$ua->ioloop( $self->_ioloop );
|
||||
}
|
||||
my $promise =
|
||||
$ua->post_p( $url, json => { chat_id => $chat_id, text => $text } );
|
||||
return $promise;
|
||||
|
@ -113,13 +133,13 @@ sub _handle_message ( $self, $message ) {
|
|||
{
|
||||
my $chat_type = $message->{chat}{type};
|
||||
if ( $chat_type ne 'private' ) {
|
||||
$promise->resolve;
|
||||
$promise->reject;
|
||||
next;
|
||||
}
|
||||
my $chat_id = $message->{chat}{id};
|
||||
my $text = $message->{text};
|
||||
if ( !defined $text ) {
|
||||
$promise->resolve;
|
||||
$promise->reject;
|
||||
next;
|
||||
}
|
||||
$self->sendMessage( $chat_id,
|
||||
|
@ -137,44 +157,53 @@ sub _handle_message ( $self, $message ) {
|
|||
}
|
||||
)->catch(
|
||||
sub ($err) {
|
||||
$promise->resolve;
|
||||
my $match = 0;
|
||||
for my $known_error (
|
||||
$TgMagicPdf::PdfBuilder::ERR_INVALID_CARD,
|
||||
$TgMagicPdf::PdfBuilder::ERR_UNABLE_TO_FIND_IMAGE,
|
||||
$TgMagicPdf::PdfBuilder::ERR_TOO_MANY_CARDS
|
||||
)
|
||||
my @error_promises;
|
||||
{
|
||||
$match = 1 if ( index( $err, $known_error ) != -1 );
|
||||
}
|
||||
if ($match) {
|
||||
my $error_to_user .= $err =~ s/ at.*$//r;
|
||||
if ( -1 != index $err,
|
||||
$TgMagicPdf::PdfBuilder::ERR_INVALID_CARD )
|
||||
{
|
||||
$error_to_user .=
|
||||
' ' . $pdf_builder->last_invalid_card;
|
||||
}
|
||||
if ( -1 != index $err,
|
||||
$TgMagicPdf::PdfBuilder::ERR_UNABLE_TO_FIND_IMAGE
|
||||
my $match = 0;
|
||||
for my $known_error (
|
||||
$TgMagicPdf::PdfBuilder::ERR_INVALID_CARD,
|
||||
$TgMagicPdf::PdfBuilder::ERR_UNABLE_TO_FIND_IMAGE,
|
||||
$TgMagicPdf::PdfBuilder::ERR_TOO_MANY_CARDS
|
||||
)
|
||||
{
|
||||
$error_to_user .=
|
||||
' ' . $pdf_builder->last_invalid_card;
|
||||
$match = 1
|
||||
if ( index( $err, $known_error ) != -1 );
|
||||
}
|
||||
$self->sendMessage( $chat_id,
|
||||
if ($match) {
|
||||
my $error_to_user = $err =~ s/^(.*?)\s*at.*$/$1/mr;
|
||||
chomp $error_to_user;
|
||||
if ( -1 != index $err,
|
||||
$TgMagicPdf::PdfBuilder::ERR_INVALID_CARD )
|
||||
{
|
||||
$error_to_user .=
|
||||
' ' . $pdf_builder->last_invalid_card;
|
||||
}
|
||||
if ( -1 != index $err,
|
||||
$TgMagicPdf::PdfBuilder::ERR_UNABLE_TO_FIND_IMAGE
|
||||
)
|
||||
{
|
||||
$error_to_user .=
|
||||
' ' . $pdf_builder->last_invalid_card;
|
||||
}
|
||||
push @error_promises,
|
||||
$self->sendMessage(
|
||||
$chat_id,
|
||||
'I could not process your deck take a look to this details: '
|
||||
. $error_to_user )->wait;
|
||||
return;
|
||||
}
|
||||
warn $err;
|
||||
$self->sendMessage( $chat_id,
|
||||
. $error_to_user
|
||||
);
|
||||
next;
|
||||
}
|
||||
push @error_promises,
|
||||
$self->sendMessage( $chat_id,
|
||||
'I could not process your deck because of a server error'
|
||||
)->wait;
|
||||
);
|
||||
}
|
||||
Mojo::Promise->all(@error_promises)
|
||||
->finally( sub { $promise->reject(1) } );
|
||||
}
|
||||
);
|
||||
}
|
||||
)->catch( sub ($err) { say $err } );
|
||||
)->catch( sub ($err) { warn $err } );
|
||||
}
|
||||
return $promise;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue