Skip to content

Commit 149d6c1

Browse files
authored
Merge pull request #39 from qaspen-python/feature/rewrite_exceptions
Rewrite all exceptions to more readable ones
2 parents 12151f5 + 55a55e3 commit 149d6c1

File tree

11 files changed

+602
-181
lines changed

11 files changed

+602
-181
lines changed

python/psqlpy/_internal/exceptions.pyi

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,62 @@
11
class RustPSQLDriverPyBaseError(Exception):
22
"""Base PSQL-Rust-Engine exception."""
33

4-
class DBPoolError(RustPSQLDriverPyBaseError):
5-
"""Error if something goes wrong with Database Pool.
4+
class BaseConnectionPoolError(RustPSQLDriverPyBaseError):
5+
"""Base error for all Connection Pool errors."""
66

7-
It has verbose error message.
8-
"""
7+
class ConnectionPoolBuildError(BaseConnectionPoolError):
8+
"""Error for errors in building connection pool."""
9+
10+
class ConnectionPoolConfigurationError(BaseConnectionPoolError):
11+
"""Error in connection pool configuration."""
12+
13+
class ConnectionPoolExecuteError(BaseConnectionPoolError):
14+
"""Error in connection pool execution."""
15+
16+
class DBPoolConfigurationError(RustPSQLDriverPyBaseError):
17+
"""Error if configuration of the database pool is unacceptable."""
18+
19+
class BaseConnectionError(RustPSQLDriverPyBaseError):
20+
"""Base error for Connection errors."""
21+
22+
class ConnectionExecuteError(BaseConnectionError):
23+
"""Error in connection execution."""
24+
25+
class BaseTransactionError(RustPSQLDriverPyBaseError):
26+
"""Base error for all transaction errors."""
27+
28+
class TransactionBeginError(BaseTransactionError):
29+
"""Error in transaction begin."""
30+
31+
class TransactionCommitError(BaseTransactionError):
32+
"""Error in transaction commit."""
33+
34+
class TransactionRollbackError(BaseTransactionError):
35+
"""Error in transaction rollback."""
36+
37+
class TransactionSavepointError(BaseTransactionError):
38+
"""Error in transaction savepoint."""
39+
40+
class TransactionExecuteError(BaseTransactionError):
41+
"""Error in transaction execution."""
42+
43+
class BaseCursorError(RustPSQLDriverPyBaseError):
44+
"""Base error for Cursor errors."""
45+
46+
class CursorStartError(BaseCursorError):
47+
"""Error in cursor declare."""
48+
49+
class CursorCloseError(BaseCursorError):
50+
"""Error in cursor close."""
51+
52+
class CursorFetchError(BaseCursorError):
53+
"""Error in cursor fetch (any fetch)."""
54+
55+
class UUIDValueConvertError(RustPSQLDriverPyBaseError):
56+
"""Error if it's impossible to convert py string UUID into rust UUID."""
57+
58+
class MacAddr6ConversionError(RustPSQLDriverPyBaseError):
59+
"""Error if cannot convert MacAddr6 string value to rust type."""
960

1061
class RustToPyValueMappingError(RustPSQLDriverPyBaseError):
1162
"""Error if it is not possible to covert rust type to python.
@@ -22,21 +73,3 @@ class PyToRustValueMappingError(RustPSQLDriverPyBaseError):
2273
You can get this exception when executing queries with parameters.
2374
So, if there are no parameters for the query, don't handle this error.
2475
"""
25-
26-
class TransactionError(RustPSQLDriverPyBaseError):
27-
"""Error if something goes wrong with `Transaction`.
28-
29-
It has verbose error message.
30-
"""
31-
32-
class DBPoolConfigurationError(RustPSQLDriverPyBaseError):
33-
"""Error if configuration of the database pool is unacceptable."""
34-
35-
class UUIDValueConvertError(RustPSQLDriverPyBaseError):
36-
"""Error if it's impossible to convert py string UUID into rust UUID."""
37-
38-
class CursorError(RustPSQLDriverPyBaseError):
39-
"""Error if something goes wrong with the cursor."""
40-
41-
class MacAddr6ConversionError(RustPSQLDriverPyBaseError):
42-
"""Error if cannot convert MacAddr6 string value to rust type."""

python/psqlpy/exceptions.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
11
from ._internal.exceptions import (
2-
CursorError,
2+
BaseConnectionError,
3+
BaseConnectionPoolError,
4+
BaseCursorError,
5+
BaseTransactionError,
6+
ConnectionExecuteError,
7+
ConnectionPoolBuildError,
8+
ConnectionPoolConfigurationError,
9+
ConnectionPoolExecuteError,
10+
CursorCloseError,
11+
CursorFetchError,
12+
CursorStartError,
313
DBPoolConfigurationError,
4-
DBPoolError,
514
MacAddr6ConversionError,
615
PyToRustValueMappingError,
716
RustPSQLDriverPyBaseError,
817
RustToPyValueMappingError,
9-
TransactionError,
18+
TransactionBeginError,
19+
TransactionCommitError,
20+
TransactionExecuteError,
21+
TransactionRollbackError,
22+
TransactionSavepointError,
1023
UUIDValueConvertError,
1124
)
1225

