Skip to content

Commit 2643399

Browse files
committed
Added one more exception and rename old ones
1 parent fda347e commit 2643399

File tree

7 files changed

+34
-20
lines changed

7 files changed

+34
-20
lines changed

python/psqlpy/_internal/exceptions.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class PyToRustValueMappingError(RustPSQLDriverPyBaseError):
2323
So, if there are no parameters for the query, don't handle this error.
2424
"""
2525

26-
class DBTransactionError(RustPSQLDriverPyBaseError):
26+
class TransactionError(RustPSQLDriverPyBaseError):
2727
"""Error if something goes wrong with `Transaction`.
2828
2929
It has verbose error message.
@@ -37,3 +37,6 @@ class UUIDValueConvertError(RustPSQLDriverPyBaseError):
3737

3838
class CursorError(RustPSQLDriverPyBaseError):
3939
"""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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
CursorError,
33
DBPoolConfigurationError,
44
DBPoolError,
5-
DBTransactionError,
5+
MacAddr6ConversionError,
66
PyToRustValueMappingError,
77
RustPSQLDriverPyBaseError,
88
RustToPyValueMappingError,
9+
TransactionError,
910
UUIDValueConvertError,
1011
)
1112

@@ -14,9 +15,9 @@
1415
"DBPoolError",
1516
"RustToPyValueMappingError",
1617
"PyToRustValueMappingError",
17-
"DBTransactionError",
18+
"TransactionError",
1819
"DBPoolConfigurationError",
1920
"UUIDValueConvertError",
2021
"CursorError",
21-
"DBTransactionError",
22+
"MacAddr6ConversionError",
2223
]

python/tests/test_connection.py

Lines changed: 2 additions & 2 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 PSQLPool, QueryResult, Transaction
9-
from psqlpy.exceptions import DBTransactionError, RustPSQLDriverPyBaseError
9+
from psqlpy.exceptions import RustPSQLDriverPyBaseError, TransactionError
1010

1111
pytestmark = pytest.mark.anyio
1212

@@ -57,7 +57,7 @@ async def test_connection_execute_many(
5757
f"INSERT INTO {table_name} VALUES ($1, $2)",
5858
insert_values,
5959
)
60-
except DBTransactionError:
60+
except TransactionError:
6161
assert not insert_values
6262
else:
6363
assert await count_rows_in_test_table(

python/tests/test_transaction.py

Lines changed: 5 additions & 5 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 Cursor, IsolationLevel, PSQLPool, ReadVariant
9-
from psqlpy.exceptions import DBTransactionError, RustPSQLDriverPyBaseError
9+
from psqlpy.exceptions import RustPSQLDriverPyBaseError, TransactionError
1010

1111
pytestmark = pytest.mark.anyio
1212

@@ -55,7 +55,7 @@ async def test_transaction_begin(
5555
connection = await psql_pool.connection()
5656
transaction = connection.transaction()
5757

58-
with pytest.raises(expected_exception=DBTransactionError):
58+
with pytest.raises(expected_exception=TransactionError):
5959
await transaction.execute(
6060
f"SELECT * FROM {table_name}",
6161
)
@@ -157,7 +157,7 @@ async def test_transaction_rollback(
157157

158158
await transaction.rollback()
159159

160-
with pytest.raises(expected_exception=DBTransactionError):
160+
with pytest.raises(expected_exception=TransactionError):
161161
await transaction.execute(
162162
f"SELECT * FROM {table_name} WHERE name = $1",
163163
parameters=[test_name],
@@ -184,7 +184,7 @@ async def test_transaction_release_savepoint(
184184

185185
await transaction.savepoint(sp_name_1)
186186

187-
with pytest.raises(expected_exception=DBTransactionError):
187+
with pytest.raises(expected_exception=TransactionError):
188188
await transaction.savepoint(sp_name_1)
189189

190190
await transaction.savepoint(sp_name_2)
@@ -227,7 +227,7 @@ async def test_transaction_execute_many(
227227
f"INSERT INTO {table_name} VALUES ($1, $2)",
228228
insert_values,
229229
)
230-
except DBTransactionError:
230+
except TransactionError:
231231
assert not insert_values
232232
else:
233233
assert await count_rows_in_test_table(

src/driver/cursor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl InnerCursor {
9191
let db_transaction_arc = self.db_transaction.clone();
9292

9393
if self.closed {
94-
return Err(RustPSQLDriverError::DBCursorError(
94+
return Err(RustPSQLDriverError::DataBaseCursorError(
9595
"Cursor is already closed".into(),
9696
));
9797
}
@@ -120,7 +120,7 @@ impl InnerCursor {
120120
let db_transaction_arc = self.db_transaction.clone();
121121

122122
if !self.is_started {
123-
return Err(RustPSQLDriverError::DBCursorError(
123+
return Err(RustPSQLDriverError::DataBaseCursorError(
124124
"Cursor is not opened, please call `start()`.".into(),
125125
));
126126
}

src/exceptions/python_errors.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ create_exception!(
1818
);
1919
create_exception!(
2020
psqlpy.exceptions,
21-
DBTransactionError,
21+
TransactionError,
2222
RustPSQLDriverPyBaseError
2323
);
2424
create_exception!(
@@ -33,6 +33,12 @@ create_exception!(
3333
RustPSQLDriverPyBaseError
3434
);
3535

36+
create_exception!(
37+
psqlpy.exceptions,
38+
MacAddr6ConversionError,
39+
RustPSQLDriverPyBaseError
40+
);
41+
3642
create_exception!(psqlpy.exceptions, CursorError, RustPSQLDriverPyBaseError);
3743

3844
#[allow(clippy::missing_errors_doc)]
@@ -50,7 +56,7 @@ pub fn python_exceptions_module(py: Python<'_>, pymod: &PyModule) -> PyResult<()
5056
"PyToRustValueMappingError",
5157
py.get_type::<PyToRustValueMappingError>(),
5258
)?;
53-
pymod.add("DBTransactionError", py.get_type::<DBTransactionError>())?;
59+
pymod.add("TransactionError", py.get_type::<TransactionError>())?;
5460
pymod.add(
5561
"DBPoolConfigurationError",
5662
py.get_type::<DBPoolConfigurationError>(),
@@ -60,5 +66,9 @@ pub fn python_exceptions_module(py: Python<'_>, pymod: &PyModule) -> PyResult<()
6066
py.get_type::<UUIDValueConvertError>(),
6167
)?;
6268
pymod.add("CursorError", py.get_type::<CursorError>())?;
69+
pymod.add(
70+
"MacAddr6ConversionError",
71+
py.get_type::<MacAddr6ConversionError>(),
72+
)?;
6373
Ok(())
6474
}

src/exceptions/rust_errors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use thiserror::Error;
22

33
use crate::exceptions::python_errors::{
4-
DBPoolConfigurationError, DBPoolError, DBTransactionError, PyToRustValueMappingError,
5-
RustPSQLDriverPyBaseError, RustToPyValueMappingError,
4+
DBPoolConfigurationError, DBPoolError, PyToRustValueMappingError, RustPSQLDriverPyBaseError,
5+
RustToPyValueMappingError, TransactionError,
66
};
77

88
use super::python_errors::{CursorError, UUIDValueConvertError};
@@ -22,7 +22,7 @@ pub enum RustPSQLDriverError {
2222
#[error("Configuration database pool error: {0}")]
2323
DataBasePoolConfigurationError(String),
2424
#[error("Cursor error: {0}")]
25-
DBCursorError(String),
25+
DataBaseCursorError(String),
2626

2727
#[error("Python exception: {0}.")]
2828
PyError(#[from] pyo3::PyErr),
@@ -57,13 +57,13 @@ impl From<RustPSQLDriverError> for pyo3::PyErr {
5757
}
5858
RustPSQLDriverError::DatabasePoolError(_) => DBPoolError::new_err((error_desc,)),
5959
RustPSQLDriverError::DataBaseTransactionError(_) => {
60-
DBTransactionError::new_err((error_desc,))
60+
TransactionError::new_err((error_desc,))
6161
}
6262
RustPSQLDriverError::DataBasePoolConfigurationError(_) => {
6363
DBPoolConfigurationError::new_err((error_desc,))
6464
}
6565
RustPSQLDriverError::UUIDConvertError(_) => UUIDValueConvertError::new_err(error_desc),
66-
RustPSQLDriverError::DBCursorError(_) => CursorError::new_err(error_desc),
66+
RustPSQLDriverError::DataBaseCursorError(_) => CursorError::new_err(error_desc),
6767
}
6868
}
6969
}

0 commit comments

Comments
 (0)