Skip to content

Commit e746291

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

File tree

4 files changed

+185
-23
lines changed

4 files changed

+185
-23
lines changed

src/driver/cursor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl Cursor {
175175

176176
db_transaction.cursor_close(&closed, &cursor_name).await?;
177177
if !is_exception_none {
178-
return Err(RustPSQLDriverError::PyError(py_err));
178+
return Err(RustPSQLDriverError::RustPyError(py_err));
179179
}
180180
Ok(())
181181
}

src/driver/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl Transaction {
153153
Ok(())
154154
} else {
155155
db_client.rollback().await?;
156-
Err(RustPSQLDriverError::PyError(py_err))
156+
Err(RustPSQLDriverError::RustPyError(py_err))
157157
};
158158

159159
pyo3::Python::with_gil(|gil| {

src/exceptions/python_errors.rs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,89 @@ use pyo3::{
33
types::{PyModule, PyModuleMethods},
44
Bound, PyResult, Python,
55
};
6-
6+
// Main exception.
77
create_exception!(
88
psqlpy.exceptions,
99
RustPSQLDriverPyBaseError,
1010
pyo3::exceptions::PyException
1111
);
12+
13+
// Rust exceptions
14+
// `Rust` means thats these exceptions come from external rust crates,
15+
// not from the code of the library.
16+
create_exception!(psqlpy.exceptions, RustException, RustPSQLDriverPyBaseError);
17+
create_exception!(psqlpy.exceptions, DriverError, RustException);
18+
create_exception!(psqlpy.exceptions, MacAddrParseError, RustException);
19+
create_exception!(psqlpy.exceptions, RuntimeJoinError, RustException);
20+
21+
// ConnectionPool exceptions
22+
create_exception!(
23+
psqlpy.exceptions,
24+
BaseConnectionPoolError,
25+
RustPSQLDriverPyBaseError
26+
);
27+
create_exception!(
28+
psqlpy.exceptions,
29+
ConnectionPoolBuildError,
30+
BaseConnectionPoolError
31+
);
32+
create_exception!(
33+
psqlpy.exceptions,
34+
ConnectionPoolExecuteError,
35+
BaseConnectionPoolError
36+
);
37+
38+
// Connection exceptions
39+
create_exception!(
40+
psqlpy.exceptions,
41+
BaseConnectionError,
42+
RustPSQLDriverPyBaseError
43+
);
44+
create_exception!(
45+
psqlpy.exceptions,
46+
ConnectionExecuteError,
47+
BaseConnectionError
48+
);
49+
50+
// Transaction exceptions
51+
create_exception!(
52+
psqlpy.exceptions,
53+
BaseTransactionError,
54+
RustPSQLDriverPyBaseError
55+
);
56+
create_exception!(
57+
psqlpy.exceptions,
58+
TransactionBeginError,
59+
BaseTransactionError
60+
);
61+
create_exception!(
62+
psqlpy.exceptions,
63+
TransactionCommitError,
64+
BaseTransactionError
65+
);
66+
create_exception!(
67+
psqlpy.exceptions,
68+
TransactionRollbackError,
69+
BaseTransactionError
70+
);
71+
create_exception!(
72+
psqlpy.exceptions,
73+
TransactionSavepointError,
74+
BaseTransactionError
75+
);
76+
create_exception!(
77+
psqlpy.exceptions,
78+
TransactionExecuteError,
79+
BaseTransactionError
80+
);
81+
82+
// Cursor exceptions
83+
create_exception!(psqlpy.exceptions, BaseCursorError, BaseTransactionError);
84+
create_exception!(psqlpy.exceptions, CursorStartError, BaseCursorError);
85+
create_exception!(psqlpy.exceptions, CursorCloseError, BaseCursorError);
86+
create_exception!(psqlpy.exceptions, CursorFetchError, BaseCursorError);
87+
88+
// Inner exceptions
1289
create_exception!(psqlpy.exceptions, DBPoolError, RustPSQLDriverPyBaseError);
1390
create_exception!(
1491
psqlpy.exceptions,

src/exceptions/rust_errors.rs

Lines changed: 105 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,62 @@ use thiserror::Error;
22
use tokio::task::JoinError;
33

44
use crate::exceptions::python_errors::{
5-
DBPoolConfigurationError, DBPoolError, PyToRustValueMappingError, RustPSQLDriverPyBaseError,
6-
RustToPyValueMappingError, TransactionError,
5+
DBPoolConfigurationError, DBPoolError, PyToRustValueMappingError, RustToPyValueMappingError,
6+
TransactionError,
77
};
88

9-
use super::python_errors::{CursorError, UUIDValueConvertError};
9+
use super::python_errors::{
10+
BaseConnectionError, BaseConnectionPoolError, BaseCursorError, BaseTransactionError,
11+
ConnectionExecuteError, ConnectionPoolBuildError, ConnectionPoolExecuteError, CursorCloseError,
12+
CursorError, CursorFetchError, CursorStartError, DriverError, MacAddrParseError,
13+
RuntimeJoinError, TransactionBeginError, TransactionCommitError, TransactionExecuteError,
14+
TransactionRollbackError, TransactionSavepointError, UUIDValueConvertError,
15+
};
1016

1117
pub type RustPSQLDriverPyResult<T> = Result<T, RustPSQLDriverError>;
1218

1319
#[derive(Error, Debug)]
1420
pub enum RustPSQLDriverError {
21+
// ConnectionPool errors
22+
#[error("Connection pool error: {0}.")]
23+
BaseConnectionPoolError(String),
24+
#[error("Connection pool build error: {0}.")]
25+
ConnectionPoolBuildError(String),
26+
#[error("Connection pool execute error: {0}.")]
27+
ConnectionPoolExecuteError(String),
28+
29+
// Connection Errors
30+
#[error("Connection error: {0}.")]
31+
BaseConnectionError(String),
32+
#[error("Connection execute error: {0}.")]
33+
ConnectionExecuteError(String),
34+
35+
// Transaction Errors
36+
#[error("Transaction error: {0}")]
37+
BaseTransactionError(String),
38+
#[error("Transaction begin error: {0}")]
39+
TransactionBeginError(String),
40+
#[error("Transaction commit error: {0}")]
41+
TransactionCommitError(String),
42+
#[error("Transaction rollback error: {0}")]
43+
TransactionRollbackError(String),
44+
#[error("Transaction savepoint error: {0}")]
45+
TransactionSavepointError(String),
46+
#[error("Transaction execute error: {0}")]
47+
TransactionExecuteError(String),
48+
49+
// Cursor Errors
50+
#[error("Cursor error: {0}")]
51+
BaseCursorError(String),
52+
#[error("Cursor start error: {0}")]
53+
CursorStartError(String),
54+
#[error("Cursor close error: {0}")]
55+
CursorCloseError(String),
56+
#[error("Cursor fetch error: {0}")]
57+
CursorFetchError(String),
58+
1559
#[error("Database pool error: {0}.")]
16-
DatabasePoolError(String),
60+
ConnectionPoolError(String),
1761
#[error("Can't convert value from driver to python type: {0}")]
1862
RustToPyValueConversionError(String),
1963
#[error("Can't convert value from python to rust type: {0}")]
@@ -26,48 +70,89 @@ pub enum RustPSQLDriverError {
2670
DataBaseCursorError(String),
2771

2872
#[error("Python exception: {0}.")]
29-
PyError(#[from] pyo3::PyErr),
73+
RustPyError(#[from] pyo3::PyErr),
3074
#[error("Database engine exception: {0}.")]
31-
DBEngineError(#[from] deadpool_postgres::tokio_postgres::Error),
75+
RustDriverError(#[from] deadpool_postgres::tokio_postgres::Error),
3276
#[error("Database engine pool exception: {0}")]
33-
DBEnginePoolError(#[from] deadpool_postgres::PoolError),
77+
RustConnectionPoolError(#[from] deadpool_postgres::PoolError),
3478
#[error("Database engine build failed: {0}")]
35-
DBEngineBuildError(#[from] deadpool_postgres::BuildError),
79+
RustDriverBuildError(#[from] deadpool_postgres::BuildError),
3680
#[error("Value convert has failed: {0}")]
37-
UUIDConvertError(#[from] uuid::Error),
81+
RustUUIDConvertError(#[from] uuid::Error),
3882
#[error("Cannot convert provided string to MacAddr6")]
39-
MacAddr6ConversionError(#[from] macaddr::ParseError),
83+
RustMacAddrConversionError(#[from] macaddr::ParseError),
4084
#[error("Cannot execute future in Rust: {0}")]
41-
RuntimeJoinError(#[from] JoinError),
85+
RustRuntimeJoinError(#[from] JoinError),
4286
}
4387

4488
impl From<RustPSQLDriverError> for pyo3::PyErr {
4589
fn from(error: RustPSQLDriverError) -> Self {
4690
let error_desc = error.to_string();
4791
match error {
48-
RustPSQLDriverError::PyError(err) => err,
49-
RustPSQLDriverError::DBEngineError(_)
50-
| RustPSQLDriverError::DBEnginePoolError(_)
51-
| RustPSQLDriverError::MacAddr6ConversionError(_)
52-
| RustPSQLDriverError::DBEngineBuildError(_)
53-
| RustPSQLDriverError::RuntimeJoinError(_) => {
54-
RustPSQLDriverPyBaseError::new_err((error_desc,))
92+
RustPSQLDriverError::RustPyError(err) => err,
93+
RustPSQLDriverError::RustDriverError(_) => DriverError::new_err((error_desc,)),
94+
RustPSQLDriverError::RustMacAddrConversionError(_) => {
95+
MacAddrParseError::new_err((error_desc,))
96+
}
97+
RustPSQLDriverError::RustRuntimeJoinError(_) => {
98+
RuntimeJoinError::new_err((error_desc,))
5599
}
56100
RustPSQLDriverError::RustToPyValueConversionError(_) => {
57101
RustToPyValueMappingError::new_err((error_desc,))
58102
}
59103
RustPSQLDriverError::PyToRustValueConversionError(_) => {
60104
PyToRustValueMappingError::new_err((error_desc,))
61105
}
62-
RustPSQLDriverError::DatabasePoolError(_) => DBPoolError::new_err((error_desc,)),
106+
RustPSQLDriverError::ConnectionPoolError(_) => DBPoolError::new_err((error_desc,)),
63107
RustPSQLDriverError::DataBaseTransactionError(_) => {
64108
TransactionError::new_err((error_desc,))
65109
}
66110
RustPSQLDriverError::DataBasePoolConfigurationError(_) => {
67111
DBPoolConfigurationError::new_err((error_desc,))
68112
}
69-
RustPSQLDriverError::UUIDConvertError(_) => UUIDValueConvertError::new_err(error_desc),
113+
RustPSQLDriverError::RustUUIDConvertError(_) => {
114+
UUIDValueConvertError::new_err(error_desc)
115+
}
70116
RustPSQLDriverError::DataBaseCursorError(_) => CursorError::new_err(error_desc),
117+
RustPSQLDriverError::BaseConnectionPoolError(_)
118+
| RustPSQLDriverError::RustConnectionPoolError(_) => {
119+
BaseConnectionPoolError::new_err((error_desc,))
120+
}
121+
RustPSQLDriverError::ConnectionPoolBuildError(_)
122+
| RustPSQLDriverError::RustDriverBuildError(_) => {
123+
ConnectionPoolBuildError::new_err((error_desc,))
124+
}
125+
RustPSQLDriverError::ConnectionPoolExecuteError(_) => {
126+
ConnectionPoolExecuteError::new_err((error_desc,))
127+
}
128+
RustPSQLDriverError::BaseConnectionError(_) => {
129+
BaseConnectionError::new_err((error_desc,))
130+
}
131+
RustPSQLDriverError::ConnectionExecuteError(_) => {
132+
ConnectionExecuteError::new_err((error_desc,))
133+
}
134+
RustPSQLDriverError::BaseTransactionError(_) => {
135+
BaseTransactionError::new_err((error_desc,))
136+
}
137+
RustPSQLDriverError::TransactionBeginError(_) => {
138+
TransactionBeginError::new_err((error_desc,))
139+
}
140+
RustPSQLDriverError::TransactionCommitError(_) => {
141+
TransactionCommitError::new_err((error_desc,))
142+
}
143+
RustPSQLDriverError::TransactionRollbackError(_) => {
144+
TransactionRollbackError::new_err((error_desc,))
145+
}
146+
RustPSQLDriverError::TransactionSavepointError(_) => {
147+
TransactionSavepointError::new_err((error_desc,))
148+
}
149+
RustPSQLDriverError::TransactionExecuteError(_) => {
150+
TransactionExecuteError::new_err((error_desc,))
151+
}
152+
RustPSQLDriverError::BaseCursorError(_) => BaseCursorError::new_err((error_desc,)),
153+
RustPSQLDriverError::CursorStartError(_) => CursorStartError::new_err((error_desc,)),
154+
RustPSQLDriverError::CursorCloseError(_) => CursorCloseError::new_err((error_desc,)),
155+
RustPSQLDriverError::CursorFetchError(_) => CursorFetchError::new_err((error_desc,)),
71156
}
72157
}
73158
}

0 commit comments

Comments
 (0)