From 26433997fc319900d8a3f7d251e828e1abea2b3d Mon Sep 17 00:00:00 2001 From: "chandr-andr (Kiselev Aleksandr)" Date: Sun, 31 Mar 2024 17:20:38 +0200 Subject: [PATCH] Added one more exception and rename old ones --- python/psqlpy/_internal/exceptions.pyi | 5 ++++- python/psqlpy/exceptions.py | 7 ++++--- python/tests/test_connection.py | 4 ++-- python/tests/test_transaction.py | 10 +++++----- src/driver/cursor.rs | 4 ++-- src/exceptions/python_errors.rs | 14 ++++++++++++-- src/exceptions/rust_errors.rs | 10 +++++----- 7 files changed, 34 insertions(+), 20 deletions(-) diff --git a/python/psqlpy/_internal/exceptions.pyi b/python/psqlpy/_internal/exceptions.pyi index b05824c9..41a63bd9 100644 --- a/python/psqlpy/_internal/exceptions.pyi +++ b/python/psqlpy/_internal/exceptions.pyi @@ -23,7 +23,7 @@ class PyToRustValueMappingError(RustPSQLDriverPyBaseError): So, if there are no parameters for the query, don't handle this error. """ -class DBTransactionError(RustPSQLDriverPyBaseError): +class TransactionError(RustPSQLDriverPyBaseError): """Error if something goes wrong with `Transaction`. It has verbose error message. @@ -37,3 +37,6 @@ class UUIDValueConvertError(RustPSQLDriverPyBaseError): class CursorError(RustPSQLDriverPyBaseError): """Error if something goes wrong with the cursor.""" + +class MacAddr6ConversionError(RustPSQLDriverPyBaseError): + """Error if cannot convert MacAddr6 string value to rust type.""" diff --git a/python/psqlpy/exceptions.py b/python/psqlpy/exceptions.py index 74c6a355..61af00d5 100644 --- a/python/psqlpy/exceptions.py +++ b/python/psqlpy/exceptions.py @@ -2,10 +2,11 @@ CursorError, DBPoolConfigurationError, DBPoolError, - DBTransactionError, + MacAddr6ConversionError, PyToRustValueMappingError, RustPSQLDriverPyBaseError, RustToPyValueMappingError, + TransactionError, UUIDValueConvertError, ) @@ -14,9 +15,9 @@ "DBPoolError", "RustToPyValueMappingError", "PyToRustValueMappingError", - "DBTransactionError", + "TransactionError", "DBPoolConfigurationError", "UUIDValueConvertError", "CursorError", - "DBTransactionError", + "MacAddr6ConversionError", ] diff --git a/python/tests/test_connection.py b/python/tests/test_connection.py index e89ba025..98590631 100644 --- a/python/tests/test_connection.py +++ b/python/tests/test_connection.py @@ -6,7 +6,7 @@ from tests.helpers import count_rows_in_test_table from psqlpy import PSQLPool, QueryResult, Transaction -from psqlpy.exceptions import DBTransactionError, RustPSQLDriverPyBaseError +from psqlpy.exceptions import RustPSQLDriverPyBaseError, TransactionError pytestmark = pytest.mark.anyio @@ -57,7 +57,7 @@ async def test_connection_execute_many( f"INSERT INTO {table_name} VALUES ($1, $2)", insert_values, ) - except DBTransactionError: + except TransactionError: assert not insert_values else: assert await count_rows_in_test_table( diff --git a/python/tests/test_transaction.py b/python/tests/test_transaction.py index c099e802..0c7b030b 100644 --- a/python/tests/test_transaction.py +++ b/python/tests/test_transaction.py @@ -6,7 +6,7 @@ from tests.helpers import count_rows_in_test_table from psqlpy import Cursor, IsolationLevel, PSQLPool, ReadVariant -from psqlpy.exceptions import DBTransactionError, RustPSQLDriverPyBaseError +from psqlpy.exceptions import RustPSQLDriverPyBaseError, TransactionError pytestmark = pytest.mark.anyio @@ -55,7 +55,7 @@ async def test_transaction_begin( connection = await psql_pool.connection() transaction = connection.transaction() - with pytest.raises(expected_exception=DBTransactionError): + with pytest.raises(expected_exception=TransactionError): await transaction.execute( f"SELECT * FROM {table_name}", ) @@ -157,7 +157,7 @@ async def test_transaction_rollback( await transaction.rollback() - with pytest.raises(expected_exception=DBTransactionError): + with pytest.raises(expected_exception=TransactionError): await transaction.execute( f"SELECT * FROM {table_name} WHERE name = $1", parameters=[test_name], @@ -184,7 +184,7 @@ async def test_transaction_release_savepoint( await transaction.savepoint(sp_name_1) - with pytest.raises(expected_exception=DBTransactionError): + with pytest.raises(expected_exception=TransactionError): await transaction.savepoint(sp_name_1) await transaction.savepoint(sp_name_2) @@ -227,7 +227,7 @@ async def test_transaction_execute_many( f"INSERT INTO {table_name} VALUES ($1, $2)", insert_values, ) - except DBTransactionError: + except TransactionError: assert not insert_values else: assert await count_rows_in_test_table( diff --git a/src/driver/cursor.rs b/src/driver/cursor.rs index 7db482a6..f1df49d9 100644 --- a/src/driver/cursor.rs +++ b/src/driver/cursor.rs @@ -91,7 +91,7 @@ impl InnerCursor { let db_transaction_arc = self.db_transaction.clone(); if self.closed { - return Err(RustPSQLDriverError::DBCursorError( + return Err(RustPSQLDriverError::DataBaseCursorError( "Cursor is already closed".into(), )); } @@ -120,7 +120,7 @@ impl InnerCursor { let db_transaction_arc = self.db_transaction.clone(); if !self.is_started { - return Err(RustPSQLDriverError::DBCursorError( + return Err(RustPSQLDriverError::DataBaseCursorError( "Cursor is not opened, please call `start()`.".into(), )); } diff --git a/src/exceptions/python_errors.rs b/src/exceptions/python_errors.rs index 893ff3da..90470cca 100644 --- a/src/exceptions/python_errors.rs +++ b/src/exceptions/python_errors.rs @@ -18,7 +18,7 @@ create_exception!( ); create_exception!( psqlpy.exceptions, - DBTransactionError, + TransactionError, RustPSQLDriverPyBaseError ); create_exception!( @@ -33,6 +33,12 @@ create_exception!( RustPSQLDriverPyBaseError ); +create_exception!( + psqlpy.exceptions, + MacAddr6ConversionError, + RustPSQLDriverPyBaseError +); + create_exception!(psqlpy.exceptions, CursorError, RustPSQLDriverPyBaseError); #[allow(clippy::missing_errors_doc)] @@ -50,7 +56,7 @@ pub fn python_exceptions_module(py: Python<'_>, pymod: &PyModule) -> PyResult<() "PyToRustValueMappingError", py.get_type::(), )?; - pymod.add("DBTransactionError", py.get_type::())?; + pymod.add("TransactionError", py.get_type::())?; pymod.add( "DBPoolConfigurationError", py.get_type::(), @@ -60,5 +66,9 @@ pub fn python_exceptions_module(py: Python<'_>, pymod: &PyModule) -> PyResult<() py.get_type::(), )?; pymod.add("CursorError", py.get_type::())?; + pymod.add( + "MacAddr6ConversionError", + py.get_type::(), + )?; Ok(()) } diff --git a/src/exceptions/rust_errors.rs b/src/exceptions/rust_errors.rs index 78c1438e..ee0e6fd6 100644 --- a/src/exceptions/rust_errors.rs +++ b/src/exceptions/rust_errors.rs @@ -1,8 +1,8 @@ use thiserror::Error; use crate::exceptions::python_errors::{ - DBPoolConfigurationError, DBPoolError, DBTransactionError, PyToRustValueMappingError, - RustPSQLDriverPyBaseError, RustToPyValueMappingError, + DBPoolConfigurationError, DBPoolError, PyToRustValueMappingError, RustPSQLDriverPyBaseError, + RustToPyValueMappingError, TransactionError, }; use super::python_errors::{CursorError, UUIDValueConvertError}; @@ -22,7 +22,7 @@ pub enum RustPSQLDriverError { #[error("Configuration database pool error: {0}")] DataBasePoolConfigurationError(String), #[error("Cursor error: {0}")] - DBCursorError(String), + DataBaseCursorError(String), #[error("Python exception: {0}.")] PyError(#[from] pyo3::PyErr), @@ -57,13 +57,13 @@ impl From for pyo3::PyErr { } RustPSQLDriverError::DatabasePoolError(_) => DBPoolError::new_err((error_desc,)), RustPSQLDriverError::DataBaseTransactionError(_) => { - DBTransactionError::new_err((error_desc,)) + TransactionError::new_err((error_desc,)) } RustPSQLDriverError::DataBasePoolConfigurationError(_) => { DBPoolConfigurationError::new_err((error_desc,)) } RustPSQLDriverError::UUIDConvertError(_) => UUIDValueConvertError::new_err(error_desc), - RustPSQLDriverError::DBCursorError(_) => CursorError::new_err(error_desc), + RustPSQLDriverError::DataBaseCursorError(_) => CursorError::new_err(error_desc), } } }