Name

Exception::Class::DBI - \s-1DBI\s0 Exception objects

Synopsis

  use DBI;
  use Exception::Class::DBI;

  my $dbh = DBI->connect($dsn, $user, $pass, {
      PrintError  => 0,
      RaiseError  => 0,
      HandleError => Exception::Class::DBI->handler,
  });

  eval { $dbh->do($sql) };

  if (my $ex = $@) {
      print STDERR "DBI Exception:\n";
      print STDERR "  Exception Type: ", ref $ex, "\n";
      print STDERR "  Error:          ", $ex->error, "\n";
      print STDERR "  Err:            ", $ex->err, "\n";
      print STDERR "  Errstr:         ", $ex->errstr, "\n";
      print STDERR "  State:          ", $ex->state, "\n";
      print STDERR "  Return Value:   ", ($ex->retval || 'undef'), "\n";
  }

Description

This module offers a set of DBI-specific exception classes. They inherit from Exception::Class, the base class for all exception objects created by the Exception::Class module from the \s-1CPAN\s0. Exception::Class::DBI itself offers a single class method, \*(C`handler()\*(C', that returns a code reference appropriate for passing to the \s-1DBI\s0 \*(C`HandleError\*(C' attribute.

The exception classes created by Exception::Class::DBI are designed to be thrown in certain \s-1DBI\s0 contexts; the code reference returned by \*(C`handler()\*(C' and passed to the \s-1DBI\s0 \*(C`HandleError\*(C' attribute determines the context and throws the apopropriate exception.

Each of the Exception::Class::DBI classes offers a set of object accessor methods in addition to those provided by Exception::Class. These can be used to output detailed diagnostic information in the event of an exception.

Interface

Exception::Class::DBI inherits from Exception::Class, and thus its entire interface. Refer to the Exception::Class documentation for details.

Class Method

my $dbh = DBI->connect($data_source, $username, $auth, { PrintError => 0, RaiseError => 0, HandleError => Exception::Class::DBI->handler }); This method returns a code reference appropriate for passing to the \s-1DBI\s0 \*(C`HandleError\*(C' attribute. When \s-1DBI\s0 encounters an error, it checks its \*(C`PrintError\*(C', \*(C`RaiseError\*(C', and \*(C`HandleError\*(C' attributes to decide what to do about it. When \*(C`HandleError\*(C' has been set to a code reference, \s-1DBI\s0 executes it, passing it the error string that would be printed for \*(C`PrintError\*(C', the \s-1DBI\s0 handle object that was executing the method call that triggered the error, and the return value of that method call (usually \*(C`undef\*(C'). Using these arguments, the code reference provided by \*(C`handler()\*(C' determines what type of exception to throw. Exception::Class::DBI contains the subclasses detailed below, each relevant to the \s-1DBI\s0 handle that triggered the error.

Classes

Exception::Class::DBI creates a number of exception classes, each one specific to a particular \s-1DBI\s0 error context. Most of the object methods described below correspond to like-named attributes in the \s-1DBI\s0 itself. Thus the documentation below summarizes the \s-1DBI\s0 attribute documentation, so you should refer to \s-1DBI\s0 itself for more in-depth information.

Exception::Class::DBI

All of the Exception::Class::DBI classes documented below inherit from Exception::Class::DBI. It offers the several object methods in addition to those it inherits from its parent, Exception::Class. These methods correspond to the \s-1DBI\s0 dynamic attributes, as well as to the values passed to the \*(C`handler()\*(C' exception handler via the \s-1DBI\s0 \*(C`HandleError\*(C' attribute. Exceptions of this base class are only thrown when there is no \s-1DBI\s0 handle object executing, e.g. in the \s-1DBI\s0 \*(C`connect()\*(C' method. Note: This functionality is not yet implemented in \s-1DBI\s0 \*(-- see the discusion that starts here: <http://archive.develooper.com/[email protected]/msg01438.html>. my $error = $ex->error; Exception::Class::DBI actually inherits this method from Exception::Class. It contains the error string that \s-1DBI\s0 prints when its \*(C`PrintError\*(C' attribute is enabled, or \*(C`die\*(C's with when its <RaiseError> attribute is enabled. my $err = $ex->err; Corresponds to the $DBI::err dynamic attribute. Returns the native database engine error code from the last driver method called. my $errstr = $ex->errstr; Corresponds to the $DBI::errstr dynamic attribute. Returns the native database engine error message from the last driver method called. my $state = $ex->state; Corresponds to the $DBI::state dynamic attribute. Returns an error code in the standard \s-1SQLSTATE\s0 five character format. my $retval = $ex->retval; The first value being returned by the \s-1DBI\s0 method that failed (typically \*(C`undef\*(C'). my $db_handle = $ex->handle; The \s-1DBI\s0 handle appropriate to the exception class. For Exception::Class::DBI::DRH, it will be a driver handle. For Exception::Class::DBI::DBH it will be a database handle. And for Exception::Class::DBI::STH it will be a statement handle. If there is no handle thrown in the exception (because, say, the exception was thrown before a driver handle could be created), the \*(C`handle\*(C' will be \*(C`undef\*(C'.

Exception::Class::DBI::H

This class inherits from Exception::Class::DBI, and is the base class for all \s-1DBI\s0 handle exceptions (see below). It will not be thrown directly. Its methods correspond to the \s-1DBI\s0 attributes common to all handles. my $warn = $ex->warn; Boolean value indicating whether \s-1DBI\s0 warnings have been enabled. Corresponds to the \s-1DBI\s0 \*(C`Warn\*(C' attribute. my $active = $ex->active; Boolean value indicating whether the \s-1DBI\s0 handle that encountered the error is active. Corresponds to the \s-1DBI\s0 \*(C`Active\*(C' attribute. my $kids = $ex->kids; For a driver handle, Kids is the number of currently existing database handles that were created from that driver handle. For a database handle, Kids is the number of currently existing statement handles that were created from that database handle. Corresponds to the \s-1DBI\s0 \*(C`Kids\*(C' attribute. my $active_kids = $ex->active_kids; Like \*(C`kids\*(C', but only counting those that are \*(C`active\*(C' (as above). Corresponds to the \s-1DBI\s0 \*(C`ActiveKids\*(C' attribute. my $compat_mode = $ex->compat_mode; Boolean value indicating whether an emulation layer (such as Oraperl) enables compatible behavior in the underlying driver (e.g., DBD::Oracle) for this handle. Corresponds to the \s-1DBI\s0 \*(C`CompatMode\*(C' attribute. my $inactive_destroy = $ex->inactive_destroy; Boolean value indicating whether the \s-1DBI\s0 has disabled the database engine related effect of \*(C`DESTROY\*(C'ing a handle. Corresponds to the \s-1DBI\s0 \*(C`InactiveDestroy\*(C' attribute. my $trace_level = $ex->trace_level; Returns the \s-1DBI\s0 trace level set on the handle that encountered the error. Corresponds to the \s-1DBI\s0 \*(C`TraceLevel\*(C' attribute. my $fetch_hash_key_name = $ex->fetch_hash_key_name; Returns the attribute name the \s-1DBI\s0 \*(C`fetchrow_hashref()\*(C' method should use to get the field names for the hash keys. Corresponds to the \s-1DBI\s0 \*(C`FetchHashKeyName\*(C' attribute. my $chop_blanks = $ex->chop_blanks; Boolean value indicating whether \s-1DBI\s0 trims trailing space characters from fixed width character (\s-1CHAR\s0) fields. Corresponds to the \s-1DBI\s0 \*(C`ChopBlanks\*(C' attribute. my $long_read_len = $ex->long_read_len; Returns the maximum length of long fields (\*(L"blob\*(R", \*(L"memo\*(R", etc.) which the \s-1DBI\s0 driver will read from the database automatically when it fetches each row of data. Corresponds to the \s-1DBI\s0 \*(C`LongReadLen\*(C' attribute. my $long_trunc_ok = $ex->long_trunc_ok; Boolean value indicating whether the \s-1DBI\s0 will truncate values it retrieves from long fields that are longer than the value returned by \*(C`long_read_len()\*(C'. Corresponds to the \s-1DBI\s0 \*(C`LongTruncOk\*(C' attribute. my $taint = $ex->taint; Boolean value indicating whether data fetched from the database is considered tainted. Corresponds to the \s-1DBI\s0 \*(C`Taint\*(C' attribute.

Exception::Class::DBI::DRH

\s-1DBI\s0 driver handle exceptions objects. This class inherits from Exception::Class::DBI::H, and offers no extra methods of its own.

Exception::Class::DBI::DBH

\s-1DBI\s0 database handle exceptions objects. This class inherits from Exception::Class::DBI::H Its methods correspond to the \s-1DBI\s0 database handle attributes. my $auto_commit = $ex->auto_commit; Returns true if the database handle \*(C`AutoCommit\*(C' attribute is enabled. meaning that database changes cannot be rolled back. Corresponds to the \s-1DBI\s0 database handle \*(C`AutoCommit\*(C' attribute. my $db_name = $ex->db_name; Returns the \*(L"name\*(R" of the database. Corresponds to the \s-1DBI\s0 database handle \*(C`Name\*(C' attribute. my $statement = $ex->statement; Returns the statement string passed to the most recent call to the \s-1DBI\s0 \*(C`prepare()\*(C' method in this database handle. If it was the \*(C`prepare()\*(C' method that encountered the error and triggered the exception, the statement string will be the statement passed to \*(C`prepare()\*(C'. Corresponds to the \s-1DBI\s0 database handle \*(C`Statement\*(C' attribute. my $row_cache_size = $ex->row_cache_size; Returns the hint to the database driver indicating the size of the local row cache that the application would like the driver to use for future \*(C`SELECT\*(C' statements. Corresponds to the \s-1DBI\s0 database handle \*(C`RowCacheSize\*(C' attribute.

Exception::Class::DBI::STH

\s-1DBI\s0 statement handle exceptions objects. This class inherits from Exception::Class::DBI::H Its methods correspond to the \s-1DBI\s0 statement handle attributes. my $num_of_fields = $ex->num_of_fields; Returns the number of fields (columns) the prepared statement will return. Corresponds to the \s-1DBI\s0 statement handle \*(C`NUM_OF_FIELDS\*(C' attribute. my $num_of_params = $ex->num_of_params; Returns the number of parameters (placeholders) in the prepared statement. Corresponds to the \s-1DBI\s0 statement handle \*(C`NUM_OF_PARAMS\*(C' attribute. my $field_names = $ex->field_names; Returns a reference to an array of field names for each column. Corresponds to the \s-1DBI\s0 statement handle \*(C`NAME\*(C' attribute. my $type = $ex->type; Returns a reference to an array of integer values for each column. The value indicates the data type of the corresponding column. Corresponds to the \s-1DBI\s0 statement handle \*(C`TYPE\*(C' attribute. my $precision = $ex->precision; Returns a reference to an array of integer values for each column. For non-numeric columns, the value generally refers to either the maximum length or the defined length of the column. For numeric columns, the value refers to the maximum number of significant digits used by the data type (without considering a sign character or decimal point). Corresponds to the \s-1DBI\s0 statement handle \*(C`PRECISION\*(C' attribute. my $scale = $ex->scale; Returns a reference to an array of integer values for each column. Corresponds to the \s-1DBI\s0 statement handle \*(C`SCALE\*(C' attribute. my $nullable = $ex->nullable; Returns a reference to an array indicating the possibility of each column returning a null. Possible values are 0 (or an empty string) = no, 1 = yes, 2 = unknown. Corresponds to the \s-1DBI\s0 statement handle \*(C`NULLABLE\*(C' attribute. my $cursor_name = $ex->cursor_name; Returns the name of the cursor associated with the statement handle, if available. Corresponds to the \s-1DBI\s0 statement handle \*(C`CursorName\*(C' attribute. my $param_values = $ex->param_values; Returns a reference to a hash containing the values currently bound to placeholders. Corresponds to the \s-1DBI\s0 statement handle \*(C`ParamValues\*(C' attribute. my $statement = $ex->statement; Returns the statement string passed to the \s-1DBI\s0 \*(C`prepare()\*(C' method. Corresponds to the \s-1DBI\s0 statement handle \*(C`Statement\*(C' attribute. my $rows_in_cache = $ex->rows_in_cache; the number of unfetched rows in the cache if the driver supports a local row cache for \*(C`SELECT\*(C' statements. Corresponds to the \s-1DBI\s0 statement handle \*(C`RowsInCache\*(C' attribute.

Exception::Class::DBI::Unknown

Exceptions of this class are thrown when the context for a \s-1DBI\s0 error cannot be determined. Inherits from Exception::Class::DBI, but implements no methods of its own.

Note

Note: Not all of the attributes offered by the \s-1DBI\s0 are exploited by these exception classes. For example, the \*(C`PrintError\*(C' and \*(C`RaiseError\*(C' attributes seemed redundant. But if folks think it makes sense to include the missing attributes for the sake of completeness, let me know. Enough interest will motivate me to get them in.

Subclassing

It is possible to subclass Exception::Class::DBI. The trick is to subclass its subclasses, too. Similar to subclassing \s-1DBI\s0 itself, this means that the handle subclasses should exist as subnamespaces of your base subclass.

It's easier to explain with an example. Say that you wanted to add a new method to all \s-1DBI\s0 exceptions that outputs a nicely formatted error message. You might do it like this:

package MyApp::Ex::DBI; use base 'Exception::Class::DBI';

sub full_message { my $self = shift; return $self->SUPER::full_message unless $self->can('statement'); return $self->SUPER::full_message . ' [for Statement "' . $self->statement . '"]'; }

You can then use this subclass just like Exception::Class::DBI itself:

my $dbh = DBI->connect($dsn, $user, $pass, { PrintError => 0, RaiseError => 0, HandleError => MyApp::Ex::DBI->handler, });

And that's all well and good, except that none of Exception::Class::DBI's own subclasses inherit from your class, so most exceptions won't be able to use your spiffy new method.

The solution is to create subclasses of both the Exception::Class::DBI subclasses and your own base subclass, as long as they each use the same package name as your subclass, plus \*(L"H\*(R", \*(L"\s-1DRH\s0\*(R", \*(L"\s-1DBH\s0\*(R", \*(L"\s-1STH\s0\*(R", and \*(L"Unknown\*(R". Here's what it looks like:

package MyApp::Ex::DBI::H; use base 'MyApp::Ex::DBI', 'Exception::Class::DBI::H';

package MyApp::Ex::DBI::DRH; use base 'MyApp::Ex::DBI', 'Exception::Class::DBI::DRH';

package MyApp::Ex::DBI::DBH; use base 'MyApp::Ex::DBI', 'Exception::Class::DBI::DBH';

package MyApp::Ex::DBI::STH; use base 'MyApp::Ex::DBI', 'Exception::Class::DBI::STH';

package MyApp::Ex::DBI::Unknown; use base 'MyApp::Ex::DBI', 'Exception::Class::DBI::Unknown';

And then things should work just spiffy! Of course, you probably don't need the H subclass unless you want to add other methods for the \s-1DRH\s0, \s-1DBH\s0, and \s-1STH\s0 classes to inherit from.

To Do

  • I need to figure out a non-database specific way of testing \s-1STH\s0 exceptions. DBD::ExampleP works well for \s-1DRH\s0 and \s-1DBH\s0 exceptions, but not so well for \s-1STH\s0 exceptions.

Support

This module is stored in an open repository at the following address:

<https://svn.kineticode.com/Exception-Class-DBI/trunk/>

Patches against Exception::Class::DBI are welcome. Please send bug reports to <[email protected]>.

Author

David Wheeler <[email protected]>

See Also

You should really only be using this module in conjunction with Tim Bunce's \s-1DBI\s0, so it pays to be familiar with its documentation.

See the documentation for Dave Rolsky's Exception::Class module for details on the methods this module's classes inherit from it. There's lots more information in these exception objects, so use them!

COPYRIGHT AND LICENSE

Copyright (c) 2002-2008, David Wheeler. Some Rights Reserved.

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