Re: PEAR and MDB error management

From: Date: Sat, 21 Aug 2004 07:05:19 +0000
Subject: Re: PEAR and MDB error management
References: 1 2  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-14207@lists.php.net to get a copy of this message
Justin Patrin wrote:
I would like to test if MDB can connect to a database. If my dsn is not correct, then MDB::connect returns an error I can trap with MDB::isError, and display the message. However, the error is then passed to the main errorHandliung fonction as define in my page as: PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, "handleError");
I tried to change this script with : $this->expectError(MDB_ERROR_CONNECT_FAILED); $tmp_conn = & MDB::connect($dsn, $_SESSION['mdb_options']); $this->popExpect(); if(!$tmp_conn){ display error message }
You've set the error mode to CALLBACK, so all errors will be sent to the callback. If you don't want an error to be sent to the callback (i.e. returned instead) you should change the error handling before the call. IMHO setting the expect is only for errors you know you don't have to handle. Try: PEAR::pushErrorHandling(PEAR_ERROR_RETURN); $tmp_conn = & MDB::connect($dsn, $_SESSION['mdb_options']); PEAR::popErrorHandling(); if (PEAR::isError($tmp_conn)) {
    display error message
}
Yup this is the only way. expect() is unfortunately severely limited and only works as a dynamic method on an object instance. BTW: this is the best PEAR Error tutorial I know of http://phpmag.net/itr/online_artikel/psecom,id,330,nodeid,114.html regards, Lukas

« previous php.pear.general (#14207) next »