Prechádzať zdrojové kódy

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

Daniele Alessandri 14 rokov pred
rodič
commit
cc2349c785

+ 2 - 2
lib/Predis/Network/PhpiredisConnection.php

@@ -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
lib/Predis/Network/StreamConnection.php

@@ -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'");

+ 1 - 1
lib/Predis/Protocols/Text/ResponseErrorHandler.php

@@ -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);
     }
 }

+ 1 - 1
lib/Predis/Protocols/Text/ResponseErrorSilentHandler.php

@@ -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
lib/Predis/ResponseError.php

@@ -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
lib/Predis/ServerException.php

@@ -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
test/PredisShared.php

@@ -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;