From cb2c0299e54265131561a90406d930b03218ab8f Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Mon, 21 Aug 2017 23:30:29 +0200 Subject: [PATCH 1/2] Fix exception handling in event listener Properly process any type of exceptions (also Scalars). --- lib/Mojo/SMTP/Client.pm | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/Mojo/SMTP/Client.pm b/lib/Mojo/SMTP/Client.pm index a8a5171..339264e 100644 --- a/lib/Mojo/SMTP/Client.pm +++ b/lib/Mojo/SMTP/Client.pm @@ -140,12 +140,19 @@ sub send { push @steps, $self->_make_cmd_steps(0, @_); # non-blocking - my $delay = $self->{delay} = Mojo::IOLoop::Delay->new(ioloop => $self->_ioloop)->steps(@steps)->catch(sub { - shift->emit(finish => $_[0]); - }); + my $delay = $self->{delay} = Mojo::IOLoop::Delay + ->new(ioloop => $self->_ioloop) + ->steps(@steps) + ->catch(sub { + # handle exceptions which were thrown + my ($ioloop, $err) = @_; + $err = Mojo::SMTP::Client::Response->new('', error => $err) unless $err->isa("Mojo::SMTP::Client::Exception"); + $ioloop->emit(finish => $err); + }); $delay->on(finish => sub { if ($cb) { my $r = $_[1]; + # handle exceptions which where returned if ($r->isa('Mojo::SMTP::Client::Exception')) { $r = Mojo::SMTP::Client::Response->new('', error => $r); } @@ -238,7 +245,7 @@ sub _make_cmd_steps { sub { eval { $self->{resp_checker}->(@_); $_[1]->{checked} = 1 }; if (my $e = $@) { - die $e unless $e->error->isa('Mojo::SMTP::Client::Exception::Response'); + die $e unless ($e->can('error') and $e->error->isa('Mojo::SMTP::Client::Exception::Response')); my $delay = shift; $self->{stream}->start if !$prepend && $mi == $#cmd; # XXX: _read_response may stop stream From 39684b11528d2c470643a414ef742dac2a2383a3 Mon Sep 17 00:00:00 2001 From: Jens Berthold Date: Wed, 30 Aug 2017 13:48:24 +0200 Subject: [PATCH 2/2] Do not convert exception if it's already a response object This fixes the test errors that the previous commit introduced. --- lib/Mojo/SMTP/Client.pm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Mojo/SMTP/Client.pm b/lib/Mojo/SMTP/Client.pm index 339264e..8903e5f 100644 --- a/lib/Mojo/SMTP/Client.pm +++ b/lib/Mojo/SMTP/Client.pm @@ -146,7 +146,9 @@ sub send { ->catch(sub { # handle exceptions which were thrown my ($ioloop, $err) = @_; - $err = Mojo::SMTP::Client::Response->new('', error => $err) unless $err->isa("Mojo::SMTP::Client::Exception"); + unless ($err->isa("Mojo::SMTP::Client::Exception") or $err->isa("Mojo::SMTP::Client::Response")) { + $err = Mojo::SMTP::Client::Response->new('', error => $err) + } $ioloop->emit(finish => $err); }); $delay->on(finish => sub {