NAME
OpenPlugin::Exception - Base class for exceptions in OpenPlugin
SYNOPSIS
# As a user of a module developed using OpenPlugin
eval { $OP->session->save( $session ) };
if ( $@ ) {
print "Error: $@", $@->creation_location, "\n";
}
# Or, get a stack trace
eval { $OP->session->save( $session ) };
if ( $@ ) {
print "Error: $@",
"Stack trace: ", $@->trace->as_string, "\n";
}
# Get all exceptions (including from subclasses that don't override
# throw()) since the stack was last cleared
my @errors = $OP->exception->get_stack;
print "Errors found:\n";
foreach my $e ( @errors ) {
print "ERROR: ", $e->creation_location, "\n";
}
# As a developer of a module which uses OpenPlugin
my $rv = eval { $dbh->do( $sql ) };
if ( $@ ) {
$@->throw( "There was an error! $@" );
}
# Throw an exception that subclasses OpenPlugin::Exception with extra
# fields
my $rv = eval { $dbh->do( $sql ) };
if ( $@ ) {
$OP->exception->DBI->throw( $@, { sql => $sql,
action => 'do' } );
}
DESCRIPTION
This class is the base for all exceptions in OpenPlugin. An exception is generally used to indicate some sort of error condition rather than a situation that might normally be encountered. For instance, you would not throw an exception if you tried to fetch()
a record not in a datastore. But you would throw an exception if the query failed because the database schema was changed and the SQL statement referred to removed fields.
You can easily create new classes of exceptions if you like, see SUBCLASSING below.
METHODS
throw( $message, [ \%params ] )
This is the main action method and often the only one you will use. It creates a new exception object and calls die
with the object. Before calling die
with it it first does the following:
- 1. We check
\%params
for any parameters matching fieldnames returned byget_fields()
, and if found set the field in the object to the parameter. - 2. Fill the object with the relevant calling information:
package
,filename
,line
,method
. - 3. Set the
trace
property of the object to a Devel::StackTrace object. - 4. Call
initialize()
so that subclasses can do any object initialization/tracking they need to do. (See SUBCLASSING below.) - 5. Track the object in our internal stack.
get_stack()
Returns a list of exceptions that had been put on the stack. This can be particularly useful when there are multiple errors thrown during the execution of your program, and you want to get information regarding each one.
my @errors = $OP->exception->get_stack;
print "Errors found:\n";
foreach my $e ( @errors ) {
print "ERROR: ", $e->creation_location, "\n";
}
Instead of $e-creation_location>, which gives you several pieces of information about each error, you can get individual pieces of information using the following individual methods:
print $e->state->{ message };
print $e->state->{ package };
print $e->state->{ filename };
print $e->state->{ line };
get_fields()
Returns a list of property names used for this class. If a subclass wants to add properties to the base exception object, the common idiom is:
my @FIELDS = qw( this that );
sub get_fields { return ( $_[0]->SUPER::get_fields(), @FIELDS ) }
So that all fields are represented.
creation_location
Returns a string with information about where the exception was thrown. It looks like (all on one line):
Created in [%package%] in method [%method%];
at file [%filename%] at line [%line%]
PROPERTIES
The following properties are default properties of an OpenPlugin::Exception object. Don't forget that one can add to this property list by subclassing this module. These properties can be accessed using:
$exception_object->state->{ $property_name };
message
This is the message the exception is created with -- there should be one with every exception. (It is bad form to throw an exception with no message.)
package
The package the exception was thrown from.
filename
The file the exception was thrown from.
line
The line number in filename
the exception was thrown from.
method
The subroutine the exception was thrown from.
trace
Returns a Devel::StackTrace object, which had been created at the point where the exception was thrown.
$@->state->{ trace }->as_string;
SUBCLASSING
It is very easy to create your own OpenPlugin or application errors:
package My::Custom::Exception;
use strict;
use base qw( OpenPlugin::Exception );
Easy! If you want to include different information that can be passed via new()
:
package My::Custom::Exception;
use strict;
use base qw( OpenPlugin::Exception );
my @FIELDS = qw( this that );
My::Custom::Exception->mk_accessors( @FIELDS );
sub get_fields { return ( $_[0]->SUPER::get_fields(), @FIELDS ) }
And now your custom exception can take extra parameters:
$self->My::Custom::Exception->throw( $@, { this => 'bermuda shorts',
that => 'teva sandals' });
If you want to do extra initialization, data checking or whatnot, just create a method initialize()
. It gets called just before the die
is called in throw()
. Example:
package My::Custom::Exception;
# ... as above
my $COUNT = 0;
sub initialize {
my ( $self, $params ) = @_;
$COUNT++;
if ( $COUNT > 5 ) {
$self->message(
$self->message . "-- More than five errors?! ($COUNT) Whattsamatta?" );
}
}
BUGS
None known.
TO DO
Nothing known.
SEE ALSO
Exception::Class for lots of good ideas.
Copyright (c) 2001-2002 Eric Andreychek. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHORS
Eric Andreychek <eric@openthought.net>
Chris Winters <chris@cwinters.com>