forked from Echo-Mirror-Butler/echomirror-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.rs
More file actions
32 lines (30 loc) · 1.58 KB
/
Copy patherrors.rs
File metadata and controls
32 lines (30 loc) · 1.58 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
use echomirror_core::EchoMirrorError;
use pyo3::exceptions::PyException;
use pyo3::{create_exception, PyErr};
// Base exception for everything raised by the SDK, mirroring the error
// hierarchy already exposed by the JS (`EchoMirrorError`/`AuthError`/...)
// and Flutter (`EchoMirrorError`/`EchoMirrorAuthError`/...) packages.
create_exception!(_echomirror, EchoMirrorException, PyException);
create_exception!(_echomirror, AuthError, EchoMirrorException);
create_exception!(_echomirror, NetworkError, EchoMirrorException);
create_exception!(_echomirror, RateLimitError, EchoMirrorException);
create_exception!(_echomirror, NotFoundError, EchoMirrorException);
create_exception!(_echomirror, ConfigError, EchoMirrorException);
/// Map a Rust-side `EchoMirrorError` onto the matching Python exception type.
pub fn to_py_err(err: EchoMirrorError) -> PyErr {
match err {
EchoMirrorError::Auth(msg) => AuthError::new_err(msg),
EchoMirrorError::AuthExpired => AuthError::new_err("Authentication token expired"),
EchoMirrorError::RateLimit { retry_after_secs } => RateLimitError::new_err(format!(
"Rate limit exceeded — retry after {retry_after_secs}s"
)),
EchoMirrorError::Network(e) => NetworkError::new_err(e.to_string()),
EchoMirrorError::NotFound(msg) => NotFoundError::new_err(msg),
EchoMirrorError::Config(msg) => ConfigError::new_err(msg),
EchoMirrorError::Http {
status: 404,
message,
} => NotFoundError::new_err(message),
other => EchoMirrorException::new_err(other.to_string()),
}
}