Skip to content

Rewrite all exceptions to more readable ones #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions python/psqlpy/_internal/exceptions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ class ConnectionPoolConfigurationError(BaseConnectionPoolError):
class ConnectionPoolExecuteError(BaseConnectionPoolError):
"""Error in connection pool execution."""

class DBPoolConfigurationError(RustPSQLDriverPyBaseError):
"""Error if configuration of the database pool is unacceptable."""

class BaseConnectionError(RustPSQLDriverPyBaseError):
"""Base error for Connection errors."""

Expand Down Expand Up @@ -55,8 +52,8 @@ class CursorFetchError(BaseCursorError):
class UUIDValueConvertError(RustPSQLDriverPyBaseError):
"""Error if it's impossible to convert py string UUID into rust UUID."""

class MacAddr6ConversionError(RustPSQLDriverPyBaseError):
"""Error if cannot convert MacAddr6 string value to rust type."""
class MacAddrConversionError(RustPSQLDriverPyBaseError):
"""Error if cannot convert MacAddr string value to rust type."""

class RustToPyValueMappingError(RustPSQLDriverPyBaseError):
"""Error if it is not possible to covert rust type to python.
Expand Down
6 changes: 2 additions & 4 deletions python/psqlpy/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
CursorCloseError,
CursorFetchError,
CursorStartError,
DBPoolConfigurationError,
MacAddr6ConversionError,
MacAddrConversionError,
PyToRustValueMappingError,
RustPSQLDriverPyBaseError,
RustToPyValueMappingError,
Expand Down Expand Up @@ -43,7 +42,6 @@
"RustPSQLDriverPyBaseError",
"RustToPyValueMappingError",
"PyToRustValueMappingError",
"DBPoolConfigurationError",
"UUIDValueConvertError",
"MacAddr6ConversionError",
"MacAddrConversionError",
]
13 changes: 8 additions & 5 deletions python/tests/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
TargetSessionAttrs,
connect,
)
from psqlpy.exceptions import DBPoolConfigurationError, RustPSQLDriverPyBaseError
from psqlpy.exceptions import (
ConnectionPoolConfigurationError,
RustPSQLDriverPyBaseError,
)

pytestmark = pytest.mark.anyio

Expand Down Expand Up @@ -94,22 +97,22 @@ async def test_pool_conn_recycling_method(


async def test_build_pool_failure() -> None:
with pytest.raises(expected_exception=DBPoolConfigurationError):
with pytest.raises(expected_exception=ConnectionPoolConfigurationError):
ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
connect_timeout_nanosec=12,
)
with pytest.raises(expected_exception=DBPoolConfigurationError):
with pytest.raises(expected_exception=ConnectionPoolConfigurationError):
ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
connect_timeout_nanosec=12,
)
with pytest.raises(expected_exception=DBPoolConfigurationError):
with pytest.raises(expected_exception=ConnectionPoolConfigurationError):
ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
keepalives_idle_nanosec=12,
)
with pytest.raises(expected_exception=DBPoolConfigurationError):
with pytest.raises(expected_exception=ConnectionPoolConfigurationError):
ConnectionPool(
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
keepalives_interval_nanosec=12,
Expand Down
21 changes: 1 addition & 20 deletions src/exceptions/python_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ create_exception!(
PyToRustValueMappingError,
RustPSQLDriverPyBaseError
);
create_exception!(
psqlpy.exceptions,
DBPoolConfigurationError,
RustPSQLDriverPyBaseError
);

create_exception!(
psqlpy.exceptions,
Expand All @@ -123,12 +118,6 @@ create_exception!(
RustPSQLDriverPyBaseError
);

create_exception!(
psqlpy.exceptions,
RustRuntimeJoinError,
RustPSQLDriverPyBaseError
);

#[allow(clippy::missing_errors_doc)]
pub fn python_exceptions_module(py: Python<'_>, pymod: &Bound<'_, PyModule>) -> PyResult<()> {
pymod.add(
Expand Down Expand Up @@ -200,20 +189,12 @@ pub fn python_exceptions_module(py: Python<'_>, pymod: &Bound<'_, PyModule>) ->
"PyToRustValueMappingError",
py.get_type_bound::<PyToRustValueMappingError>(),
)?;
pymod.add(
"DBPoolConfigurationError",
py.get_type_bound::<DBPoolConfigurationError>(),
)?;
pymod.add(
"UUIDValueConvertError",
py.get_type_bound::<UUIDValueConvertError>(),
)?;
pymod.add(
"MacAddr6ConversionError",
py.get_type_bound::<MacAddrConversionError>(),
)?;
pymod.add(
"RustRuntimeJoinError",
"MacAddrConversionError",
py.get_type_bound::<MacAddrConversionError>(),
)?;
Ok(())
Expand Down
15 changes: 7 additions & 8 deletions src/exceptions/rust_errors.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use thiserror::Error;
use tokio::task::JoinError;

use crate::exceptions::python_errors::{
DBPoolConfigurationError, PyToRustValueMappingError, RustToPyValueMappingError,
};
use crate::exceptions::python_errors::{PyToRustValueMappingError, RustToPyValueMappingError};

use super::python_errors::{
BaseConnectionError, BaseConnectionPoolError, BaseCursorError, BaseTransactionError,
ConnectionExecuteError, ConnectionPoolBuildError, ConnectionPoolExecuteError, CursorCloseError,
CursorFetchError, CursorStartError, DriverError, MacAddrParseError, RuntimeJoinError,
TransactionBeginError, TransactionCommitError, TransactionExecuteError,
TransactionRollbackError, TransactionSavepointError, UUIDValueConvertError,
ConnectionExecuteError, ConnectionPoolBuildError, ConnectionPoolConfigurationError,
ConnectionPoolExecuteError, CursorCloseError, CursorFetchError, CursorStartError, DriverError,
MacAddrParseError, RuntimeJoinError, TransactionBeginError, TransactionCommitError,
TransactionExecuteError, TransactionRollbackError, TransactionSavepointError,
UUIDValueConvertError,
};

pub type RustPSQLDriverPyResult<T> = Result<T, RustPSQLDriverError>;
Expand Down Expand Up @@ -97,7 +96,7 @@ impl From<RustPSQLDriverError> for pyo3::PyErr {
PyToRustValueMappingError::new_err((error_desc,))
}
RustPSQLDriverError::ConnectionPoolConfigurationError(_) => {
DBPoolConfigurationError::new_err((error_desc,))
ConnectionPoolConfigurationError::new_err((error_desc,))
}
RustPSQLDriverError::RustUUIDConvertError(_) => {
UUIDValueConvertError::new_err(error_desc)
Expand Down