1326
__all__ = [
27+
"BaseConnectionPoolError",
28+
"ConnectionPoolBuildError",
29+
"ConnectionPoolConfigurationError",
30+
"ConnectionPoolExecuteError",
31+
"BaseConnectionError",
32+
"ConnectionExecuteError",
33+
"BaseTransactionError",
34+
"TransactionBeginError",
35+
"TransactionCommitError",
36+
"TransactionRollbackError",
37+
"TransactionSavepointError",
38+
"TransactionExecuteError",
39+
"BaseCursorError",
40+
"CursorStartError",
41+
"CursorCloseError",
42+
"CursorFetchError",
1443
"RustPSQLDriverPyBaseError",
15-
"DBPoolError",
1644
"RustToPyValueMappingError",
1745
"PyToRustValueMappingError",
18-
"TransactionError",
1946
"DBPoolConfigurationError",
2047
"UUIDValueConvertError",
21-
"CursorError",
2248
"MacAddr6ConversionError",
2349
]

python/tests/test_connection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from tests.helpers import count_rows_in_test_table
77

88
from psqlpy import ConnectionPool, QueryResult, Transaction
9-
from psqlpy.exceptions import RustPSQLDriverPyBaseError, TransactionError
9+
from psqlpy.exceptions import ConnectionExecuteError, TransactionExecuteError
1010

1111
pytestmark = pytest.mark.anyio
1212

@@ -72,7 +72,7 @@ async def test_connection_execute_many(
7272
f"INSERT INTO {table_name} VALUES ($1, $2)",
7373
insert_values,
7474
)
75-
except TransactionError:
75+
except TransactionExecuteError:
7676
assert not insert_values
7777
else:
7878
assert await count_rows_in_test_table(
@@ -99,7 +99,7 @@ async def test_connection_fetch_row_more_than_one_row(
9999
table_name: str,
100100
) -> None:
101101
connection = await psql_pool.connection()
102-
with pytest.raises(RustPSQLDriverPyBaseError):
102+
with pytest.raises(ConnectionExecuteError):
103103
await connection.fetch_row(
104104
f"SELECT * FROM {table_name}",
105105
[],
@@ -123,7 +123,7 @@ async def test_connection_fetch_val_more_than_one_row(
123123
table_name: str,
124124
) -> None:
125125
connection = await psql_pool.connection()
126-
with pytest.raises(RustPSQLDriverPyBaseError):
126+
with pytest.raises(ConnectionExecuteError):
127127
await connection.fetch_row(
128128
f"SELECT * FROM {table_name}",
129129
[],

python/tests/test_transaction.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
from tests.helpers import count_rows_in_test_table
77

88
from psqlpy import ConnectionPool, Cursor, IsolationLevel, ReadVariant
9-
from psqlpy.exceptions import RustPSQLDriverPyBaseError, TransactionError
9+
from psqlpy.exceptions import (
10+
RustPSQLDriverPyBaseError,
11+
TransactionBeginError,
12+
TransactionExecuteError,
13+
TransactionSavepointError,
14+
)
1015

1116
pytestmark = pytest.mark.anyio
1217

@@ -55,7 +60,7 @@ async def test_transaction_begin(
5560
connection = await psql_pool.connection()
5661
transaction = connection.transaction()
5762

58-
with pytest.raises(expected_exception=TransactionError):
63+
with pytest.raises(expected_exception=TransactionBeginError):
5964
await transaction.execute(
6065
f"SELECT * FROM {table_name}",
6166
)
@@ -157,7 +162,7 @@ async def test_transaction_rollback(
157162

158163
await transaction.rollback()
159164

160-
with pytest.raises(expected_exception=TransactionError):
165+
with pytest.raises(expected_exception=TransactionBeginError):
161166
await transaction.execute(
162167
f"SELECT * FROM {table_name} WHERE name = $1",
163168
parameters=[test_name],
@@ -184,7 +189,7 @@ async def test_transaction_release_savepoint(
184189

185190
await transaction.create_savepoint(sp_name_1)
186191

187-
with pytest.raises(expected_exception=TransactionError):
192+
with pytest.raises(expected_exception=TransactionSavepointError):
188193
await transaction.create_savepoint(sp_name_1)
189194

190195
await transaction.create_savepoint(sp_name_2)
@@ -242,7 +247,7 @@ async def test_transaction_execute_many(
242247
f"INSERT INTO {table_name} VALUES ($1, $2)",
243248
insert_values,
244249
)
245-
except TransactionError:
250+
except TransactionExecuteError:
246251
assert not insert_values
247252
else:
248253
assert await count_rows_in_test_table(

0 commit comments

Comments
 (0)