WWW::Postini::Exception - Enhanced exception class


WWW-Postini documentation Contained in the WWW-Postini distribution.

Index


Code Index:

NAME

Top

WWW::Postini::Exception - Enhanced exception class

SYNOPSIS

Top

  use WWW::Postini::Exception;
  throw WWW::Postini::Exception('The sky is falling!');

DESCRIPTION

Top

Based on Exception::Class, this module adds support for recursive exception throwing. This permits exceptions to be caused by other exceptions, in a way very similar to Java's exceptions.

OBJECT METHODS

Top

throw($arg)
throw(%args)

Creates a new WWW::Postini::Exception object and die()s with it. If $arg is an instance of Exception::Class::Base, either directly or by way of subclassing, $arg will be set as the cause of the existing exception object.

Alternatively, if %args is passed with a cause attribute, that value will be set to the cause of the exception object.

For all other parameter-passing conventions of the throw() method, please refer to Exception::Class.

cause()
cause($object)

Get or set the cause of the exception.

Returns the exception object that caused the current exception. If $object is set, the original exception's cause is updated to reflect the new value.

SEE ALSO

Top

WWW::Postini, Exception::Class

AUTHOR

Top

Peter Guzis, <pguzis@cpan.org>

COPYRIGHT AND LICENSE

Top


WWW-Postini documentation Contained in the WWW-Postini distribution.

package WWW::Postini::Exception;

use strict;
use warnings;

use Exception::Class;

use vars qw( @ISA $VERSION );

@ISA = qw( Exception::Class::Base );
$VERSION = '0.01';

#################
## initializer ##
#################

sub _initialize {

	my $self = shift;
	my %params;
	
	if (@_ == 1) {
	
		# passing an exception object as a cause
		
		if (UNIVERSAL::isa($_[0], 'WWW::Postini::Exception')) {
			
			$params{'cause'} = shift;
			
		# passing error text
		
		} else {
		
			$params{'error'} = shift;
		
		}
	
	} else {
	
		%params = @_;
	
	}
	
	# cause object is defined
	
	if (defined $params{'cause'}) {
	
		# set cause and remove parameter unsupported by base class		
		my $cause = $self->cause(delete $params{'cause'});

		# call superclass initializer
		$self->SUPER::_initialize(@_);

		# prohibit superclass from using $! as the default message
		$self->{'message'} = ref $cause unless defined $params{'message'};
		
		my $trace = $self->trace();
		my $cause_trace = $cause->trace();
		my $frame_count = $trace->frame_count();

		# strip any frames already present in cause stack trace
		
		for my $idx (0..$frame_count - 1) {

			my $frame = $trace->frame($idx);

			if (grep $_->filename() eq $frame->filename()
				&& $_->line() == $frame->line(),
				@{$cause_trace->{'frames'}}
			) {
			
				splice @{$trace->{'frames'}}, $idx;
				last;
			
			}	
		
		}
		
	} else {
	
		$self->SUPER::_initialize(%params);
		
	}

}

######################
## accessor methods ##
######################

# cause

sub cause {

	my $self = shift;
	
	if (@_) {
	
		$self->{'cause'} = shift
		
	}
	
	$self->{'cause'};

}

1;

__END__