Would folks be interested in the option of specifying a coderef for the
RaiseError option?


Instead of:

    $dbh = DBI->connect(
        $dsn, $user, $password,
        { RaiseError => 1, AutoCommit => 0 }
    );

you might then have:

    $dbh = DBI->connect(
        $dsn, $user, $password,
        { RaiseError => \&bug_out, AutoCommit => 0 }
    );
    sub bug_out {
        # log stuff, send email, run around in circles,
        # etc
    }



and in the DBI.pm code, instead of

    Carp::croak($msg) if $attr->{RaiseError};

there might be:

    if ( $attr->{RaiseError} ) {
        if ( ref $attr->{RaiseError} eq 'CODE' ) {
            &{ $attr->{RaiseError} }( $msg );
            return(); # ensure no more code is run
        }
        Carp::croak($msg);
    }


This sort of thing would seem to be backwards compatible, while offering
additional programmer flexibility (and maybe getting rid of the need to
use eval in some cases?).

Good Idea? Bad Idea? Ugly Idea?

On the face of it, it seems that any generic RaiseError coderef might
have trouble knowing enough about the current context to do anything
usefull with the error, but the coderef could use caller(), as well as
global variables. The caller() function could let you access the call
stack as far back as you wanted. If you wanted a simpler (?) method, you
could just do

    $dbh = DBI->connect(
        $dsn, $user, $password,
        { RaiseError => \&bug_out, AutoCommit => 0 }
    );
    my $line_number;
    sub bug_out {
        # log stuff, send email, run around in circles,
        # reference $line_number
    }
    #
    # ... lots of stuff happens
    #
    $line_number = __LINE__;
    # some DBI method that might raise an error
    $line_number = undef; # just to be safe
    #
    # ... lots of stuff happens
    #
    $line_number = __LINE__;
    # another DBI method that might raise an error
    $line_number = undef; # just to be safe


I think I'd rather use caller myself, or maybe have DBI pass the
RaiseError coderef the $msg as well as the caller() info at the
appropriate depth.

Feedback?

-matt

Reply via email to