Error::Exception - Combines Error and Exception::Class with correct stringication


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

Index


Code Index:

NAME

Top

Error::Exception - Combines Error and Exception::Class with correct stringication

SYNOPSIS

Top

A base exception class that combines the functionality of the Error and Exception::Class packages and stringifies properly when uncaught even in Test::Unit context.

FUNCTIONS

Top

new

Instantiates the object and handles the initialization of the base classes properly.

stringify

Converts a derived exception into a usable string for debugging.

Throws

Nothing

AUTHOR

Top

Stephen Vance, <steve at vance.com>

BUGS

Top

Please report any bugs or feature requests to bug-error-exception at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Error-Exception. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Error::Exception

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Error-Exception

* CPAN Ratings

http://cpanratings.perl.org/d/Error-Exception

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Error-Exception

* Search CPAN

http://search.cpan.org/dist/Error-Exception

ACKNOWLEDGEMENTS

Top

Thank you to The MathWorks, Inc. for sponsoring this work and to the BaT Team for their valuable input, review, and contributions.

COPYRIGHT

Top

LICENSE

Top

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


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

# Copyright (C) 2008 Stephen Vance
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of the Perl Artistic License.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Perl
# Artistic License for more details.
# 
# You should have received a copy of the Perl Artistic License along
# with this library; if not, see:
#
#       http://www.perl.com/language/misc/Artistic.html
# 
# Designed and written by Stephen Vance (steve@vance.com) on behalf
# of The MathWorks, Inc.

package Error::Exception;

use strict;
use warnings;

use Exception::Class;
use base qw( Error Exception::Class::Base );

our $VERSION = '1.1';

# This "no critic" is open because I couldn't find the policy name
sub new { ## no critic
    my $class = shift;

    local $Error::Depth = $Error::Depth + 1; ## no critic 'Dynamic::ValidateAgainstSymbolTable'

    my $self = $class->SUPER::new(@_);

    $self->{'-text'} = $self->_stringify();
    return $self;
}

sub _stringify {     
    my $self = shift;
    
    my $text = ref( $self ) . ' thrown in ' . $self->file
                . ' at line ' .  $self->line . "\n";
    my $msg = $self->{'-text'};
    if( defined( $msg ) && length( $msg ) > 0 ) {
        $text .= "with message <<" . $msg . ">>\n";
    }

    my @fields = $self->Fields();
    if( scalar @fields > 0 ) {
        $text .= "with fields:\n";
        for my $field ( @fields ) {
            my $value = $self->$field();
            if( ! defined( $value ) ) {
                $value = 'undef';
            }
            elsif( ref( $value ) eq 'ARRAY' ) {
                $value = '[ ' . join( "\n", @{$value} ) . ' ]';
            }
            # Don't expect hashes or objects
            $text .= "\t" . $field . " = '" . $value . "'\n";
        }
    }

    return $text;
}

1;
__END__