[Bps-public-commit] r17530 - in Net-Trac/trunk: .

trs at bestpractical.com trs at bestpractical.com
Fri Jan 2 14:45:49 EST 2009


Author: trs
Date: Fri Jan  2 14:45:48 2009
New Revision: 17530

Added:
   Net-Trac/trunk/TODO
Modified:
   Net-Trac/trunk/   (props changed)
   Net-Trac/trunk/lib/Net/Trac/Connection.pm
   Net-Trac/trunk/lib/Net/Trac/Ticket.pm
   Net-Trac/trunk/lib/Net/Trac/TicketSearch.pm

Log:
 r43404 at zot:  tom | 2009-01-02 14:27:53 -0500
 Move to an error model where we warn and return undef.


Added: Net-Trac/trunk/TODO
==============================================================================
--- (empty file)
+++ Net-Trac/trunk/TODO	Fri Jan  2 14:45:48 2009
@@ -0,0 +1,14 @@
+write perldoc!
+
+we should really cache validation data in the Connection.
+or just not use it at all
+
+review error handling -- esp. die on error usage in api
+
+regex escape?
+
+grep for XXX TODO as well
+
+
+Pull ticket data from a specific search, not the ticket's own CSV
+Get history from RSS feed for a ticket

Modified: Net-Trac/trunk/lib/Net/Trac/Connection.pm
==============================================================================
--- Net-Trac/trunk/lib/Net/Trac/Connection.pm	(original)
+++ Net-Trac/trunk/lib/Net/Trac/Connection.pm	Fri Jan  2 14:45:48 2009
@@ -47,11 +47,12 @@
     my $query   = shift;
     my $abs_url = $self->url . $query;
     $self->mech->get($abs_url);
-    $self->_die_on_error($abs_url);
-    return $self->mech->content;
+
+    if ( $self->_warn_on_error($abs_url) ) { return }
+    else { return $self->mech->content }
 }
 
