in reply to Mapping database errors to user errors
If there's a few known errors, you could check for those to display a different user message:my $errmsg = ''; my $sql = "INSERT INTO url (url, url_id) VALUES (?, ?)"; my @bind = ( 'blah.com', 3 ); $dbh->do($sql,{},@bind); if( $dbh->err ){ warn "INSERT url FAILED: " . $dbh->errstr; $errmsg = "There was an error adding the URL"; }
Even better is to try to trap the conditions that create the error .. for example, in this case, a SELECT to see if the "url" column already exists could be done first and if a row is found toss an error to the user.if( $dbh->err ){ warn "INSERT url FAILED: " . $dbh->errstr; if( $dbh->err eq '123' ){ $errmsg = "Please fix ___ before adding the URL."; }elsif( $dbh->errstr =~ /not unique/ ){ $errmsg = "This URL already exists."; }else{ $errmsg = "There was an error adding the URL"; } }
One more advantage of using the last two code blocks above is that you have code that's hit IFF a certain error condition arises, so if you check the test coverage it won't be 100% for these blocks unless you have explicit tests for those errors -- so it explicitly outlines the errors in the code, and makes it easier to list the possible run-time conditions to test for.my $tmp_id = $dbh->selectrow_array("select url_id from url where url = + ?",{},$url); if( $ct ){ $errmsg = "The URL '$url' already exists w/id=$tmp_id."; return; } ... INSERT INTO ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Mapping database errors to user errors
by Aristotle (Chancellor) on Nov 26, 2005 at 01:25 UTC | |
by Ovid (Cardinal) on Nov 26, 2005 at 06:01 UTC | |
by qq (Hermit) on Nov 27, 2005 at 17:27 UTC | |
by Aristotle (Chancellor) on Nov 27, 2005 at 18:26 UTC |