zen_engine/
error.rs

1use crate::handler::graph::DecisionGraphValidationError;
2use crate::handler::node::NodeError;
3use crate::loader::LoaderError;
4use jsonschema::{ErrorIterator, ValidationError};
5use serde::ser::SerializeMap;
6use serde::{Serialize, Serializer};
7use serde_json::{Map, Value};
8use std::iter::once;
9use thiserror::Error;
10
11#[derive(Debug, Error)]
12pub enum EvaluationError {
13    #[error("Loader error")]
14    LoaderError(Box<LoaderError>),
15
16    #[error("Node error")]
17    NodeError(Box<NodeError>),
18
19    #[error("Depth limit exceeded")]
20    DepthLimitExceeded,
21
22    #[error("Invalid graph")]
23    InvalidGraph(Box<DecisionGraphValidationError>),
24
25    #[error("Validation failed")]
26    Validation(Box<Value>),
27}
28
29impl Serialize for EvaluationError {
30    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
31    where
32        S: Serializer,
33    {
34        let mut map = serializer.serialize_map(None)?;
35        match self {
36            EvaluationError::DepthLimitExceeded => {
37                map.serialize_entry("type", "DepthLimitExceeded")?;
38            }
39            EvaluationError::NodeError(err) => {
40                map.serialize_entry("type", "NodeError")?;
41                map.serialize_entry("nodeId", &err.node_id)?;
42                map.serialize_entry("source", &err.source.to_string())?;
43
44                if let Some(trace) = &err.trace {
45                    map.serialize_entry("trace", &trace)?;
46                }
47            }
48            EvaluationError::LoaderError(err) => {
49                map.serialize_entry("type", "LoaderError")?;
50                match err.as_ref() {
51                    LoaderError::Internal { key, source } => {
52                        map.serialize_entry("key", key)?;
53                        map.serialize_entry("source", &source.to_string())?;
54                    }
55                    LoaderError::NotFound(key) => {
56                        map.serialize_entry("key", key)?;
57                    }
58                }
59            }
60            EvaluationError::InvalidGraph(err) => {
61                map.serialize_entry("type", "InvalidGraph")?;
62                map.serialize_entry("source", err)?;
63            }
64            EvaluationError::Validation(err) => {
65                map.serialize_entry("type", "Validation")?;
66                map.serialize_entry("source", err)?;
67            }
68        }
69
70        map.end()
71    }
72}
73
74impl From<LoaderError> for Box<EvaluationError> {
75    fn from(error: LoaderError) -> Self {
76        Box::new(EvaluationError::LoaderError(error.into()))
77    }
78}
79
80impl From<Box<LoaderError>> for Box<EvaluationError> {
81    fn from(error: Box<LoaderError>) -> Self {
82        Box::new(EvaluationError::LoaderError(error))
83    }
84}
85
86impl From<NodeError> for Box<EvaluationError> {
87    fn from(error: NodeError) -> Self {
88        Box::new(EvaluationError::NodeError(error.into()))
89    }
90}
91
92impl From<Box<NodeError>> for Box<EvaluationError> {
93    fn from(error: Box<NodeError>) -> Self {
94        Box::new(EvaluationError::NodeError(error))
95    }
96}
97
98impl From<DecisionGraphValidationError> for Box<EvaluationError> {
99    fn from(error: DecisionGraphValidationError) -> Self {
100        Box::new(EvaluationError::InvalidGraph(error.into()))
101    }
102}
103
104#[derive(Serialize)]
105#[serde(rename_all = "camelCase")]
106struct ValidationErrorJson {
107    path: String,
108    message: String,
109}
110
111impl<'a> From<ValidationError<'a>> for ValidationErrorJson {
112    fn from(value: ValidationError<'a>) -> Self {
113        ValidationErrorJson {
114            path: value.instance_path.to_string(),
115            message: format!("{}", value),
116        }
117    }
118}
119
120impl<'a> From<ErrorIterator<'a>> for Box<EvaluationError> {
121    fn from(error_iter: ErrorIterator<'a>) -> Self {
122        let errors: Vec<ValidationErrorJson> = error_iter.into_iter().map(From::from).collect();
123
124        let mut json_map = Map::new();
125        json_map.insert(
126            "errors".to_string(),
127            serde_json::to_value(errors).unwrap_or_default(),
128        );
129
130        Box::new(EvaluationError::Validation(Box::new(Value::Object(
131            json_map,
132        ))))
133    }
134}
135
136impl<'a> From<ValidationError<'a>> for Box<EvaluationError> {
137    fn from(value: ValidationError<'a>) -> Self {
138        let iterator: ErrorIterator<'a> = Box::new(once(value));
139        Box::<EvaluationError>::from(iterator)
140    }
141}