-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.rs
More file actions
239 lines (208 loc) · 8.05 KB
/
errors.rs
File metadata and controls
239 lines (208 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/// NOTE: We define the potentially raisable errors/exceptions in Python
/// so we have nice IDE support for docs-on-hover and for 'go to definition'.
use std::error::Error;
use opsqueue::common::chunk::ChunkId;
use opsqueue::common::errors::{
ChunkNotFound, IncorrectUsage, SubmissionNotCancellable, SubmissionNotFound,
UnexpectedOpsqueueConsumerServerResponse, E,
};
use pyo3::exceptions::PyBaseException;
use pyo3::{import_exception, Bound, PyErr, Python};
use crate::common;
use crate::common::{ChunkIndex, SubmissionId};
// Expected errors:
import_exception!(opsqueue.exceptions, SubmissionFailedError);
// Incorrect usage errors:
import_exception!(opsqueue.exceptions, IncorrectUsageError);
import_exception!(opsqueue.exceptions, TryFromIntError);
import_exception!(opsqueue.exceptions, ChunkNotFoundError);
import_exception!(opsqueue.exceptions, SubmissionNotFoundError);
import_exception!(opsqueue.exceptions, SubmissionNotCancellableError);
import_exception!(opsqueue.exceptions, NewObjectStoreClientError);
import_exception!(opsqueue.exceptions, SubmissionNotCompletedYetError);
// Internal errors:
import_exception!(opsqueue.exceptions, OpsqueueInternalError);
import_exception!(
opsqueue.exceptions,
UnexpectedOpsqueueConsumerServerResponseError
);
import_exception!(opsqueue.exceptions, ChunkRetrievalError);
import_exception!(opsqueue.exceptions, ChunksStorageError);
import_exception!(opsqueue.exceptions, ChunkStorageError);
import_exception!(opsqueue.exceptions, InternalConsumerClientError);
import_exception!(opsqueue.exceptions, InternalProducerClientError);
/// A newtype so we can write From/Into implementations turning various error types
/// into PyErr, including those defined in other crates.
///
/// This follows the 'newtype wrapper' approach from
/// <https://pyo3.rs/v0.22.5/function/error-handling#foreign-rust-error-types>
///
/// The 'C' stands for 'Convertible'.
pub struct CError<T>(pub T);
impl<T> From<T> for CError<T> {
fn from(value: T) -> Self {
CError(value)
}
}
/// Result type alias to help with the automatic conversion of error types
/// into PyErr.
///
/// This follows the 'newtype wrapper' approach from
/// <https://pyo3.rs/v0.22.5/function/error-handling#foreign-rust-error-types>
///
/// The 'C' stands for 'Convertible'.
pub type CPyResult<T, E> = Result<T, CError<E>>;
/// Indicates a 'fatal' PyErr: Any Python exception which is _not_ a subclass of `PyException`.
///
/// These are known as 'fatal' exceptions in Python.
/// c.f. <https://docs.python.org/3/tutorial/errors.html#tut-userexceptions>
///
/// We don't consume/wrap these errors but propagate them,
/// allowing things like KeyboardInterrupt, SystemExit or MemoryError,
/// to trigger cleanup-and-exit.
#[derive(thiserror::Error, Debug)]
#[error("Fatal Python exception: {0}")]
pub struct FatalPythonException(#[from] pub PyErr);
impl From<CError<FatalPythonException>> for PyErr {
fn from(value: CError<FatalPythonException>) -> Self {
value.0 .0
}
}
impl From<FatalPythonException> for PyErr {
fn from(value: FatalPythonException) -> Self {
value.0
}
}
impl From<CError<opsqueue::common::errors::TryFromIntError>> for PyErr {
fn from(value: CError<opsqueue::common::errors::TryFromIntError>) -> Self {
TryFromIntError::new_err(value.0.to_string())
}
}
impl From<CError<opsqueue::consumer::client::InternalConsumerClientError>> for PyErr {
fn from(value: CError<opsqueue::consumer::client::InternalConsumerClientError>) -> Self {
InternalConsumerClientError::new_err(value.0.to_string())
}
}
impl From<CError<opsqueue::producer::client::InternalProducerClientError>> for PyErr {
fn from(value: CError<opsqueue::producer::client::InternalProducerClientError>) -> Self {
InternalProducerClientError::new_err(value.0.to_string())
}
}
impl From<CError<opsqueue::object_store::ChunkRetrievalError>> for PyErr {
fn from(value: CError<opsqueue::object_store::ChunkRetrievalError>) -> Self {
ChunkRetrievalError::new_err(value.0.to_string())
}
}
impl From<CError<opsqueue::object_store::ChunksStorageError>> for PyErr {
fn from(value: CError<opsqueue::object_store::ChunksStorageError>) -> Self {
ChunksStorageError::new_err(value.0.to_string())
}
}
impl From<CError<opsqueue::object_store::ChunkStorageError>> for PyErr {
fn from(value: CError<opsqueue::object_store::ChunkStorageError>) -> Self {
ChunkStorageError::new_err(value.0.to_string())
}
}
impl<T: Error> From<CError<IncorrectUsage<T>>> for PyErr {
fn from(value: CError<IncorrectUsage<T>>) -> Self {
IncorrectUsageError::new_err(value.0.to_string())
}
}
impl From<CError<SubmissionNotCancellable>> for PyErr {
fn from(value: CError<SubmissionNotCancellable>) -> Self {
let c: Option<common::ChunkFailed> = match &value.0 {
opsqueue::common::errors::SubmissionNotCancellable::Failed(submission, chunk) => Some(
common::ChunkFailed::from_internal(chunk.clone(), submission),
),
_ => None,
};
let s: common::SubmissionNotCancellable = value.0.into();
SubmissionNotCancellableError::new_err((s, c))
}
}
impl From<CError<SubmissionNotFound>> for PyErr {
fn from(value: CError<SubmissionNotFound>) -> Self {
let submission_id = value.0 .0;
SubmissionNotFoundError::new_err(u64::from(submission_id))
}
}
pub struct SubmissionFailed(
pub crate::common::SubmissionFailed,
pub crate::common::ChunkFailed,
);
impl From<CError<SubmissionFailed>> for PyErr {
fn from(value: CError<SubmissionFailed>) -> Self {
let submission: crate::common::SubmissionFailed = value.0 .0;
let chunk: crate::common::ChunkFailed = value.0 .1;
SubmissionFailedError::new_err((submission, chunk))
}
}
impl From<CError<crate::producer::SubmissionNotCompletedYetError>> for PyErr {
fn from(value: CError<crate::producer::SubmissionNotCompletedYetError>) -> Self {
let submission_id = value.0 .0;
SubmissionNotCompletedYetError::new_err((value.0.to_string(), submission_id))
}
}
impl From<CError<ChunkNotFound>> for PyErr {
fn from(value: CError<ChunkNotFound>) -> Self {
let ChunkId {
submission_id,
chunk_index,
} = value.0 .0;
ChunkNotFoundError::new_err((
value.0.to_string(),
(
SubmissionId::from(submission_id),
ChunkIndex::from(chunk_index),
),
))
}
}
impl From<CError<opsqueue::object_store::NewObjectStoreClientError>> for PyErr {
fn from(value: CError<opsqueue::object_store::NewObjectStoreClientError>) -> Self {
NewObjectStoreClientError::new_err(value.0.to_string())
}
}
impl From<CError<UnexpectedOpsqueueConsumerServerResponse>> for PyErr {
fn from(value: CError<UnexpectedOpsqueueConsumerServerResponse>) -> Self {
UnexpectedOpsqueueConsumerServerResponseError::new_err(value.0.to_string())
}
}
impl<T> From<PyErr> for CError<E<FatalPythonException, T>> {
fn from(value: PyErr) -> Self {
CError(E::L(FatalPythonException(value)))
}
}
impl<T> From<FatalPythonException> for CError<E<FatalPythonException, T>> {
fn from(value: FatalPythonException) -> Self {
CError(E::L(value))
}
}
impl<L, R> From<CError<E<L, R>>> for PyErr
where
PyErr: From<CError<L>> + From<CError<R>>, // CError<L>: Into<PyErr>,
// CError<R>: Into<PyErr>,
{
fn from(value: CError<E<L, R>>) -> Self {
match value.0 {
E::L(e) => CError(e).into(),
E::R(e) => CError(e).into(),
}
}
}
impl From<CError<PyErr>> for PyErr {
fn from(value: CError<PyErr>) -> Self {
value.0
}
}
impl<'py, T> pyo3::IntoPyObject<'py> for CError<T>
where
CError<T>: Into<PyErr>,
{
type Target = PyBaseException;
type Output = Bound<'py, Self::Target>;
type Error = std::convert::Infallible;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
Ok(CError(self.0).into().value(py).clone())
}
}