Skip to content

Commit

Permalink
moved error event fallbacks to Mojo::EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 1, 2013
1 parent 4717de2 commit 5531616
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
19 changes: 14 additions & 5 deletions lib/Mojo/EventEmitter.pm
Expand Up @@ -5,6 +5,12 @@ use Scalar::Util qw(blessed weaken);

use constant DEBUG => $ENV{MOJO_EVENTEMITTER_DEBUG} || 0;

sub new {
my $self = shift->SUPER::new(@_);
$self->on(error => sub { warn $_[1] if @{$_[0]->subscribers('error')} < 2 });
return $self;
}

sub emit {
my ($self, $name) = (shift, shift);

Expand All @@ -30,11 +36,7 @@ sub emit_safe {
if ($name eq 'error') { warn qq{Event "error" failed: $@} }

# Normal event failed
else {
$self->once(error => sub { warn $_[1] })
unless $self->has_subscribers('error');
$self->emit_safe('error', qq{Event "$name" failed: $@});
}
else { $self->emit_safe('error', qq{Event "$name" failed: $@}) }
}
}
}
Expand Down Expand Up @@ -118,6 +120,13 @@ L<Mojo::EventEmitter> is a simple base class for event emitting objects.
L<Mojo::EventEmitter> inherits all methods from L<Mojo::Base> and
implements the following new ones.
=head2 C<new>
my $e = Mojo::EventEmitter->new;
Construct a new L<Mojo::EventEmitter> object and subscribe to C<error> event
with default error handling.
=head2 C<emit>
$e = $e->emit('foo');
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/Reactor/Poll.pm
Expand Up @@ -100,18 +100,17 @@ sub _poll { shift->{poll} ||= IO::Poll->new }

sub _sandbox {
my ($self, $desc, $cb) = (shift, shift, shift);
return if eval { $self->$cb(@_); 1 };
$self->once(error => sub { warn $_[1] })
unless $self->has_subscribers('error');
$self->emit_safe(error => "$desc failed: $@");
eval { $self->$cb(@_); 1 } or $self->emit_safe(error => "$desc failed: $@");
}

sub _timer {
my ($self, $recurring, $after, $cb) = @_;

my $id;
do { $id = md5_sum('t' . time . rand 999) } while $self->{timers}{$id};
my $t = $self->{timers}{$id} = {cb => $cb, time => time + $after};
$t->{recurring} = $after if $recurring;

return $id;
}

Expand Down
4 changes: 0 additions & 4 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -289,11 +289,7 @@ sub _connection {

sub _error {
my ($self, $id, $err, $emit) = @_;

# Add error to transaction if possible
if (my $tx = $self->{connections}{$id}{tx}) { $tx->res->error($err) }
$self->once(error => sub { warn $_[1] })
if $emit && !$self->has_subscribers('error');
$self->emit(error => $err) if $emit;
$self->_handle($id => $err);
}
Expand Down
8 changes: 4 additions & 4 deletions t/mojo/user_agent_online.t
Expand Up @@ -196,10 +196,10 @@ is $tx->req->url, 'http://ipv6.google.com', 'right url';
is $tx->res->code, 200, 'right status';

# Simple HTTPS request
$tx = $ua->get('https://www.metacpan.org');
is $tx->req->method, 'GET', 'right method';
is $tx->req->url, 'https://www.metacpan.org', 'right url';
is $tx->res->code, 200, 'right status';
$tx = $ua->get('https://metacpan.org');
is $tx->req->method, 'GET', 'right method';
is $tx->req->url, 'https://metacpan.org', 'right url';
is $tx->res->code, 200, 'right status';

# HTTPS request that requires IPv6
$tx = $ua->get('https://ipv6.google.com');
Expand Down

0 comments on commit 5531616

Please sign in to comment.