[Templates-cvs] cvs commit: TT3/lib/Template Component.pm

cvs@template-toolkit.org cvs@template-toolkit.org
Thu, 25 Mar 2004 16:20:11 +0000


cvs         04/03/25 16:20:10

  Modified:    lib/Template Component.pm
  Log:
  * added correct error handling for search() method
  
  Revision  Changes    Path
  1.2       +27 -43    TT3/lib/Template/Component.pm
  
  Index: Component.pm
  ===================================================================
  RCS file: /template-toolkit/TT3/lib/Template/Component.pm,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Component.pm	2004/03/25 14:35:53	1.1
  +++ Component.pm	2004/03/25 16:20:10	1.2
  @@ -32,7 +32,7 @@
   #     else
   #
   # REVISION
  -#   $Id: Component.pm,v 1.1 2004/03/25 14:35:53 abw Exp $
  +#   $Id: Component.pm,v 1.2 2004/03/25 16:20:10 abw Exp $
   #
   #========================================================================
   
  @@ -46,11 +46,12 @@
   use vars qw( $VERSION $DEBUG $ERROR $MAX_DEPTH );
   use base qw( Template::Base );
   
  -$VERSION   = sprintf("%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/);
  +$VERSION   = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
   $DEBUG     = 0 unless defined $DEBUG;
   $ERROR     = '';
   $MAX_DEPTH = 64;
   
  +our $THROW = 'component';
   
   
   #------------------------------------------------------------------------
  @@ -539,56 +540,39 @@
       # "Do The Right Thing" to see if the location contains the 
       # named resource and return it if it does.
   
  -    # we'll take a peek in locations to save calling locate()
  -    # unless we really have to
       my $locations = $self->{ locations }->{ $resource }
           || $self->locate($resource) || return;
   
  -#    $self->debug(scalar @$locations, " $resource location(s)\n") if $self->{ DEBUG };
  -
       my ($items, $fetch);
   
  -    foreach $items (@$locations) {
  -        if (ref($items) eq 'HASH') {
  -            # look for the item in the hash
  -            last if defined ($item = $items->{ $name });
  -        }
  -        elsif ($fetch = UNIVERSAL::can($items, 'fetch')) {
  -            # call on a provider to fetch it
  -            eval { $item = &$fetch($items, $name, @opts) };
  -            if ($@) {
  -                chomp $@;
  -                return $self->error($@);
  +    eval {
  +        # provide a useful exception type for errors thrown
  +        local $self->{ THROW } .= "search.$resource.$name";
  +
  +        foreach $items (@$locations) {
  +            if (ref($items) eq 'HASH') {
  +                # look for the item in the hash
  +                last if defined ($item = $items->{ $name });
               }
  -            
  -            # undefined value is an error
  -            return $self->error($items->error())
  -                unless defined $item;
  -            
  -            # true value is a result, otherwise continue
  -            last if $item;
  -        }
  -        elsif (UNIVERSAL::isa($items, 'CODE')) {
  -            eval { $item = &$items($self, $name, @opts) };
  -            if ($@) {
  -                chomp $@;
  -                return $self->error($@);
  +            elsif ($fetch = UNIVERSAL::can($items, 'fetch')) {
  +                # call on a provider to fetch it
  +                last if ($item = &$fetch($items, $name, @opts));
               }
  -            
  -            # undef value is an error - code should have already 
  -            # called $self->error($reason) to set message
  -            return unless defined $item;
  -            
  -            # true value is a result, otherwise continue
  -            last if $item;
  -        }
  -        else {
  -            $self->warning("invalid $resource collection: $items");
  +            elsif (UNIVERSAL::isa($items, 'CODE')) {
  +                # call subroutine to fetch it
  +                last if ($item = &$items($self, $name, @opts));
  +            }
  +            else {
  +                $self->warning("invalid $resource collection: $items");
  +            }
           }
  +    };
  +    if ($@) {
  +        chomp($@) unless ref $@;
  +        return $self->error($@);
       }
   
  -    return $item
  -        || $self->decline("$name not found in $resource");
  +    return $item || $self->decline("$name not found in $resource");
   }
   
   
  @@ -836,7 +820,7 @@
   
   =head1 VERSION
   
  -$Revision: 1.1 $
  +$Revision: 1.2 $
   
   =head1 COPYRIGHT