/** * Test whether exceptions are handled by the exception handler passed to the try catch wrapper */ public function testHandleExceptions() { $expectedException = new Exception(); $client = $this->getMockForAbstractClass(Redis::class, array('set')); $client->expects($this->once())->method('set')->will($this->throwException($expectedException)); $connection = $this->getMockForAbstractClass(Connection::class, array('getClient')); $connection->expects($this->any())->method('getClient')->will($this->returnValue($client)); $handler = $this->getMockBuilder(ExceptionHandler::class)->setMethods(array('handleException'))->getMock(); $handler->expects($this->once())->method('handleException')->with($expectedException)->will($this->returnValue(ExceptionStrategy::DISCARD())); $wrapper = new TryCatchWrapper($connection, $handler); $wrapper->set('testKey', 'testValue'); }
/** * @param Exception $e the Exception that was thrown * @param string $command the command that was being executed * @param array $args the arguments of the command * @throws \Plista\Core\Redis\Exception * @return bool */ private function handleException(Exception $e, $command, $args) { $excBrief = "{$e->getCode()}: {$e->getMessage()} ({$e->getFile()}:{$e->getLine()}) Stacktrace:\n" . $e->getTraceAsString(); // $excHash = md5($excBrief); // $curTime = Registry::getSystem()->getTime()->current(); // // $this->excLog[$excHash]['count']++; // $this->excLog[$excHash]['time'] = $curTime; // log the exception $this->log->warning("Caught Redis exception: {$excBrief}"); if ($this->exceptionHandler) { // notify the handler and get the strategy $context = ExceptionContext::fromFunctionAndArguments($command, $args); $strategy = $this->exceptionHandler->handleException($e, $context); } else { // use the default strategy $strategy = $this->defaultHandlerStrategy; } // default is to discard the exception if (!$strategy) { $strategy = ExceptionStrategy::DISCARD(); } // let's see what the handler wants us to do if ($strategy->equals(ExceptionStrategy::RETHROW)) { throw $e; } // this case is used by the FailoverWrapper if ($strategy->equals(ExceptionStrategy::RETURN_VALUE)) { return $strategy->returnValue; } // return false to signal failure. maybe interpreted by some callers as a valid value (which it // is in some cases), but that's not for us to worry about. return false; }