sumalatha has asked for the wisdom of the Perl Monks concerning the following question:

In my web application want to skip displaying of error messages showing full query on screen to endusers.

example: ## save the variables to procure.RECPO2 using INSERT

$insert = "INSERT INTO procure.RECPO2 (po_number, requesto +r, req_num, send_to, vend_name, buyer_name) VALUES ('" . $sPO . "', '" . $vReqName . "', '" . $vSource . "', '" . $vSendTo . "', '" . $vVenNm . "', '" . $vBuyer . "')"; ## if requisition # is zero and requester blank - don't write record if (length($vSource) gt 1 && length($vReqName) gt 1) { $st6 = $dbh->prepare_cached($insert) or die "Couldn't prepare insert: " . $dbh->errstr; $st6->execute() or die "Couldn't execute insert: " . $st6->errstr; }

Result:

DBD::Oracle::st execute failed: ORA-00001: unique constraint (PROCURE. +PCR_RECPO2 _PK) violated (DBD ERROR: OCIStmtExecute) [for Statement "INSERT INTO +procure.RE CPO2 (po_number, requestor, req_num, send_to, vend_name, buy +er_name) V ALUES (' 1625162', 'ADRIAN SAMPS +ON', '9371 46', 'asampson@ochsner.org', 'NEOTECH PRODUCTS INC', 'LAURA SPURGEON') +"] at reqN ote_sh line 148. Couldn't execute insert: ORA-00001: unique constraint (PROCURE.PCR_REC +PO2_PK) vi olated (DBD ERROR: OCIStmtExecute) at reqNote_sh line 148.

Here in my application don't want to display the complete query to end users, instead of that want to show common error message.

Please let me know how can i do it. Thanks in advance.

  • Comment on want to skip displaying of "DBD::Oracle::st execute failed:" error messages showing full query
  • Select or Download Code

Replies are listed 'Best First'.
Re: want to skip displaying of "DBD::Oracle::st execute failed:" error messages showing full query
by Corion (Patriarch) on Apr 07, 2015 at 06:58 UTC

    See DBI->connect and its parameters. Most likely, you want to construct your database handle as:

    my $dbh= DBI->connect($dsn, $user, $password, { PrintError => 0, Raise +Error => 1 } );

      Tied connecting to DBI as per user response. But still me getting the Software Error Statement displaying on screen.

      i want only content given in die to be displayed whenever there is any error.

      Please help me.

        Maybe you want to also set RaiseError to zero. But I really recommend first reading and understanding the documentation.

Re: want to skip displaying of "DBD::Oracle::st execute failed:" error messages showing full query
by boftx (Deacon) on Apr 07, 2015 at 17:04 UTC

    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.

      What problems did you encounter when using Try::Tiny (to catch DBI errors)?

        I'll have to go back and find a copy of the offending code and sanitize it. It is now accepted practice at $work to use eval for DBI operations, and Try::Tiny for everything else.

        You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.