Skip to content

Instantly share code, notes, and snippets.

@ghandmann
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghandmann/a2fc0bbb2392fbd8822c to your computer and use it in GitHub Desktop.
Save ghandmann/a2fc0bbb2392fbd8822c to your computer and use it in GitHub Desktop.
Fatal errors in Event-Handlers only emit a finish-event with errorcode 1006
#!/usr/bin/env perl
use Mojo::Base -strict;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new();
$ua->on(error => sub {
my ($ua, $error) = @_;
warn "[ERROR] $error";
});
$ua->websocket("ws://echo.websocket.org/" => sub {
my ($ua, $tx) = @_;
say 'WebSocket handshake failed!' and return unless $tx->is_websocket;
$tx->on(error => sub {
my ($tx, $error) = @_;
warn "[ERROR] $error";
});
$tx->on(finish => sub {
my ($tx, $code, $reason) = @_;
$reason //= "Unknown";
warn "[FINISHED] Code: $code Reason: $reason";
});
$tx->on(message => sub {
my ($tx, $msg) = @_;
say "-> MSG: $msg";
if($msg =~ /fail/i) {
die "Failing hard...";
}
# Even worse, runtime-errors also just give me a finish-event...
# $tx->SomeUnknwonMethod();
});
$tx->send("Websocket Test");
$tx->send("Fail this one!");
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
__DATA__
Output of script:
$ MOJO_EVENTEMITTER_DEBUG=1 perl test.pl
-- Emit start in Mojo::UserAgent (0)
-- Emit connect in Mojo::IOLoop::Client safely (1)
-- Emit connection in Mojo::Transaction::HTTP (0)
-- Emit progress in Mojo::Message::Request (0)
-- Emit progress in Mojo::Message::Request (0)
-- Emit progress in Mojo::Message::Request (0)
-- Emit progress in Mojo::Message::Request (0)
-- Emit progress in Mojo::Message::Request (0)
-- Emit finish in Mojo::Message::Request (0)
-- Emit write in Mojo::IOLoop::Stream safely (0)
-- Emit drain in Mojo::IOLoop::Stream safely (0)
-- Emit read in Mojo::IOLoop::Stream safely (1)
-- Emit body in Mojo::Content::Single (0)
-- Emit progress in Mojo::Message::Response (0)
-- Emit finish in Mojo::Message::Response (0)
-- Emit finish in Mojo::Transaction::HTTP (0)
-- Emit resume in Mojo::Transaction::WebSocket (1)
-- Emit resume in Mojo::Transaction::WebSocket (1)
-- Emit resume in Mojo::Transaction::WebSocket (1)
-- Emit drain in Mojo::Transaction::WebSocket (0)
-- Emit write in Mojo::IOLoop::Stream safely (0)
-- Emit drain in Mojo::IOLoop::Stream safely (2)
-- Emit read in Mojo::IOLoop::Stream safely (1)
-- Emit frame in Mojo::Transaction::WebSocket (1)
-- Emit text in Mojo::Transaction::WebSocket (0)
-- Emit message in Mojo::Transaction::WebSocket (1)
-> MSG: Websocket Test
-- Emit frame in Mojo::Transaction::WebSocket (1)
-- Emit text in Mojo::Transaction::WebSocket (0)
-- Emit message in Mojo::Transaction::WebSocket (1)
-> MSG: Fail this one!
-- Emit error in Mojo::IOLoop::Stream (1)
-- Emit close in Mojo::IOLoop::Stream safely (2)
-- Emit finish in Mojo::Transaction::WebSocket (1)
[FINISHED] Code: 1006 Reason: Unknown at test.pl line 25.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment