Mike Blezien [EMAIL PROTECTED] wrote:
> Hi,
>
> we have a small automatied billing system, written in perl
> using DBI to access our MySQL database. This system utilizes
> various control panels for customer, webmasters,...etc...
> where they log in to perform various functions, submit
> forms,..etc.. alot of activity with our MySQL db.
>
> what we'd like to do, is when there is any type of a
> database error, connection, queries,..etc,.. is then
> 'trapped' the error for later review, if needed, and display
> in the browser at the time of the error, a simple error
> messages to indicate there was a problem encoutered, nothing
> specific, then log the DBI error on the server for review.
>
> would it be best to log in each error to a file, using
> something like:
>
> if ($DBI::errstr) { $err_mesg = $DBI::errstr };
>
> then write this to a separate error logfile ??
>
> and is it best to set the PrintError=1 & RasieError=1 for
> this type of procedure ??
>
> appreciate any suggestions or tips,
Here's my take - I do pretty much exactly what the
DBI perldocs ('perldoc DBI' at a command prompt) suggest
for error handling using 'eval' and Transactions. Here's
a snippet from the DBI perldocs on 'Transactions':
The recommended way to implement robust transactions in Perl applica-
tions is to use "RaiseError" and "eval { ... }" (which is very fast,
unlike "eval "...""). For example:
$dbh->{AutoCommit} = 0; # enable transactions, if possible
$dbh->{RaiseError} = 1;
eval {
foo(...) # do lots of work here
bar(...) # including inserts
baz(...) # and updates
$dbh->commit; # commit the changes if we get this far
};
if ($@) {
warn "Transaction aborted because $@";
$dbh->rollback; # undo the incomplete changes
# add other application on-error-clean-up code here
}
If any errors occur in the eval, the RaiseError will cause
a die and the $@ variable will be populated with the error
message. So, in the 'if ($@) {' block, you can do whatever
you want to - where it says 'warn...', if you want to display
some generic error message you can do that, and if you want
to log the error you can do that too. I typically log errors
to the webserver(apache) error log by printing errors to
STDERR, which is exactly what 'warn' does above.
HTH.
--
Hardy Merrill
Red Hat, Inc.