Skip to content

Commit a5d79af

Browse files
author
Sergio García Prado
committed
ISSUE #43
* Improve exception handling.
1 parent b90ddfe commit a5d79af

File tree

1 file changed

+20
-17
lines changed
  • packages/plugins/minos-database-aiopg/minos/plugins/aiopg

1 file changed

+20
-17
lines changed

packages/plugins/minos-database-aiopg/minos/plugins/aiopg/clients.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __init__(
6565
super().__init__(
6666
*args,
6767
**kwargs,
68-
circuit_breaker_exceptions=(OperationalError, TimeoutError, *circuit_breaker_exceptions),
68+
circuit_breaker_exceptions=(ConnectionException, *circuit_breaker_exceptions),
6969
)
7070

7171
if host is None:
@@ -112,14 +112,17 @@ async def recreate(self) -> None:
112112
logger.debug(f"Created {self.database!r} database connection identified by {id(self._connection)}!")
113113

114114
async def _connect(self) -> Connection:
115-
return await aiopg.connect(
116-
timeout=self._connection_timeout,
117-
host=self.host,
118-
port=self.port,
119-
dbname=self.database,
120-
user=self.user,
121-
password=self.password,
122-
)
115+
try:
116+
return await aiopg.connect(
117+
timeout=self._connection_timeout,
118+
host=self.host,
119+
port=self.port,
120+
dbname=self.database,
121+
user=self.user,
122+
password=self.password,
123+
)
124+
except (OperationalError, TimeoutError) as exc:
125+
raise ConnectionException(f"There was not possible to connect to the database: {exc!r}")
123126

124127
async def close(self) -> None:
125128
"""Close database connection.
@@ -163,27 +166,27 @@ async def _fetch_all(self) -> AsyncIterator[tuple]:
163166
except ProgrammingError as exc:
164167
raise ProgrammingException(str(exc))
165168
except OperationalError as exc:
166-
msg = f"There was an {exc!r} while trying to connect to the database."
167-
logger.warning(msg)
168-
raise ConnectionException(msg)
169+
raise ConnectionException(f"There was not possible to connect to the database: {exc!r}")
169170

170171
# noinspection PyUnusedLocal
171172
async def _execute(self, operation: AiopgDatabaseOperation) -> None:
172173
if not isinstance(operation, AiopgDatabaseOperation):
173174
raise ValueError(f"The operation must be a {AiopgDatabaseOperation!r} instance. Obtained: {operation!r}")
174175

175176
fn = partial(self._execute_cursor, operation=operation.query, parameters=operation.parameters)
176-
try:
177-
await self.with_circuit_breaker(fn)
178-
except IntegrityError as exc:
179-
raise IntegrityException(f"The requested operation raised a integrity error: {exc!r}")
177+
await self.with_circuit_breaker(fn)
180178

181179
async def _execute_cursor(self, operation: str, parameters: dict):
182180
if not await self.is_connected():
183181
await self.recreate()
184182

185183
self._cursor = await self._connection.cursor(timeout=self._cursor_timeout)
186-
await self._cursor.execute(operation=operation, parameters=parameters)
184+
try:
185+
await self._cursor.execute(operation=operation, parameters=parameters)
186+
except OperationalError as exc:
187+
raise ConnectionException(f"There was not possible to connect to the database: {exc!r}")
188+
except IntegrityError as exc:
189+
raise IntegrityException(f"The requested operation raised a integrity error: {exc!r}")
187190

188191
async def _destroy_cursor(self, **kwargs):
189192
if self._cursor is not None:

0 commit comments

Comments
 (0)