Skip to content

Commit 70a3c08

Browse files
committed
error
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 1b8bddd commit 70a3c08

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

python/frequenz/microgrid_component_graph/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33

44
"""Python bindings for the Frequenz microgrid component graph rust library."""
55

6-
from ._component_graph import ComponentGraph, ComponentGraphConfig, InvalidGraphError
6+
from ._component_graph import (
7+
ComponentGraph,
8+
ComponentGraphConfig,
9+
FormulaGenerationError,
10+
InvalidGraphError,
11+
)
712

813
__all__ = [
914
"ComponentGraph",
1015
"ComponentGraphConfig",
16+
"FormulaGenerationError",
1117
"InvalidGraphError",
1218
]

python/frequenz/microgrid_component_graph/__init__.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ from typing import AbstractSet, Generic, Protocol, TypeVar
1111
class InvalidGraphError(Exception):
1212
"""Exception type that will be thrown if graph data is not valid."""
1313

14+
class FormulaGenerationError(Exception):
15+
"""Encountered during formula generation from the component graph."""
16+
1417
class ComponentIdProtocol(Protocol):
1518
def __int__(self) -> int:
1619
"""Get the integer representation of the Component ID."""

src/graph.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::collections::BTreeSet;
55

66
use crate::{
7-
InvalidGraphError,
7+
FormulaGenerationError, InvalidGraphError,
88
category::{category_from_python_component, match_category},
99
component::Component,
1010
connection::Connection,
@@ -210,21 +210,21 @@ impl ComponentGraph {
210210
self.graph
211211
.consumer_formula()
212212
.map(|f| f.to_string())
213-
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
213+
.map_err(|e| PyErr::new::<FormulaGenerationError, _>(e.to_string()))
214214
}
215215

216216
fn producer_formula(&self) -> PyResult<String> {
217217
self.graph
218218
.producer_formula()
219219
.map(|f| f.to_string())
220-
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
220+
.map_err(|e| PyErr::new::<FormulaGenerationError, _>(e.to_string()))
221221
}
222222

223223
fn grid_formula(&self) -> PyResult<String> {
224224
self.graph
225225
.grid_formula()
226226
.map(|f| f.to_string())
227-
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
227+
.map_err(|e| PyErr::new::<FormulaGenerationError, _>(e.to_string()))
228228
}
229229

230230
#[pyo3(signature = (battery_ids=None))]
@@ -236,15 +236,15 @@ impl ComponentGraph {
236236
self.graph
237237
.battery_formula(extract_ids(py, battery_ids)?)
238238
.map(|f| f.to_string())
239-
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
239+
.map_err(|e| PyErr::new::<FormulaGenerationError, _>(e.to_string()))
240240
}
241241

242242
#[pyo3(signature = (chp_ids=None))]
243243
fn chp_formula(&self, py: Python<'_>, chp_ids: Option<Bound<'_, PyAny>>) -> PyResult<String> {
244244
self.graph
245245
.chp_formula(extract_ids(py, chp_ids)?)
246246
.map(|f| f.to_string())
247-
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
247+
.map_err(|e| PyErr::new::<FormulaGenerationError, _>(e.to_string()))
248248
}
249249

250250
#[pyo3(signature = (pv_inverter_ids=None))]
@@ -256,7 +256,7 @@ impl ComponentGraph {
256256
self.graph
257257
.pv_formula(extract_ids(py, pv_inverter_ids)?)
258258
.map(|f| f.to_string())
259-
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
259+
.map_err(|e| PyErr::new::<FormulaGenerationError, _>(e.to_string()))
260260
}
261261

262262
#[pyo3(signature = (ev_charger_ids=None))]
@@ -268,14 +268,14 @@ impl ComponentGraph {
268268
self.graph
269269
.ev_charger_formula(extract_ids(py, ev_charger_ids)?)
270270
.map(|f| f.to_string())
271-
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
271+
.map_err(|e| PyErr::new::<FormulaGenerationError, _>(e.to_string()))
272272
}
273273

274274
fn grid_coalesce_formula(&self) -> PyResult<String> {
275275
self.graph
276276
.grid_coalesce_formula()
277277
.map(|f| f.to_string())
278-
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
278+
.map_err(|e| PyErr::new::<FormulaGenerationError, _>(e.to_string()))
279279
}
280280

281281
#[pyo3(signature = (battery_ids=None))]
@@ -287,7 +287,7 @@ impl ComponentGraph {
287287
self.graph
288288
.battery_ac_coalesce_formula(extract_ids(py, battery_ids)?)
289289
.map(|f| f.to_string())
290-
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
290+
.map_err(|e| PyErr::new::<FormulaGenerationError, _>(e.to_string()))
291291
}
292292

293293
#[pyo3(signature = (pv_inverter_ids=None))]
@@ -299,7 +299,7 @@ impl ComponentGraph {
299299
self.graph
300300
.pv_ac_coalesce_formula(extract_ids(py, pv_inverter_ids)?)
301301
.map(|f| f.to_string())
302-
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))
302+
.map_err(|e| PyErr::new::<FormulaGenerationError, _>(e.to_string()))
303303
}
304304
}
305305

src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@ pyo3::create_exception!(
1616
InvalidGraphError,
1717
pyo3::exceptions::PyException
1818
);
19+
pyo3::create_exception!(
20+
_component_graph,
21+
FormulaGenerationError,
22+
pyo3::exceptions::PyException
23+
);
1924

2025
/// A Python module implemented in Rust.
2126
#[pymodule]
2227
mod _component_graph {
28+
#[pymodule_export]
29+
use super::FormulaGenerationError;
2330
#[pymodule_export]
2431
use super::InvalidGraphError;
2532

2633
#[pymodule_export]
2734
use crate::graph::ComponentGraph;
28-
2935
#[pymodule_export]
3036
use crate::graph::ComponentGraphConfig;
3137
}

0 commit comments

Comments
 (0)