Skip to content

Commit

Permalink
never raise exceptions but return them
Browse files Browse the repository at this point in the history
coherency with other methods
  • Loading branch information
thefab committed Aug 27, 2015
1 parent b5defcd commit e34abc0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
7 changes: 2 additions & 5 deletions tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ def test_empty_pipeline(self):
c = Client()
yield c.connect()
p = Pipeline()
try:
yield c.call(p)
raise Exception("not raised ClientError")
except ClientError:
pass
res = yield c.call(p)
self.assertTrue(isinstance(res, ClientError))
c.disconnect()
8 changes: 2 additions & 6 deletions tests/test_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from tornadis.pubsub import PubSubClient
from tornadis.client import Client
from tornadis.exceptions import ClientError
from support import test_redis_or_raise_skiptest


Expand Down Expand Up @@ -91,9 +90,6 @@ def test_issue17(self):
def test_empty_subscribe(self):
c = PubSubClient()
yield c.connect()
try:
yield c.pubsub_subscribe()
raise Exception("ClientError not raised")
except ClientError:
pass
res = yield c.pubsub_subscribe()
self.assertFalse(res)
c.disconnect()
13 changes: 7 additions & 6 deletions tornadis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,14 @@ def async_call(self, *args, **kwargs):
- callback
Function called (with the result as argument) when the result
is available. If not set, the reply is silently discarded. In
case of connection errors, the callback is called with a
ConnectionError object as argument.
case of errors, the callback is called with a
TornadisException object as argument.
Args:
*args: full redis command as variable length argument list or
a Pipeline object (as a single argument).
**kwargs: options as keyword parameters.
Raises:
ClientError: your Pipeline object is empty.
Examples:
>>> def cb(result):
Expand Down Expand Up @@ -254,7 +251,11 @@ def _call(self, *args, **kwargs):
fn = self._pipelined_call
pipeline = args[0]
if pipeline.number_of_stacked_calls == 0:
raise ClientError("empty pipeline")
excep = ClientError("empty pipeline")
if callback:
kwargs['callback'](excep)
else:
return tornado.gen.maybe_future(excep)
arguments = (pipeline,)
else:
if "__multiple_replies" in kwargs:
Expand Down
24 changes: 11 additions & 13 deletions tornadis/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import tornado.ioloop
import tornado.gen
from datetime import timedelta
import logging

from tornadis.client import Client
from tornadis.exceptions import ConnectionError, ClientError


LOG = logging.getLogger(__name__)


class PubSubClient(Client):
"""High level specific object to interact with pubsub redis.
Expand Down Expand Up @@ -53,9 +57,6 @@ def pubsub_subscribe(self, *args):
Returns:
Future: Future with True as result if the subscribe is ok.
Raises:
ClientError: if you don't provide at least one channel
Examples:
>>> yield client.pubsub_subscribe("channel1", "channel2")
Expand All @@ -73,9 +74,6 @@ def pubsub_psubscribe(self, *args):
Returns:
Future: Future with True as result if the subscribe is ok.
Raises:
ClientError: if you don't provide at least one pattern
Examples:
>>> yield client.pubsub_psubscribe("channel*", "foo*")
Expand All @@ -85,7 +83,8 @@ def pubsub_psubscribe(self, *args):
@tornado.gen.coroutine
def _pubsub_subscribe(self, command, *args):
if len(args) == 0:
raise ClientError("you must provide at least one argument")
LOG.warning("you must provide at least one argument")
raise tornado.gen.Return(False)
results = yield Client.call(self, command, *args,
__multiple_replies=len(args))
for reply in results:
Expand Down Expand Up @@ -156,14 +155,13 @@ def pubsub_pop_message(self, deadline=None):
Returns:
Future with the popped message as result (or None if timeout
or ConnectionError object in case of connection errors).
Raises:
ClientError: when you are not subscribed to anything
or ConnectionError object in case of connection errors
or ClientError object if you are not subscribed)
"""
if not self.subscribed:
raise ClientError("you must subscribe before using "
"pubsub_pop_message")
excep = ClientError("you must subscribe before using "
"pubsub_pop_message")
raise tornado.gen.Return(excep)
reply = None
try:
reply = self._reply_list.pop(0)
Expand Down

0 comments on commit e34abc0

Please sign in to comment.