[Yapcom-commit] rev 82 - lib/YAPC t

[prev] [thread] [next] [lurker] [Date index for 2003/11/27]

From: svn
Subject: [Yapcom-commit] rev 82 - lib/YAPC t
Date: 23:00 on 27 Nov 2003
Author: gabor
Date: 2003-11-27 23:00:19 +0200 (Thu, 27 Nov 2003)
New Revision: 82

Added:
   lib/YAPC/Error.pm
Modified:
   lib/YAPC/Login.pm
   lib/YAPC/Organizer.pm
   t/02-login.t
Log:
add try/catch Error handling to one method

Added: lib/YAPC/Error.pm
===================================================================
--- lib/YAPC/Error.pm	2003-11-27 20:26:14 UTC (rev 81)
+++ lib/YAPC/Error.pm	2003-11-27 21:00:19 UTC (rev 82)
@@ -0,0 +1,26 @@
+package YAPC::Error;
+use strict;
+use warnings;
+
+use base qw(Error);
+use overload ('""' => 'stringify');
+
+sub new {
+   my $self = shift;
+   my $text = "" . shift;
+   my @args = ();
+
+   local $Error::Depth = $Error::Depth + 1;
+   local $Error::Debug = 1;  # Enables storing of stacktrace
+
+   $self->SUPER::new(-text => $text, @args);
+}
+1;
+	  
+#package YAPC::DB::Error;
+#use base qw(YAPC::Error);
+#1;
+
+
+1;
+

Modified: lib/YAPC/Login.pm
===================================================================
--- lib/YAPC/Login.pm	2003-11-27 20:26:14 UTC (rev 81)
+++ lib/YAPC/Login.pm	2003-11-27 21:00:19 UTC (rev 82)
@@ -6,6 +6,7 @@
 use Data::Dumper;
 use base 'YAPC::DBI';
 use YAPC::Person;
+use YAPC::Error;
 __PACKAGE__->set_up_table('login');
 __PACKAGE__->has_a(   user_id => 'YAPC::Person' );
 
@@ -43,7 +44,7 @@
    my @people = YAPC::Person->search(email => $args{email}, password => $args{password});
    croak("Too many users fit the description\n") if @people > 1;
    croak("No match found\n") if @people == 0;
-   croak("Not acknowladged") if not $people[0]->acked;
+   throw YAPC::Error("Not acknowladged") if not $people[0]->acked;
 
    $self->create({user_id => $people[0]->id}); 
 }

Modified: lib/YAPC/Organizer.pm
===================================================================
--- lib/YAPC/Organizer.pm	2003-11-27 20:26:14 UTC (rev 81)
+++ lib/YAPC/Organizer.pm	2003-11-27 21:00:19 UTC (rev 82)
@@ -10,6 +10,7 @@
 use YAPC::Config;
 use YAPC::Login;
 use YAPC::Talk;
+use Error qw(:try);
 use Mail::Sendmail;
 use HTML::Template; # we use it explicitely hence we need it here.
 
@@ -399,16 +400,9 @@
        return $t->output;
     }
 
-    eval {
-      $login = YAPC::Login->new($q->Vars);
-    };
+    try {
+    $login = YAPC::Login->new($q->Vars);
 
-    if ($@) {
-      my $msg = $@;
-      $t->param(MESSAGE => 'Login failed.');
-      return $t->output;
-    }
-
     # success
     my $path = $ENV{REQUEST_URI};
     $path =~ s@[^/]*$@@; # remove everything after last /
@@ -424,6 +418,17 @@
      my $page = $q->param("next") || 'user_account';
      my $tm = $self->_server_page($page);
      return $tm->output;
+    } 
+    catch YAPC::Error with {
+      my $er = shift;
+      #$t->param(MESSAGE => "$er");
+      $t->param(MESSAGE => 'Login failed.');
+      return $t->output;
+    } 
+    catch Error with {
+      $t->param(MESSAGE => 'Login failed.');
+      return $t->output;
+    };
 }
 
 

Modified: t/02-login.t
===================================================================
--- t/02-login.t	2003-11-27 20:26:14 UTC (rev 81)
+++ t/02-login.t	2003-11-27 21:00:19 UTC (rev 82)
@@ -8,6 +8,7 @@
 use lib qw(t/lib);
 use YAPC::Test 'init_db';
 use YAPC::Test::Data;
+use Error qw(:try);
 
 BEGIN { use_ok( 'YAPC::Person' ); }
 BEGIN { use_ok( 'YAPC::Login' ); }
@@ -25,10 +26,16 @@
 
 
 ######## Login to not acknowladged account should fail
-eval {
-   YAPC::Login->new((email => $people[1]{email}, password => $people[1]{password}));
-};
-like($@, qr/Not acknowladged/, 'Login to not acknowladged account failed');
+{
+  my $ex;
+  try {
+     YAPC::Login->new((email => $people[1]{email}, password => $people[1]{password}));
+  }
+  catch YAPC::Error with {
+     $ex = shift;
+  };
+  like($ex, qr/Not acknowladged/, 'Login to not acknowladged account failed');
+}
 
 
 ######## acknowladge user

Generated at 23:10 on 27 Nov 2003 by mariachi 0.51