Skip to content

Commit 55a55e3

Browse files
committed
Rewrite all exceptions to more readable ones
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent ac92f84 commit 55a55e3

File tree

5 files changed

+166
-41
lines changed

5 files changed

+166
-41
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(

src/exceptions/python_errors.rs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ create_exception!(
8585
);
8686

8787
// Cursor exceptions
88-
create_exception!(psqlpy.exceptions, BaseCursorError, BaseTransactionError);
88+
create_exception!(
89+
psqlpy.exceptions,
90+
BaseCursorError,
91+
RustPSQLDriverPyBaseError
92+
);
8993
create_exception!(psqlpy.exceptions, CursorStartError, BaseCursorError);
9094
create_exception!(psqlpy.exceptions, CursorCloseError, BaseCursorError);
9195
create_exception!(psqlpy.exceptions, CursorFetchError, BaseCursorError);
@@ -115,7 +119,7 @@ create_exception!(
115119

116120
create_exception!(
117121
psqlpy.exceptions,
118-
MacAddr6ConversionError,
122+
MacAddrConversionError,
119123
RustPSQLDriverPyBaseError
120124
);
121125

@@ -131,6 +135,63 @@ pub fn python_exceptions_module(py: Python<'_>, pymod: &Bound<'_, PyModule>) ->
131135
"RustPSQLDriverPyBaseError",
132136
py.get_type_bound::<RustPSQLDriverPyBaseError>(),
133137
)?;
138+
139+
pymod.add(
140+
"BaseConnectionPoolError",
141+
py.get_type_bound::<BaseConnectionPoolError>(),
142+
)?;
143+
pymod.add(
144+
"ConnectionPoolBuildError",
145+
py.get_type_bound::<ConnectionPoolBuildError>(),
146+
)?;
147+
pymod.add(
148+
"ConnectionPoolConfigurationError",
149+
py.get_type_bound::<ConnectionPoolConfigurationError>(),
150+
)?;
151+
pymod.add(
152+
"ConnectionPoolExecuteError",
153+
py.get_type_bound::<ConnectionPoolExecuteError>(),
154+
)?;
155+
156+
pymod.add(
157+
"BaseConnectionError",
158+
py.get_type_bound::<BaseConnectionError>(),
159+
)?;
160+
pymod.add(
161+
"ConnectionExecuteError",
162+
py.get_type_bound::<ConnectionExecuteError>(),
163+
)?;
164+
165+
pymod.add(
166+
"BaseTransactionError",
167+
py.get_type_bound::<BaseTransactionError>(),
168+
)?;
169+
pymod.add(
170+
"TransactionBeginError",
171+
py.get_type_bound::<TransactionBeginError>(),
172+
)?;
173+
pymod.add(
174+
"TransactionCommitError",
175+
py.get_type_bound::<TransactionCommitError>(),
176+
)?;
177+
pymod.add(
178+
"TransactionRollbackError",
179+
py.get_type_bound::<TransactionRollbackError>(),
180+
)?;
181+
pymod.add(
182+
"TransactionSavepointError",
183+
py.get_type_bound::<TransactionSavepointError>(),
184+
)?;
185+
pymod.add(
186+
"TransactionExecuteError",
187+
py.get_type_bound::<TransactionExecuteError>(),
188+
)?;
189+
190+
pymod.add("BaseCursorError", py.get_type_bound::<BaseCursorError>())?;
191+
pymod.add("CursorStartError", py.get_type_bound::<CursorStartError>())?;
192+
pymod.add("CursorCloseError", py.get_type_bound::<CursorCloseError>())?;
193+
pymod.add("CursorFetchError", py.get_type_bound::<CursorFetchError>())?;
194+
134195
pymod.add(
135196
"RustToPyValueMappingError",
136197
py.get_type_bound::<RustToPyValueMappingError>(),
@@ -149,11 +210,11 @@ pub fn python_exceptions_module(py: Python<'_>, pymod: &Bound<'_, PyModule>) ->
149210
)?;
150211
pymod.add(
151212
"MacAddr6ConversionError",
152-
py.get_type_bound::<MacAddr6ConversionError>(),
213+
py.get_type_bound::<MacAddrConversionError>(),
153214
)?;
154215
pymod.add(
155216
"RustRuntimeJoinError",
156-
py.get_type_bound::<MacAddr6ConversionError>(),
217+
py.get_type_bound::<MacAddrConversionError>(),
157218
)?;
158219
Ok(())
159220
}

0 commit comments

Comments
 (0)