Skip to content

Commit f323369

Browse files
author
Sergio García Prado
committed
ISSUE #367
* Minor improvements related with exceptions.
1 parent b30075a commit f323369

File tree

7 files changed

+33
-12
lines changed

7 files changed

+33
-12
lines changed

packages/core/minos-microservice-aggregate/minos/aggregate/events/repositories/pg.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
UUID,
1111
)
1212

13-
from psycopg2 import (
14-
IntegrityError,
15-
)
1613
from psycopg2.sql import (
1714
SQL,
1815
Composable,
@@ -24,6 +21,7 @@
2421
NULL_UUID,
2522
Config,
2623
DatabaseMixin,
24+
IntegrityException,
2725
)
2826

2927
from ...exceptions import (
@@ -65,7 +63,7 @@ async def _submit(self, entry: EventEntry, **kwargs) -> EventEntry:
6563

6664
try:
6765
response = await self.submit_query_and_fetchone(query, params, lock=lock)
68-
except IntegrityError:
66+
except IntegrityException:
6967
raise EventRepositoryConflictException(
7068
f"{entry!r} could not be submitted due to a key (uuid, version, transaction) collision",
7169
await self.offset,

packages/core/minos-microservice-common/minos/common/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
AiopgDatabaseClient,
1818
DatabaseClient,
1919
DatabaseClientBuilder,
20+
DatabaseClientException,
2021
DatabaseClientPool,
2122
DatabaseLock,
2223
DatabaseLockPool,
2324
DatabaseMixin,
25+
IntegrityException,
2426
PostgreSqlLock,
2527
PostgreSqlLockPool,
2628
PostgreSqlMinosDatabase,

packages/core/minos-microservice-common/minos/common/database/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
AiopgDatabaseClient,
77
DatabaseClient,
88
DatabaseClientBuilder,
9+
DatabaseClientException,
10+
IntegrityException,
911
UnableToConnectException,
1012
)
1113
from .locks import (

packages/core/minos-microservice-common/minos/common/database/clients/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from .abc import (
22
DatabaseClient,
33
DatabaseClientBuilder,
4+
DatabaseClientException,
5+
IntegrityException,
46
UnableToConnectException,
57
)
68
from .aiopg import (

packages/core/minos-microservice-common/minos/common/database/clients/abc.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
from ...config import (
2525
Config,
2626
)
27+
from ...exceptions import (
28+
MinosException,
29+
)
2730

2831

2932
class DatabaseClient(ABC, BuildableMixin):
@@ -50,6 +53,7 @@ async def execute(self, *args, **kwargs) -> None:
5053
"""TODO"""
5154

5255
async def fetch_one(self, *args, **kwargs) -> Any:
56+
"""TODO"""
5357
return await self.fetch_all(*args, **kwargs).__anext__()
5458

5559
@abstractmethod
@@ -75,5 +79,13 @@ def with_config(self, config: Config) -> DatabaseClientBuilder:
7579
DatabaseClient.set_builder(DatabaseClientBuilder)
7680

7781

78-
class UnableToConnectException(Exception):
82+
class DatabaseClientException(MinosException):
83+
"""TODO"""
84+
85+
86+
class UnableToConnectException(DatabaseClientException):
87+
"""TODO"""
88+
89+
90+
class IntegrityException(DatabaseClientException):
7991
"""TODO"""

packages/core/minos-microservice-common/minos/common/database/clients/aiopg.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
ClosableQueue,
2323
)
2424
from psycopg2 import (
25+
IntegrityError,
2526
OperationalError,
2627
)
2728

2829
from .abc import (
2930
DatabaseClient,
31+
IntegrityException,
3032
UnableToConnectException,
3133
)
3234

@@ -92,8 +94,9 @@ async def _create_connection(self):
9294
host=self.host, port=self.port, dbname=self.database, user=self.user, password=self.password
9395
)
9496
except OperationalError as exc:
95-
logger.warning(f"There was an {exc!r} while trying to get a database connection.")
96-
raise UnableToConnectException
97+
msg = f"There was an {exc!r} while trying to get a database connection."
98+
logger.warning(msg)
99+
raise UnableToConnectException(msg)
97100

98101
logger.debug(f"Created {self.database!r} database connection identified by {id(self._connection)}!")
99102

@@ -171,7 +174,10 @@ async def execute(
171174
:return: This method does not return anything.
172175
"""
173176
await self._create_cursor(lock=lock)
174-
await self._cursor.execute(operation=operation, parameters=parameters, timeout=timeout)
177+
try:
178+
await self._cursor.execute(operation=operation, parameters=parameters, timeout=timeout)
179+
except IntegrityError as exc:
180+
raise IntegrityException(f"The requested operation raised a integrity error: {exc!r}")
175181

176182
async def _create_cursor(self, *args, lock: Optional[Hashable] = None, **kwargs):
177183
if self._cursor is None:

packages/core/minos-microservice-networks/minos/networks/brokers/subscribers/filtered/validators/duplicates/pg.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
UUID,
1010
)
1111

12-
from psycopg2 import (
13-
IntegrityError,
14-
)
1512
from psycopg2.sql import (
1613
SQL,
1714
)
@@ -20,6 +17,7 @@
2017
Builder,
2118
Config,
2219
DatabaseMixin,
20+
IntegrityException,
2321
)
2422

2523
from .abc import (
@@ -64,7 +62,7 @@ async def _is_unique(self, topic: str, uuid: UUID) -> bool:
6462
try:
6563
await self.submit_query(self._query_factory.build_insert_row(), {"topic": topic, "uuid": uuid})
6664
return True
67-
except IntegrityError:
65+
except IntegrityException:
6866
return False
6967

7068

@@ -84,6 +82,7 @@ def with_config(self, config: Config):
8482
PostgreSqlBrokerSubscriberDuplicateValidator.set_builder(PostgreSqlBrokerSubscriberDuplicateValidatorBuilder)
8583

8684

85+
# noinspection SqlNoDataSourceInspection
8786
class PostgreSqlBrokerSubscriberDuplicateValidatorQueryFactory:
8887
"""PostgreSql Broker Subscriber Duplicate Detector Query Factory class."""
8988

0 commit comments

Comments
 (0)