Simply wrap your execute statement in an eval block and catch the error. (I'd say use Try::Tiny but it doesn't play nice with DBI, at least for me.)
eval {
$sth->execute();
...
};
if ( $@ ) {
# log the full error message
write_log( $sth->errstr );
# and re-throw the common message
die 'HEY!!!! Something is messed up here!';
}
By all means keep
RaiseError => 1 and
PrintError => 0 for this to work.
EDIT:
On a side note, it is extremely poor practice (and bad security) to interpolate values directly into the SQL statement. Use placeholders instead and supply the values as params to the execute statement instead.
my $sql = q{INSERT INTO mytable VALUES ( ?,?,? )};
eval {
$dbh->do( $sql, undef, ($val1, $val2, $val3) );
};
if ( $@ ) }
# error trapping here
...
}
You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.