summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Colish <dcolish@gmail.com>2011-03-14 17:56:17 -0400
committerDan Colish <dcolish@gmail.com>2011-03-14 17:56:17 -0400
commitaefa71df8f47a70c772cf7721f881bfd74d9cf2a (patch)
treec1c3c457d8ad29ded5835f35a8656f884d791e72
parent6796d6bc9c7b7be904c31bc07b210e46f0a90652 (diff)
downloadredis-py-aefa71df8f47a70c772cf7721f881bfd74d9cf2a.tar.gz
add PubSubError to better report disallowed command when using pub/sub
-rw-r--r--redis/__init__.py3
-rw-r--r--redis/client.py11
-rw-r--r--redis/exceptions.py4
-rw-r--r--tests/server_commands.py1
4 files changed, 13 insertions, 6 deletions
diff --git a/redis/__init__.py b/redis/__init__.py
index 499f658..c7d2460 100644
--- a/redis/__init__.py
+++ b/redis/__init__.py
@@ -5,6 +5,7 @@ from redis.exceptions import (
ConnectionError,
DataError,
InvalidResponse,
+ PubSubError,
RedisError,
ResponseError,
)
@@ -15,5 +16,5 @@ __version__ = '2.2.3'
__all__ = [
'Redis', 'ConnectionPool',
'RedisError', 'ConnectionError', 'ResponseError', 'AuthenticationError',
- 'InvalidResponse', 'DataError',
+ 'InvalidResponse', 'DataError', 'PubSubError',
]
diff --git a/redis/client.py b/redis/client.py
index 7b1c1ae..57ea0f0 100644
--- a/redis/client.py
+++ b/redis/client.py
@@ -8,6 +8,7 @@ from redis.exceptions import (
AuthenticationError,
ConnectionError,
DataError,
+ PubSubError,
RedisError,
ResponseError,
WatchError,
@@ -229,8 +230,8 @@ class Redis(threading.local):
def _execute_command(self, command_name, command, **options):
subscription_command = command_name in self.SUBSCRIPTION_COMMANDS
if self.subscribed and not subscription_command:
- raise RedisError("Cannot issue commands other than SUBSCRIBE and "
- "UNSUBSCRIBE while channels are open")
+ raise PubSubError("Cannot issue commands other than SUBSCRIBE and "
+ "UNSUBSCRIBE while channels are open")
try:
self.connection.send(command, self)
if subscription_command:
@@ -319,7 +320,7 @@ class Redis(threading.local):
def shutdown(self):
"Shutdown the server"
if self.subscribed:
- raise RedisError("Can't call 'shutdown' from a pipeline'")
+ raise PubSubError("Can't call 'shutdown' when 'subscribed'")
try:
self.execute_command('SHUTDOWN')
except ConnectionError:
@@ -610,7 +611,7 @@ class Redis(threading.local):
Watches the values at keys ``names``, or None if the key doesn't exist
"""
if self.subscribed:
- raise RedisError("Can't call 'watch' from a pipeline'")
+ raise PubSubError("Can't call 'watch' when 'subscribed'")
return self.execute_command('WATCH', *names)
@@ -619,7 +620,7 @@ class Redis(threading.local):
Unwatches the value at key ``name``, or None of the key doesn't exist
"""
if self.subscribed:
- raise RedisError("Can't call 'unwatch' from a pipeline'")
+ raise PubSubError("Can't call 'unwatch' when 'subscribed'")
return self.execute_command('UNWATCH')
diff --git a/redis/exceptions.py b/redis/exceptions.py
index 193016e..ffa574b 100644
--- a/redis/exceptions.py
+++ b/redis/exceptions.py
@@ -18,5 +18,9 @@ class InvalidResponse(RedisError):
class DataError(RedisError):
pass
+class PubSubError(RedisError):
+ pass
+
class WatchError(RedisError):
pass
+
diff --git a/tests/server_commands.py b/tests/server_commands.py
index fb5f3f5..742f356 100644
--- a/tests/server_commands.py
+++ b/tests/server_commands.py
@@ -1216,6 +1216,7 @@ class ServerCommandsTestCase(unittest.TestCase):
time.sleep(0.01)
messages = []
+ self.assertRaises(redis.PubSubError, r.set, 'foo', 'bar')
# should receive a message for each subscribe/unsubscribe command
# plus a message for each iteration of the loop * num channels
# we hide the data messages that tell the client to unsubscribe