Error::Hierarchy::Mixin - Provides often-used exception-related methods


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

Index


Code Index:

NAME

Top

Error::Hierarchy::Mixin - Provides often-used exception-related methods

VERSION

Top

version 1.103530

SYNOPSIS

Top

  package MyClass;
  use Error::Hierarchy::Mixin;

  Some::Exception->throw(foo => 'bar');

DESCRIPTION

Top

This mixin provides several methods that you will often use when dealing with exceptions.

It also overrides CORE::GLOBAL::die() so die() will produce an Error::Hierarchy::Internal::CustomMessage. If you have to use the original die(), use CORE::die() instead.

METHODS

Top

throw

Takes an exception class name (a string) and a hash of arguments. Loads the exception class, constructs an exception object, passes it the arguments and throws it by calling the exception object's throw() method. It populates the UNIVERSAL namespace, so all packages get this ability.

record

Like throw(), but records the exception using the exception object's record() method.

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::Mixin;
BEGIN {
  $Error::Hierarchy::Mixin::VERSION = '1.103530';
}
# ABSTRACT: Provides often-used exception-related methods
use Error;    # to get $Error::Depth

BEGIN {
    *CORE::GLOBAL::die = sub (@) {

        # Error.pm die()s as well, but we don't want an endless recursion.
        CORE::die(@_) if (caller)[0] eq 'Error' || ref $_[0];
        local $Error::Depth = $Error::Depth + 1;    # skip this level
        throw Error::Hierarchy::Internal::CustomMessage(
            custom_message => join(' ', @_),);
    };
}

# Any class that wants to throw an exception can simply use or inherit from
# this module and call 'throw Error::Whatever' without having to 'require' it
# first. By putting the throw() method in UNIVERSAL:: we catch method calls on
# exception classes that haven't been loaded yet. We load the class, then
# throw the exception.
sub UNIVERSAL::throw {

    # use Data::Dumper; print Dumper \@_; exit if ++(our $cnt) > 5;
    my ($exception_class, %args) = @_;

    # need to modify $Error::Depth (see Error.pm) to make certain parts
    # of the call stack invisible to caller()
    # +1 to make UNIVERSAL::throw() invisible
    # in case it wasn't loaded; to make sure $Error::Depth isn't undef
    require Error;
    local $Error::Depth = $Error::Depth + 1;
    eval "require $exception_class";
    CORE::die($@) if $@;
    $exception_class->throw(%args);
}

# Similar reasoning for record().
sub UNIVERSAL::record {
    my ($exception_class, %args) = @_;

    # need to modify $Error::Depth (see Error.pm) to make certain parts
    # of the call stack invisible to caller()
    # +1 to make UNIVERSAL::record() invisible
    # in case it wasn't loaded; to make sure $Error::Depth isn't undef
    require Error;
    local $Error::Depth = $Error::Depth + 1;
    eval "require $exception_class";
    CORE::die $@ if $@;
    $exception_class->record(%args);
}
1;


__END__