Skip to content

Commit 4b5f250

Browse files
authored
Exposed exceptions to Python API. (#22)
1 parent 77ff25f commit 4b5f250

File tree

10 files changed

+63
-20
lines changed

10 files changed

+63
-20
lines changed

python/natsrpy/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
from . import js
1+
from . import exceptions, js
22
from ._natsrpy_rs import CallbackSubscription, IteratorSubscription, Message, Nats
33

4-
__all__ = ["CallbackSubscription", "IteratorSubscription", "Message", "Nats", "js"]
4+
__all__ = [
5+
"CallbackSubscription",
6+
"IteratorSubscription",
7+
"Message",
8+
"Nats",
9+
"exceptions",
10+
"js",
11+
]

python/natsrpy/_natsrpy_rs/__init__.pyi

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ from typing import Any, final, overload
44

55
from typing_extensions import Self
66

7-
from . import js
7+
from . import exceptions, js
88

99
@final
1010
class Message:
@@ -107,4 +107,11 @@ class Nats:
107107
backpressure_on_inflight: bool | None = None,
108108
) -> js.JetStream: ...
109109

110-
__all__ = ["CallbackSubscription", "IteratorSubscription", "Message", "Nats", "js"]
110+
__all__ = [
111+
"CallbackSubscription",
112+
"IteratorSubscription",
113+
"Message",
114+
"Nats",
115+
"exceptions",
116+
"js",
117+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class NatsrpyBaseError(Exception): ...
2+
class NatsrpySessionError(NatsrpyBaseError): ...
3+
class NatsrpyPublishError(NatsrpyBaseError): ...
4+
5+
__all__ = [
6+
"NatsrpyBaseError",
7+
"NatsrpyPublishError",
8+
"NatsrpySessionError",
9+
]

python/natsrpy/exceptions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from ._natsrpy_rs.exceptions import (
2+
NatsrpyBaseError,
3+
NatsrpyPublishError,
4+
NatsrpySessionError,
5+
)
6+
7+
__all__ = [
8+
"NatsrpyBaseError",
9+
"NatsrpyPublishError",
10+
"NatsrpySessionError",
11+
]

python/natsrpy/js.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
ReplayPolicy,
1111
)
1212
from ._natsrpy_rs.js.kv import KeyValue, KVConfig
13-
from ._natsrpy_rs.js.object_store import ObjectStore, ObjectStoreConfig
13+
from ._natsrpy_rs.js.object_store import (
14+
ObjectInfo,
15+
ObjectInfoIterator,
16+
ObjectLink,
17+
ObjectStore,
18+
ObjectStoreConfig,
19+
)
1420
from ._natsrpy_rs.js.stream import (
1521
Compression,
1622
ConsumerLimits,
@@ -38,6 +44,9 @@
3844
"JetStream",
3945
"KVConfig",
4046
"KeyValue",
47+
"ObjectInfo",
48+
"ObjectInfoIterator",
49+
"ObjectLink",
4150
"ObjectStore",
4251
"ObjectStoreConfig",
4352
"PersistenceMode",

python/tests/conftest.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ async def nats(nats_url: str) -> AsyncGenerator[Nats, None]:
2727
nats = Nats(addrs=[nats_url])
2828
await nats.startup()
2929

30-
yield nats
31-
32-
await nats.shutdown()
30+
try:
31+
yield nats
32+
finally:
33+
await nats.shutdown()
3334

3435

3536
@pytest.fixture(scope="session")

python/tests/test_object_store.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
from pathlib import Path
66

77
import pytest
8-
from natsrpy._natsrpy_rs.js.object_store import (
8+
from natsrpy.js import (
9+
JetStream,
910
ObjectInfo,
1011
ObjectInfoIterator,
1112
ObjectLink,
12-
)
13-
from natsrpy.js import (
14-
JetStream,
1513
ObjectStore,
1614
ObjectStoreConfig,
1715
StorageType,

src/js/kv.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,15 @@ impl KeyValue {
181181
) -> NatsrpyResult<Bound<'py, PyAny>> {
182182
let store = self.store.clone();
183183
let data = bytes::Bytes::copy_from_slice(value.as_bytes());
184-
natsrpy_future(py, async move {
185-
let status = store.read().await.put(key, data).await?;
186-
Ok(status)
187-
})
184+
natsrpy_future(
185+
py,
186+
async move { Ok(store.read().await.put(key, data).await?) },
187+
)
188188
}
189189

190190
pub fn delete<'py>(&self, py: Python<'py>, key: String) -> NatsrpyResult<Bound<'py, PyAny>> {
191191
let store = self.store.clone();
192-
natsrpy_future(py, async move {
193-
let kv = store.read().await;
194-
Ok(kv.delete(key).await?)
195-
})
192+
natsrpy_future(py, async move { Ok(store.read().await.delete(key).await?) })
196193
}
197194
}
198195

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ pub mod _natsrpy_rs {
4040
#[pymodule_export]
4141
use super::subscriptions::{callback::CallbackSubscription, iterator::IteratorSubscription};
4242

43+
#[pymodule_export]
44+
use super::exceptions::py_err::pymod as exceptions;
45+
4346
#[pymodule_export]
4447
use super::js::pymod as js;
4548

src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ pub mod headers;
33
pub mod py;
44
pub mod py_types;
55
pub mod streamer;
6+
67
pub use futures::natsrpy_future;

0 commit comments

Comments
 (0)