Skip to content

Commit

Permalink
Merge pull request #8737 from RasaHQ/8727-redis-error
Browse files Browse the repository at this point in the history
Fix Redis errors as RasaException subclass
  • Loading branch information
ancalita authored May 25, 2021
2 parents 57356a2 + 5438e72 commit e428b38
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/8727.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Catch `ConnectionError` during `TrackerStore` and `LockStore` creation and re-raise as `RasaException` subclass, `ConnectionException`.
7 changes: 5 additions & 2 deletions rasa/core/lock_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from async_generator import asynccontextmanager
from typing import Text, Union, Optional, AsyncGenerator

from rasa.shared.exceptions import RasaException
from rasa.shared.exceptions import RasaException, ConnectionException
import rasa.shared.utils.common
from rasa.core.constants import DEFAULT_LOCK_LIFETIME
from rasa.core.lock import TicketLock
Expand Down Expand Up @@ -43,8 +43,11 @@ def create(obj: Union["LockStore", EndpointConfig, None]) -> "LockStore":

if isinstance(obj, LockStore):
return obj
else:

try:
return _create_from_endpoint_config(obj)
except ConnectionError as error:
raise ConnectionException("Cannot connect to lock store.") from error

@staticmethod
def create_lock(conversation_id: Text) -> TicketLock:
Expand Down
1 change: 1 addition & 0 deletions rasa/core/tracker_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def create(
BotoCoreError,
pymongo.errors.ConnectionFailure,
sqlalchemy.exc.OperationalError,
ConnectionError,
) as error:
raise ConnectionException("Cannot connect to tracker store.") from error

Expand Down
14 changes: 14 additions & 0 deletions tests/core/test_lock_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
from rasa.core.constants import DEFAULT_LOCK_LIFETIME
from rasa.shared.constants import INTENT_MESSAGE_PREFIX
from rasa.core.lock import TicketLock
import rasa.core.lock_store
from rasa.core.lock_store import (
InMemoryLockStore,
LockError,
LockStore,
RedisLockStore,
DEFAULT_REDIS_LOCK_STORE_KEY_PREFIX,
)
from rasa.shared.exceptions import ConnectionException
from rasa.utils.endpoints import EndpointConfig


class FakeRedisLockStore(RedisLockStore):
Expand Down Expand Up @@ -88,6 +91,17 @@ def test_create_lock_store(lock_store: LockStore):
assert lock.conversation_id == conversation_id


def test_raise_connection_exception_redis_lock_store(monkeypatch: MonkeyPatch):
monkeypatch.setattr(
rasa.core.lock_store, "RedisLockStore", Mock(side_effect=ConnectionError())
)

with pytest.raises(ConnectionException):
LockStore.create(
EndpointConfig(username="username", password="password", type="redis")
)


@pytest.mark.parametrize("lock_store", [InMemoryLockStore(), FakeRedisLockStore()])
def test_serve_ticket(lock_store: LockStore):
conversation_id = "my id 1"
Expand Down
14 changes: 14 additions & 0 deletions tests/core/test_tracker_stores.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ def test_exception_tracker_store_from_endpoint_config(
assert "test exception" in str(e.value)


def test_raise_connection_exception_redis_tracker_store_creation(
domain: Domain, monkeypatch: MonkeyPatch, endpoints_path: Text
):
store = read_endpoint_config(endpoints_path, "tracker_store")
monkeypatch.setattr(
rasa.core.tracker_store,
"RedisTrackerStore",
Mock(side_effect=ConnectionError()),
)

with pytest.raises(ConnectionException):
TrackerStore.create(store, domain)


class HostExampleTrackerStore(RedisTrackerStore):
pass

Expand Down

0 comments on commit e428b38

Please sign in to comment.