Skip to content

Commit

Permalink
Promote all die's to confess(): more usefull information in case of e…
Browse files Browse the repository at this point in the history
…rrors

Signed-off-by: Pedro Melo <melo@simplicidade.org>
  • Loading branch information
melo committed Aug 4, 2010
1 parent 623d7d4 commit fbd7d22
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Expand Up @@ -10,6 +10,7 @@ WriteMakefile(
PL_FILES => {},
PREREQ_PM => {
'Test::More' => 0,
'Test::Exception' => 0,
'IO::Socket::INET' => 0,
'Data::Dumper' => 0,
'Carp' => 0,
Expand Down
13 changes: 7 additions & 6 deletions lib/Redis.pm
Expand Up @@ -48,10 +48,11 @@ sub new {
$self->{debug} ||= $ENV{REDIS_DEBUG};
$self->{encoding} ||= 'utf8'; ## default to lax utf8

$self->{server} ||= $ENV{REDIS_SERVER} || '127.0.0.1:6379';
$self->{sock} = IO::Socket::INET->new(
PeerAddr => $self->{server} || $ENV{REDIS_SERVER} || '127.0.0.1:6379',
PeerAddr => $self->{server},
Proto => 'tcp',
) || die $!;
) || confess("Could not connect to Redis server at $self->{server}: $!");

return bless($self, $class);
}
Expand All @@ -62,7 +63,7 @@ sub DESTROY {}
our $AUTOLOAD;
sub AUTOLOAD {
my $self = shift;
my $sock = $self->{sock} || die "no server connected";
my $sock = $self->{sock} || confess("Not connected to any server");
my $enc = $self->{encoding};
my $deb = $self->{debug};

Expand All @@ -81,11 +82,11 @@ sub AUTOLOAD {
print $sock $send;

if ( $command eq 'quit' ) {
close( $sock ) || die "can't close socket: $!";
close( $sock ) || confess("Can't close socket: $!");
return 1;
}

my $result = <$sock> || die "can't read socket: $!";
my $result = <$sock> || confess("Can't read socket: $!");
my $type = substr($result,0,1);
$result = substr($result,1,-2);

Expand Down Expand Up @@ -129,7 +130,7 @@ sub __read_bulk {
my $enc = $self->{encoding};
my $v = '';
if ( $len > 0 ) {
read($self->{sock}, $v, $len) || die $!;
read($self->{sock}, $v, $len) || confess("Could not read from sock: $!");
$v = decode($enc, $v) if $enc;
}
my $crlf;
Expand Down
7 changes: 6 additions & 1 deletion t/01-Redis.t
Expand Up @@ -3,7 +3,8 @@
use warnings;
use strict;

use Test::More tests => 110;
use Test::More tests => 111;
use Test::Exception;
use Data::Dumper;

use lib 'lib';
Expand Down Expand Up @@ -195,3 +196,7 @@ diag Dumper( $info );
diag "Connection handling";

ok( $o->quit, 'quit' );

throws_ok sub { Redis->new(server => '127.0.0.1:1') },
qr/Could not connect to Redis server at 127[.]0[.]0[.]1:1:/,
'Failed connection throws exception';

0 comments on commit fbd7d22

Please sign in to comment.