/** * Offers a generic and reusable method to handle exceptions generated by * a connection object. * * @param CommunicationException $exception Exception. */ public static function handle(CommunicationException $exception) { if ($exception->shouldResetConnection()) { $connection = $exception->getConnection(); if ($connection->isConnected()) { $connection->disconnect(); } } throw $exception; }
/** * {@inheritdoc} */ public function handle(CompositeConnectionInterface $connection, $payload) { $length = (int) $payload; if ("{$length}" !== $payload) { CommunicationException::handle(new ProtocolException($connection, "Cannot parse '{$payload}' as a valid length of a multi-bulk response.")); } if ($length === -1) { return; } $list = array(); if ($length > 0) { $handlersCache = array(); $reader = $connection->getProtocol()->getResponseReader(); for ($i = 0; $i < $length; ++$i) { $header = $connection->readLine(); $prefix = $header[0]; if (isset($handlersCache[$prefix])) { $handler = $handlersCache[$prefix]; } else { $handler = $reader->getHandler($prefix); $handlersCache[$prefix] = $handler; } $list[$i] = $handler->handle($connection, substr($header, 1)); } } return $list; }
/** * Handles a multi-bulk reply returned by Redis. * * @param ComposableConnectionInterface $connection Connection to Redis. * @param string $lengthString Number of items in the multi-bulk reply. * @return array */ public function handle(ComposableConnectionInterface $connection, $lengthString) { $length = (int) $lengthString; if ("{$length}" !== $lengthString) { CommunicationException::handle(new ProtocolException($connection, "Cannot parse '{$lengthString}' as multi-bulk length")); } if ($length === -1) { return null; } $list = array(); if ($length > 0) { $handlersCache = array(); $reader = $connection->getProtocol()->getReader(); for ($i = 0; $i < $length; $i++) { $header = $connection->readLine(); $prefix = $header[0]; if (isset($handlersCache[$prefix])) { $handler = $handlersCache[$prefix]; } else { $handler = $reader->getHandler($prefix); $handlersCache[$prefix] = $handler; } $list[$i] = $handler->handle($connection, substr($header, 1)); } } return $list; }
/** * Handles a multi-bulk reply returned by Redis in a streamable fashion. * * @param ComposableConnectionInterface $connection Connection to Redis. * @param string $lengthString Number of items in the multi-bulk reply. * @return MultiBulkResponseSimple */ public function handle(ComposableConnectionInterface $connection, $lengthString) { $length = (int) $lengthString; if ("{$length}" != $lengthString) { CommunicationException::handle(new ProtocolException($connection, "Cannot parse '{$lengthString}' as multi-bulk length")); } return new MultiBulkResponseSimple($connection, $length); }
/** * {@inheritdoc} */ public function handle(CompositeConnectionInterface $connection, $payload) { $length = (int) $payload; if ("{$length}" != $payload) { CommunicationException::handle(new ProtocolException($connection, "Cannot parse '{$payload}' as a valid length for a multi-bulk response.")); } return new MultiBulkIterator($connection, $length); }
/** * @group disconnected * @expectedException Predis\CommunicationException * @expectedExceptionMessage Communication error */ public function testCommunicationExceptionHandling() { $connection = $this->getMock('Predis\\Connection\\SingleConnectionInterface'); $connection->expects($this->once())->method('isConnected')->will($this->returnValue(true)); $connection->expects($this->once())->method('disconnect'); $exception = $this->getException($connection, 'Communication error'); CommunicationException::handle($exception); }
/** * {@inheritdoc} */ public function handle(CompositeConnectionInterface $connection, $payload) { if (is_numeric($payload)) { return (int) $payload; } if ($payload !== 'nil') { CommunicationException::handle(new ProtocolException($connection, "Cannot parse '{$payload}' as a valid numeric response.")); } return; }
/** * Handles an integer reply returned by Redis. * * @param ComposableConnectionInterface $connection Connection to Redis. * @param string $number String representation of an integer. * @return int */ public function handle(ComposableConnectionInterface $connection, $number) { if (is_numeric($number)) { return (int) $number; } if ($number !== 'nil') { CommunicationException::handle(new ProtocolException($connection, "Cannot parse '{$number}' as numeric response")); } return null; }
/** * {@inheritdoc} */ public function handle(CompositeConnectionInterface $connection, $payload) { if (is_numeric($payload)) { $integer = (int) $payload; return $integer == $payload ? $integer : $payload; } if ($payload !== 'nil') { CommunicationException::handle(new ProtocolException($connection, "Cannot parse '{$payload}' as a valid numeric response [{$connection->getParameters()}]")); } return; }
/** * Handles a bulk reply returned by Redis. * * @param ComposableConnectionInterface $connection Connection to Redis. * @param string $lengthString Bytes size of the bulk reply. * @return string */ public function handle(ComposableConnectionInterface $connection, $lengthString) { $length = (int) $lengthString; if ("{$length}" !== $lengthString) { CommunicationException::handle(new ProtocolException($connection, "Cannot parse '{$lengthString}' as bulk length")); } if ($length >= 0) { return substr($connection->readBytes($length + 2), 0, -2); } if ($length == -1) { return null; } }
/** * {@inheritdoc} */ public function handle(CompositeConnectionInterface $connection, $payload) { $length = (int) $payload; if ("{$length}" !== $payload) { CommunicationException::handle(new ProtocolException($connection, "Cannot parse '{$payload}' as a valid length for a bulk response.")); } if ($length >= 0) { return substr($connection->readBuffer($length + 2), 0, -2); } if ($length == -1) { return null; } CommunicationException::handle(new ProtocolException($connection, "Value '{$payload}' is not a valid length for a bulk response.")); return; }
/** * {@inheritdoc} */ public function read(CompositeConnectionInterface $connection) { $chunk = $connection->readLine(); $prefix = $chunk[0]; $payload = substr($chunk, 1); switch ($prefix) { case '+': return new StatusResponse($payload); case '$': $size = (int) $payload; if ($size === -1) { return; } return substr($connection->readBuffer($size + 2), 0, -2); case '*': $count = (int) $payload; if ($count === -1) { return; } if ($this->mbiterable) { return new MultiBulkIterator($connection, $count); } $multibulk = array(); for ($i = 0; $i < $count; ++$i) { $multibulk[$i] = $this->read($connection); } return $multibulk; case ':': $integer = (int) $payload; return $integer == $payload ? $integer : $payload; case '-': return new ErrorResponse($payload); default: CommunicationException::handle(new ProtocolException($connection, "Unknown response prefix: '{$prefix}' [{$connection->getParameters()}]")); return; } }
/** * {@inheritdoc} */ public function read(ComposableConnectionInterface $connection) { $chunk = $connection->readLine(); $prefix = $chunk[0]; $payload = substr($chunk, 1); switch ($prefix) { case '+': switch ($payload) { case 'OK': return true; case 'QUEUED': return new ResponseQueued(); default: return $payload; } case '$': $size = (int) $payload; if ($size === -1) { return null; } return substr($connection->readBytes($size + 2), 0, -2); case '*': $count = (int) $payload; if ($count === -1) { return null; } if ($this->mbiterable) { return new MultiBulkResponseSimple($connection, $count); } $multibulk = array(); for ($i = 0; $i < $count; $i++) { $multibulk[$i] = $this->read($connection); } return $multibulk; case ':': return (int) $payload; case '-': return new ResponseError($payload); default: CommunicationException::handle(new ProtocolException($connection, "Unknown prefix: '{$prefix}'")); } }
/** * Handles protocol errors generated while reading responses from a * connection. * * @param CompositeConnectionInterface $connection Redis connection that generated the error. * @param string $message Error message. */ protected function onProtocolError(CompositeConnectionInterface $connection, $message) { CommunicationException::handle(new ProtocolException($connection, "{$message} [{$connection->getParameters()}]")); }
/** * Helper method for protocol errors encountered inside the transaction. * * @param string $message Error message. */ private function onProtocolError($message) { // Since a MULTI/EXEC block cannot be initialized when using aggregate // connections we can safely assume that Predis\Client::getConnection() // will return a Predis\Connection\NodeConnectionInterface instance. CommunicationException::handle(new ProtocolException($this->client->getConnection(), $message)); }
/** * Helper method used to handle a protocol error generated while reading a * reply from a connection to Redis. * * @param ComposableConnectionInterface $connection Connection to Redis that generated the error. * @param string $message Error message. */ private function protocolError(ComposableConnectionInterface $connection, $message) { CommunicationException::handle(new ProtocolException($connection, $message)); }
/** * Helper method to handle protocol errors. * * @param string $message Error message. */ protected function onProtocolError($message) { CommunicationException::handle(new ProtocolException($this, "{$message} [{$this->parameters->scheme}://{$this->getIdentifier()}]")); }
/** * Helper method to handle protocol errors. * * @param string $message Error message. */ protected function onProtocolError($message) { CommunicationException::handle(new ProtocolException($this, "{$message} [{$this->getParameters()}]")); }
/** * Helper method to handle protocol errors. * * @param string $message Error message. */ protected function onProtocolError($message) { CommunicationException::handle(new ProtocolException($this, static::createExceptionMessage($message))); }
/** * Handles protocol errors generated while reading responses from a * connection. * * @param CompositeConnectionInterface $connection Redis connection that generated the error. * @param string $message Error message. */ protected function onProtocolError(CompositeConnectionInterface $connection, $message) { CommunicationException::handle(new ProtocolException($connection, $message)); }
/** * Helper method to handle protocol errors. * * @param string $message Error message. */ protected function onProtocolError($message) { CommunicationException::handle(new ProtocolException($this, $message)); }