use crate::error::StixError as RustStixError;
use crate::python_bindings::convert::json_value_to_py;
use pyo3::prelude::*;
use pyo3::{create_exception, PyErr};
create_exception!(stixflayer, StixError, pyo3::exceptions::PyException);
create_exception!(stixflayer, ValidationError, StixError);
create_exception!(stixflayer, DeserializationError, StixError);
pub fn errors_to_py(py: Python<'_>, error: &RustStixError) -> PyObject {
let entries = error.errors();
let value = serde_json::to_value(entries).expect("ErrorEntry serializes to JSON");
json_value_to_py(py, &value)
}
pub fn stix_to_pyerr(error: RustStixError) -> PyErr {
Python::with_gil(|py| {
let message = error.display_message();
let py_errors = errors_to_py(py, &error);
let err = match error.envelope_category() {
"validation" => PyErr::new::<ValidationError, _>((message,)),
"deserialization" => PyErr::new::<DeserializationError, _>((message,)),
_ => PyErr::new::<StixError, _>((message,)),
};
err.value_bound(py).setattr("errors", py_errors).ok();
err
})
}