Change how error messages for server-side errors are handled.

This commit is contained in:
Daniele Alessandri
2011-05-14 10:26:12 +02:00
parent 8efe76ad60
commit cc2349c785
7 changed files with 37 additions and 24 deletions
+2 -2
View File
@@ -86,11 +86,11 @@ class PhpiredisConnection extends ConnectionBase {
private function getErrorHandler($throwErrors = true) {
if ($throwErrors) {
return function($errorMessage) {
throw new ServerException(substr($errorMessage, 4));
throw new ServerException($errorMessage);
};
}
return function($errorMessage) {
return new ResponseError(substr($errorMessage, 4));
return new ResponseError($errorMessage);
};
}
+2 -3
View File
@@ -163,11 +163,10 @@ class StreamConnection extends ConnectionBase {
return (int) $payload;
case '-': // error
$errorMessage = substr($payload, 4);
if ($this->_throwErrors) {
throw new ServerException($errorMessage);
throw new ServerException($payload);
}
return new ResponseError($errorMessage);
return new ResponseError($payload);
default:
$this->onProtocolError("Unknown prefix: '$prefix'");
@@ -8,6 +8,6 @@ use Predis\Network\IConnectionComposable;
class ResponseErrorHandler implements IResponseHandler {
public function handle(IConnectionComposable $connection, $errorMessage) {
throw new ServerException(substr($errorMessage, 4));
throw new ServerException($errorMessage);
}
}
@@ -8,6 +8,6 @@ use Predis\Network\IConnectionComposable;
class ResponseErrorSilentHandler implements IResponseHandler {
public function handle(IConnectionComposable $connection, $errorMessage) {
return new ResponseError(substr($errorMessage, 4));
return new ResponseError($errorMessage);
}
}
+9 -5
View File
@@ -5,17 +5,21 @@ namespace Predis;
class ResponseError {
public $skipParse = true;
private $_message;
private $_type;
public function __construct($message) {
$this->_message = $message;
$this->_type = substr($message, 0, strpos($message, ' '));
}
public function __get($property) {
if ($property === 'error') {
return true;
}
if ($property === 'message') {
return $this->_message;
switch ($property) {
case 'error':
return true;
case 'message':
return $this->_message;
case 'type':
return $this->_type;
}
}
+11 -1
View File
@@ -3,8 +3,18 @@
namespace Predis;
class ServerException extends PredisException {
// Server-side errors
private $_errorType;
public function __construct($message) {
parent::__construct($message);
$this->_errorType = substr($message, 0, strpos($message, ' '));
}
public function toResponseError() {
return new ResponseError($this->getMessage());
}
public function getErrorType() {
return $this->_errorType;
}
}
+11 -11
View File
@@ -25,17 +25,17 @@ class RC {
const DEFAULT_DATABASE = 15;
const WIPE_OUT = 1;
const EXCEPTION_WRONG_TYPE = 'Operation against a key holding the wrong kind of value';
const EXCEPTION_NO_SUCH_KEY = 'no such key';
const EXCEPTION_OUT_OF_RANGE = 'index out of range';
const EXCEPTION_OFFSET_RANGE = 'offset is out of range';
const EXCEPTION_INVALID_DB_IDX = 'invalid DB index';
const EXCEPTION_VALUE_NOT_INT = 'value is not an integer';
const EXCEPTION_EXEC_NO_MULTI = 'EXEC without MULTI';
const EXCEPTION_SETEX_TTL = 'invalid expire time in SETEX';
const EXCEPTION_HASH_VALNOTINT = 'hash value is not an integer';
const EXCEPTION_BIT_VALUE = 'bit is not an integer or out of range';
const EXCEPTION_BIT_OFFSET = 'bit offset is not an integer or out of range';
const EXCEPTION_WRONG_TYPE = 'ERR Operation against a key holding the wrong kind of value';
const EXCEPTION_NO_SUCH_KEY = 'ERR no such key';
const EXCEPTION_OUT_OF_RANGE = 'ERR index out of range';
const EXCEPTION_OFFSET_RANGE = 'ERR offset is out of range';
const EXCEPTION_INVALID_DB_IDX = 'ERR invalid DB index';
const EXCEPTION_VALUE_NOT_INT = 'ERR value is not an integer';
const EXCEPTION_EXEC_NO_MULTI = 'ERR EXEC without MULTI';
const EXCEPTION_SETEX_TTL = 'ERR invalid expire time in SETEX';
const EXCEPTION_HASH_VALNOTINT = 'ERR hash value is not an integer';
const EXCEPTION_BIT_VALUE = 'ERR bit is not an integer or out of range';
const EXCEPTION_BIT_OFFSET = 'ERR bit offset is not an integer or out of range';
private static $_connection;