-sub _die_on_error {
+sub _warn_on_error {
     my $self = shift;
     my $url  = shift;
     my $die  = 0;
@@ -74,14 +75,15 @@
         $die++;
     }
 
-    if ( $die ) { die "Request errored out.\n" }
-    else        { return undef }
+    # Returns TRUE if it got an error, for nicer conditionals when calling
+    if ( $die ) { warn "Request errored out.\n"; return 1; }
+    else        { return }
 }
 
 sub ensure_logged_in {
     my $self = shift;
     if ( !defined $self->logged_in ) {
-        $self->_fetch("/login");
+        $self->_fetch("/login") or return;
         $self->logged_in(1);
     }
     return $self->logged_in;
@@ -91,8 +93,12 @@
 sub _fetch_feed {
     my $self  = shift;
     my $query = shift;
-    my $feed  = XML::Feed->parse( URI->new( $self->url . $query ) )
-        or die XML::Feed->errstr;
+    my $feed  = XML::Feed->parse( URI->new( $self->url . $query ) );
+
+    if ( not $feed ) {
+        warn XML::Feed->errstr;
+        return;
+    }
 
     return $feed;
 }

Modified: Net-Trac/trunk/lib/Net/Trac/Ticket.pm
==============================================================================
--- Net-Trac/trunk/lib/Net/Trac/Ticket.pm	(original)
+++ Net-Trac/trunk/lib/Net/Trac/Ticket.pm	Fri Jan  2 14:45:48 2009
@@ -33,7 +33,7 @@
 
 sub valid_props {
     qw( id summary type status priority severity resolution owner reporter cc
-        description keywords component milestone version )
+        description keywords component milestone version time changetime )
 }
 
 for my $prop ( __PACKAGE__->valid_props ) {
@@ -49,7 +49,7 @@
 sub load {
     my $self = shift;
     my ($id) = validate_pos( @_, { type => SCALAR } );
-    $self->connection->_fetch( "/ticket/" . $id . "?format=csv" );
+    $self->connection->_fetch( "/ticket/" . $id . "?format=csv" ) or return;
 
     my $content  = $self->connection->mech->content;
     my $stateref = $self->connection->_csv_to_struct( data => \$content, key => 'id' );
@@ -77,7 +77,7 @@
 sub _get_new_ticket_form {
     my $self = shift;
     $self->connection->ensure_logged_in;
-    $self->connection->_fetch("/newticket");
+    $self->connection->_fetch("/newticket") or return;
     my $i = 1; # form number
     for my $form ( $self->connection->mech->forms() ) {
         return ($form,$i) if $form->find_input('field_reporter');
@@ -89,7 +89,7 @@
 sub _get_update_ticket_form {
     my $self = shift;
     $self->connection->ensure_logged_in;
-    $self->connection->_fetch("/ticket/".$self->id);
+    $self->connection->_fetch("/ticket/".$self->id) or return;
     my $i = 1; # form number;
     for my $form ( $self->connection->mech->forms() ) {
         return ($form,$i) if $form->find_input('field_reporter');
@@ -184,7 +184,7 @@
     );
 
     my $reply = $self->connection->mech->response;
-    $self->connection->_die_on_error( $reply->base->as_string );
+    $self->connection->_warn_on_error( $reply->base->as_string ) and return;
 
     if ($reply->title =~ /^#(\d+)/) {
         my $id = $1;
@@ -235,9 +235,6 @@
     );
 
     my $reply = $self->connection->mech->response;
-
-    # XXX TODO: use _die_on_error here?
-
     if ( $reply->is_success ) {
         return $self->load($self->id);
     }
@@ -274,7 +271,7 @@
 sub _get_add_attachment_form {
     my $self = shift;
     $self->connection->ensure_logged_in;
-    $self->connection->_fetch("/attachment/ticket/".$self->id."/?action=new");
+    $self->connection->_fetch("/attachment/ticket/".$self->id."/?action=new") or return;
     my $i = 1; # form number;
     for my $form ( $self->connection->mech->forms() ) {
         return ($form,$i) if $form->find_input('attachment');
@@ -299,7 +296,7 @@
     );
 
     my $reply = $self->connection->mech->response;
-    $self->connection->_die_on_error( $reply->base->as_string );
+    $self->connection->_warn_on_error( $reply->base->as_string ) and return;
 
     return $self->attachments->[-1];
 }
@@ -307,7 +304,8 @@
 sub _update_attachments {
     my $self = shift;
     $self->connection->ensure_logged_in;
-    my $content = $self->connection->_fetch("/attachment/ticket/".$self->id."/");
+    my $content = $self->connection->_fetch("/attachment/ticket/".$self->id."/")
+        or return;
     
     if ( $content =~ m{<dl class="attachments">(.+?)</dl>}is ) {
         my $html = $1 . '<dt>'; # adding a <dt> here is a hack that lets us

Modified: Net-Trac/trunk/lib/Net/Trac/TicketSearch.pm
==============================================================================
--- Net-Trac/trunk/lib/Net/Trac/TicketSearch.pm	(original)
+++ Net-Trac/trunk/lib/Net/Trac/TicketSearch.pm	Fri Jan  2 14:45:48 2009
@@ -16,12 +16,17 @@
     my $self  = shift;
     my %query = @_;
 
+    # Clear current results
+    $self->results([]);
+
     # Build a URL from the fields we want and the query
     my $url = '/query?format=csv&order=id&max=' . $self->limit;
     $url .= '&' . join '&', map { "col=$_" } Net::Trac::Ticket->valid_props;
     $url .= '&' . join '&', map { "$_=".$query{$_} } keys %query;
 
-    my $content = $self->connection->_fetch( $url );
+    my $content = $self->connection->_fetch( $url )
+        or return;
+
     my $data = $self->connection->_csv_to_struct( data => \$content, key => 'id', type => 'array' );
 
     my @tickets = ();
@@ -31,7 +36,6 @@
         push @tickets, $ticket if $id;
     }
 
-    $self->results([]);
     return $self->results( \@tickets );
 }
 



More information about the Bps-public-commit mailing list