From fbd7d22719c793e16bf4230b0b3ef4cef3403fc4 Mon Sep 17 00:00:00 2001 From: Pedro Melo Date: Wed, 4 Aug 2010 20:16:40 +0100 Subject: [PATCH] Promote all die's to confess(): more usefull information in case of errors Signed-off-by: Pedro Melo --- Makefile.PL | 1 + lib/Redis.pm | 13 +++++++------ t/01-Redis.t | 7 ++++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index 98daa4f..231c034 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -10,6 +10,7 @@ WriteMakefile( PL_FILES => {}, PREREQ_PM => { 'Test::More' => 0, + 'Test::Exception' => 0, 'IO::Socket::INET' => 0, 'Data::Dumper' => 0, 'Carp' => 0, diff --git a/lib/Redis.pm b/lib/Redis.pm index 49eed27..3be91ff 100644 --- a/lib/Redis.pm +++ b/lib/Redis.pm @@ -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; diff --git a/t/01-Redis.t b/t/01-Redis.t index 1d98a2f..8157773 100755 --- a/t/01-Redis.t +++ b/t/01-Redis.t @@ -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'; -- 2.20.1