Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Error Handling #13

Closed
dexteruk opened this issue Dec 22, 2016 · 5 comments
Closed

Async Error Handling #13

dexteruk opened this issue Dec 22, 2016 · 5 comments

Comments

@dexteruk
Copy link

Hi there seems to be a problem when it comes to using Async im using your default example, and im getting some strange errors

            $chatmessage = "Hello";
            $api->api_request ('sendMessage', {
                    chat_id      => $message->{'message'}{'chat'}{'id'},
                    text    => $chatmessage
            }, sub {
              my ($ua, $tx) = @_;
              die 'Something bad happened!' unless $tx->success;
              say $tx->res->json->{ok} ? 'YAY!' : ':('; # Not production ready!
            });

As you can see the PAYLOAD is being sent

PAYLOAD = [
"https://api.telegram.org/botXXXXXXXXX/sendMessage",
"json",
{
"chat_id" : 1XXXXXXXX,
"text" : "Hello"
}
]

But if i dump the $tx i see this error 'message' => 'Premature connection close'

$VAR1 = bless( {
'events' => {},
'res' => bless( {
'content' => bless( {
'headers' => bless( {
'headers' => {}
}, 'Mojo::Headers' ),
'events' => {
'read' => [
sub { "DUMMY" }
]
},
'read' => $VAR1->{'res'}{'content'}{'events'}{'read'}[0]
}, 'Mojo::Content::Single' ),
'error' => {
'message' => 'Premature connection close'
},
'state' => 'finished',
'finished' => 2,
'events' => {}
}, 'Mojo::Message::Response' ),
'completed' => 1,
'req' => bless( {

If i turn of async = 0 then the message gets thought.

Thanks
dexter

@Robertof
Copy link
Owner

Hello,

thanks for your report!
Did you start the Mojolicious event loop by using:

Mojo::IOLoop->start;

after the request?

Happy holidays,
Roberto

@dexteruk
Copy link
Author

dexteruk commented Dec 30, 2016

http://pastebin.com/0x6U0jGn

I took the code back to some basics. Basically if i set Async => 1 it seems to start the Mojo::IO Loop as i see an error
[error] Mojo::IOLoop already running at /home/vapi/test.pl line 35.
so i replace this with
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;

But im still getting 'message' => 'Premature connection close'

Hope you have a wonderful New Year and may 2017 be amazing for you

@Robertof
Copy link
Owner

Robertof commented Dec 31, 2016

Hello,

thanks for getting back!
Running the IOLoop when inside a Mojolicious app is not necessary, so you should be fine.

Consider this example code, derived from yours:

BEGIN { ++$ENV{TELEGRAM_BOTAPI_DEBUG} }

use Mojolicious::Lite;
use WWW::Telegram::BotAPI;

my $api = WWW::Telegram::BotAPI->new (
    token => 'tok',
    async => 1
);

any '/' => sub {
    my $self = shift;

    $self->render_later;

    $api->getMe (sub {
        my (undef, $tx) = @_;
        unless ($tx->success) {
            my $message = ($tx->error || {})->{message} || "something went wrong!";
            app->log->error ("Error! HTTP message: $message");
            app->log->error ("Please inspect \$tx->res->json for further information.");
            return $self->render (json => { ok => \0 })
        }
        $self->render (json => $tx->res->json);
    });
};

app->start;

On my side, this works with no problem. Try it on your side and see if it works.

On which platform are you currently using this module?
Thanks, and happy new year to you too!

@dexteruk
Copy link
Author

dexteruk commented Jan 1, 2017 via email

@Robertof
Copy link
Owner

Robertof commented Jan 3, 2017

That's interesting! Sorry for the delay -- I will do more research on that topic and I will probably include a note in the documentation.

Thank you for being a great, collaborative user, and may you have a fantastic year too!
Have a nice day,
Roberto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants