Error::Hierarchy::Container - Container for hierarchical exceptions


Error-Hierarchy documentation Contained in the Error-Hierarchy distribution.

Index


Code Index:

NAME

Top

Error::Hierarchy::Container - Container for hierarchical exceptions

VERSION

Top

version 1.103530

SYNOPSIS

Top

    my $my_exception = create_some_exception();
    my %exception_args = (foo => 'bar');
    my $uuid1 = gen_some_uuid();
    my $uuid2 = gen_some_uuid();

    my $container = Error::Hierarchy::Container->new;
    $container->items_set_push($my_exception);
    $container->record('Some::Exception', %exception_args);
    $container->delete_by_uuid($uuid1, $uuid2);

DESCRIPTION

Top

This class implements a container for hierarchical exception objects. It is effectively a Data::Container but also has the following methods.

METHODS

Top

record

Takes an exception class name (a string) and a hash of arguments. First the exception is constructed with the given arguments, then it is added - using items_set_push() - to the container. It's really a shortcut that saves you from having to record the exception and then adding it to the container yourself.

delete_by_uuid

Takes a list of uuid values and deletes all those exceptions from the container whose uuid appears in the given list.

delete_duplicate_exceptions

Deletes duplicate exceptions. Two exceptions are considered to be the same if they are of the same class and have the same properties, as defined by the exception's properties_as_hash() method.

items_push

Overrides Data::Container's items_push() method by calling the before_push trigger before pushing. The list of items to be pushed is passed to the trigger. One possible use might be to warn if you try to push any items that are not derived from Error::Hierarchy. The trigger mechanism is based on Class::Trigger.

items_set_push

Similar to items_push().

INSTALLATION

Top

See perlmodinstall for information and options on installing Perl modules.

BUGS AND LIMITATIONS

Top

No bugs have been reported.

Please report any bugs or feature requests through the web interface at http://rt.cpan.org/Public/Dist/Display.html?Name=Error-Hierarchy.

AVAILABILITY

Top

The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit http://www.perl.com/CPAN/ to find a CPAN site near you, or see http://search.cpan.org/dist/Error-Hierarchy/.

The development version lives at http://github.com/hanekomu/Error-Hierarchy and may be cloned from git://github.com/hanekomu/Error-Hierarchy. Instead of sending patches, please fork this project using the standard git and github infrastructure.

AUTHOR

Top

Marcel Gruenauer <marcel@cpan.org>

COPYRIGHT AND LICENSE

Top


Error-Hierarchy documentation Contained in the Error-Hierarchy distribution.

use 5.008;
use strict;
use warnings;

package Error::Hierarchy::Container;
BEGIN {
  $Error::Hierarchy::Container::VERSION = '1.103530';
}

use Class::Trigger;
use Error::Hierarchy::Util 'load_class';

# ABSTRACT: Container for hierarchical exceptions
use parent qw(
  Data::Container
  Error::Hierarchy::Base
);

sub items_push {
    my ($self, @values) = @_;
    $self->call_trigger('before_push', @values);
    $self->SUPER::items_push(@values);

}

sub items_set_push {
    my ($self, @values) = @_;

    # Some methods won't work on non-E-H objects; report them so they appear
    # directly - e.g., in the error.log if running under mod_perl
    $self->call_trigger('before_push', @values);
    $self->SUPER::items_set_push(@values);
}

sub record {
    my ($self, $exception_class, %args) = @_;

    load_class $exception_class, 1;
    # make record() invisible to caller when reporting exception location
    local $Error::Depth = $Error::Depth + 1;
    $self->items_set_push($exception_class->record(%args));
}

# Given a list of uuid's, deletes all exceptions from the container whose uuid
# is one of those given.
sub delete_by_uuid {
    my ($self, @uuid) = @_;
    my %uuid;
    @uuid{@uuid} = ();
    $self->items(grep { !exists $uuid{$_} } $self->items);
}

sub delete_duplicate_exceptions {
    my $self = shift;
    my @items;
    my %seen;
    for my $exception ($self->items) {
        my %properties = $exception->properties_as_hash;
        $properties{__package} = ref $exception;
        my $signature =
          join ';' => map { $_ => $properties{$_} } sort keys %properties;
        next if $seen{$signature}++;
        push @items => $exception;
    }
    $self->items(@items);
    $self;
}
1;


__END__