From 522f4e16705298660d2aeebe033df16ed278ad04 Mon Sep 17 00:00:00 2001 From: Pedro Melo Date: Sun, 20 Jul 2014 17:38:11 +0100 Subject: [PATCH] Hide the call to error_cb in a new method _log_error, API changes: The new error_cb will still receive the same two first parameters, the connection object and the raw error, but will also receive a third parameter, more user friendly error message. Signed-off-by: Pedro Melo --- lib/AnyEvent/NSQ/Connection.pm | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/AnyEvent/NSQ/Connection.pm b/lib/AnyEvent/NSQ/Connection.pm index e9e2a0e..25cddd0 100644 --- a/lib/AnyEvent/NSQ/Connection.pm +++ b/lib/AnyEvent/NSQ/Connection.pm @@ -59,8 +59,6 @@ sub connect { my ($self) = @_; return if $self->{handle}; - my $err_cb = $self->{error_cb}; - $self->{handle} = AnyEvent::Handle->new( connect => [$self->{host}, $self->{port}], @@ -69,11 +67,11 @@ sub connect { on_connect_error => sub { $self->_disconnected; - $err_cb->($self, '(connect) ' . ($_[1] || $!)); + $self->_log_error('(connect failed) ' . ($_[1] || $!)); }, on_error => sub { $self->_disconnected; - $err_cb->($self, '(read) ' . ($_[2] || $!)); + $self->_log_error('(read error) ' . ($_[2] || $!)); }, on_eof => sub { $self->_disconnected; @@ -311,4 +309,13 @@ sub _on_success_frame { } +## Error logging +sub _log_error { + my ($self, $err) = @_; + + $self->{error_cb}->($err, qq{FATAL: dropping connection to host '$self->{host}' port $self->{port}, reason: $err}); + + return; +} + 1;