Fixing no errors bug.

This commit is contained in:
Sergiotarxz 2024-05-23 22:39:03 +02:00
parent 1307566334
commit e885958ea0
2 changed files with 103 additions and 73 deletions

View File

@ -63,6 +63,7 @@ sub from_text ( $self, $text ) {
@cards = map { $self->_fill_scryfall_id($_) } @cards; @cards = map { $self->_fill_scryfall_id($_) } @cards;
}; };
if ($@) { if ($@) {
say $@;
$promise->reject($@); $promise->reject($@);
next; next;
} }

View File

@ -39,7 +39,9 @@ sub run ($self) {
} }
sub _dispatch_updates ($self) { sub _dispatch_updates ($self) {
Mojo::IOLoop->recurring(0.5, sub { Mojo::IOLoop->recurring(
0.5,
sub {
if ( $self->_used_cores >= $self->_cores ) { if ( $self->_used_cores >= $self->_cores ) {
return; return;
} }
@ -63,10 +65,26 @@ sub _dispatch_updates ($self) {
sub { sub {
my $message = $update->{message}; my $message = $update->{message};
say 'Generated pdf for ' say 'Generated pdf for '
. (Data::Dumper::Dumper $message->{from}) . ' ' . ( Data::Dumper::Dumper $message->{from} )
. ' '
. $message->{date}; . $message->{date};
Mojo::IOLoop->stop; 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; Mojo::IOLoop->start;
return; return;
@ -78,20 +96,25 @@ sub _dispatch_updates ($self) {
); );
say $i; say $i;
} }
}); }
);
} }
sub _dispatch_update ( $self, $update ) { sub _dispatch_update ( $self, $update ) {
my $promise = Mojo::Promise->new; my $promise = Mojo::Promise->new;
{ {
if ( !defined $update->{message} ) { if ( !defined $update->{message} ) {
$promise->resolve; $promise->reject;
next; next;
} }
$self->_handle_message( $update->{message} )->finally( $self->_handle_message( $update->{message} )->then(
sub { sub {
$promise->resolve; $promise->resolve;
} }
)->catch(
sub ($err) {
$promise->reject($err);
}
); );
} }
return $promise; return $promise;
@ -100,9 +123,6 @@ sub _dispatch_update ( $self, $update ) {
sub sendMessage ( $self, $chat_id, $text ) { sub sendMessage ( $self, $chat_id, $text ) {
my $url = "@{[$self->_tg_root]}/sendMessage"; my $url = "@{[$self->_tg_root]}/sendMessage";
my $ua = $self->_ua; my $ua = $self->_ua;
if ( defined $self->_ioloop ) {
$ua->ioloop( $self->_ioloop );
}
my $promise = my $promise =
$ua->post_p( $url, json => { chat_id => $chat_id, text => $text } ); $ua->post_p( $url, json => { chat_id => $chat_id, text => $text } );
return $promise; return $promise;
@ -113,13 +133,13 @@ sub _handle_message ( $self, $message ) {
{ {
my $chat_type = $message->{chat}{type}; my $chat_type = $message->{chat}{type};
if ( $chat_type ne 'private' ) { if ( $chat_type ne 'private' ) {
$promise->resolve; $promise->reject;
next; next;
} }
my $chat_id = $message->{chat}{id}; my $chat_id = $message->{chat}{id};
my $text = $message->{text}; my $text = $message->{text};
if ( !defined $text ) { if ( !defined $text ) {
$promise->resolve; $promise->reject;
next; next;
} }
$self->sendMessage( $chat_id, $self->sendMessage( $chat_id,
@ -137,7 +157,8 @@ sub _handle_message ( $self, $message ) {
} }
)->catch( )->catch(
sub ($err) { sub ($err) {
$promise->resolve; my @error_promises;
{
my $match = 0; my $match = 0;
for my $known_error ( for my $known_error (
$TgMagicPdf::PdfBuilder::ERR_INVALID_CARD, $TgMagicPdf::PdfBuilder::ERR_INVALID_CARD,
@ -145,10 +166,12 @@ sub _handle_message ( $self, $message ) {
$TgMagicPdf::PdfBuilder::ERR_TOO_MANY_CARDS $TgMagicPdf::PdfBuilder::ERR_TOO_MANY_CARDS
) )
{ {
$match = 1 if ( index( $err, $known_error ) != -1 ); $match = 1
if ( index( $err, $known_error ) != -1 );
} }
if ($match) { if ($match) {
my $error_to_user .= $err =~ s/ at.*$//r; my $error_to_user = $err =~ s/^(.*?)\s*at.*$/$1/mr;
chomp $error_to_user;
if ( -1 != index $err, if ( -1 != index $err,
$TgMagicPdf::PdfBuilder::ERR_INVALID_CARD ) $TgMagicPdf::PdfBuilder::ERR_INVALID_CARD )
{ {
@ -162,19 +185,25 @@ sub _handle_message ( $self, $message ) {
$error_to_user .= $error_to_user .=
' ' . $pdf_builder->last_invalid_card; ' ' . $pdf_builder->last_invalid_card;
} }
$self->sendMessage( $chat_id, push @error_promises,
$self->sendMessage(
$chat_id,
'I could not process your deck take a look to this details: ' 'I could not process your deck take a look to this details: '
. $error_to_user )->wait; . $error_to_user
return; );
next;
} }
warn $err; push @error_promises,
$self->sendMessage( $chat_id, $self->sendMessage( $chat_id,
'I could not process your deck because of a server error' '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; return $promise;
} }