Re: expect()/popExpect()

From: Date: Fri, 01 Oct 2004 21:14:44 +0000
Subject: Re: expect()/popExpect()
References: 1 2 3 4 5 6  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-33608@lists.php.net to get a copy of this message
Greg Beaver wrote:
Lukas Smith wrote:
Here is the piece of code inside MDB2 which I would have to modify. Could you give me some more hints how I would need to transform the code in question? $this->expectError(MDB2_ERROR_NOSUCHTABLE); $result =& $this->_doQuery($query); $this->popExpect(); if (MDB2::isError($result)) { if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) {
    // handle the expected error ..
} }
The implementation of expectError() is pretty straightforward. You actually would only need to change the MDB2::isError(). If you would like to keep it, this is still possible, all you need to do is do the reverse of what I have here - push and log only if it is a MDB2_ERROR_NOSUCHTABLE, and pass everything else to the default callback. I'm assuming in this example, that there is a callback that MDB2 already has set which will end the script. If in fact, you want to handle the possibility of all callbacks set by the user, you'll need a bit more checking (if the callback is false, then grab the default callback and use it for _oldCallback())
Actually the idea is that the user can define anything he wants as the pear error handler and I just want to temporarily want to ignore that handler for a specific error code.
function doExpectError($err) {
    if (in_array($err['code'], $this->_expectedErrors)) {
        $this->_flag[] = $err; // one possibility
        return PEAR_ERRORSTACK_IGNORE;
    } else {
        if (is_callable($this->_oldCallback)) {
            return $this->_oldCallback($err);
        }
    }
} function expectError($errors) {
    if (!is_array($errors)) {
        $errors = array($errors);
    }
    $this->_oldCallback = $this->_stack->popCallback();
    if ($this->_oldCallback) { // save the existing callback
        $this->_stack->pushCallback($this->_oldCallback());
    }
    $this->_expectedErrors = $errors;
    $this->_stack->pushCallback(array(&$this, 'doExpectError'));
} function popExpect() {
    $this->_expectedErrors = array();
    $this->_stack->popCallback();
} ..... $this->expectError(MDB2_ERROR_NOSUCHTABLE); $result = &$this->doQuery(); $this->popExpect(); if (count($this->_flag) && $ondemand) {
    // handle the error
}
hmmm ... that still looks like alot of work. I can implement that much easier inside my own raiseError() method that I have already. anyways thx for the pointers I will ponder things :-) regards, Lukas

« previous php.pear.dev (#33608) next »