diff --git a/synapse/storage/directory.py b/synapse/storage/directory.py
index 52373a28a6..2be9c41374 100644
--- a/synapse/storage/directory.py
+++ b/synapse/storage/directory.py
@@ -14,10 +14,15 @@
# limitations under the License.
from ._base import SQLBaseStore
+
+from synapse.api.errors import SynapseError
+
from twisted.internet import defer
from collections import namedtuple
+import sqlite3
+
RoomAliasMapping = namedtuple(
"RoomAliasMapping",
@@ -75,13 +80,18 @@ class DirectoryStore(SQLBaseStore):
Returns:
Deferred
"""
- yield self._simple_insert(
- "room_aliases",
- {
- "room_alias": room_alias.to_string(),
- "room_id": room_id,
- },
- )
+ try:
+ yield self._simple_insert(
+ "room_aliases",
+ {
+ "room_alias": room_alias.to_string(),
+ "room_id": room_id,
+ },
+ )
+ except sqlite3.IntegrityError:
+ raise SynapseError(
+ 409, "Room alias %s already exists" % room_alias.to_string()
+ )
for server in servers:
# TODO(erikj): Fix this to bulk insert
@@ -95,6 +105,7 @@ class DirectoryStore(SQLBaseStore):
def delete_room_alias(self, room_alias):
return self.runInteraction(
+ "delete_room_alias",
self._delete_room_alias_txn,
room_alias,
)
|