Promote all die's to confess(): more usefull information in case of errors
authorPedro Melo <melo@simplicidade.org>
Wed, 4 Aug 2010 19:16:40 +0000 (20:16 +0100)
committerPedro Melo <melo@simplicidade.org>
Wed, 4 Aug 2010 19:16:40 +0000 (20:16 +0100)
Signed-off-by: Pedro Melo <melo@simplicidade.org>
Makefile.PL
lib/Redis.pm
t/01-Redis.t

index 98daa4f..231c034 100644 (file)
@@ -10,6 +10,7 @@ WriteMakefile(
     PL_FILES            => {},
     PREREQ_PM => {
         'Test::More' => 0,
+        'Test::Exception' => 0,
                'IO::Socket::INET' => 0,
                'Data::Dumper' => 0,
                'Carp' => 0,
index 49eed27..3be91ff 100644 (file)
@@ -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);
 }
@@ -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};
 
@@ -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);
 
@@ -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;
index 1d98a2f..8157773 100755 (executable)
@@ -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';
@@ -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';