gesha_core/conversions/
error.rs

1use openapi_types::v3_0::OpenApiDataType;
2
3pub type Result<A> = std::result::Result<A, Error>;
4
5pub type Output<A> = openapi_types::core::Output<A, Error>;
6
7#[derive(Debug)]
8pub enum Error {
9    Enclosed {
10        key: String,
11        causes: Vec<Error>,
12    },
13    Multiple {
14        causes: Vec<Error>,
15    },
16
17    /// ## Client Error
18    /// e.g. a reference to a schema that does not exist.
19    ReferenceObjectNotFound(String),
20
21    /// ## Client Error
22    /// e.g. a schema with an unknown format.
23    UnknownFormat {
24        data_type: OpenApiDataType,
25        format: String,
26    },
27
28    /// ## Internal Error
29    /// e.g. a shape that has not been processed.
30    TransformBroken {
31        detail: String,
32    },
33
34    /// ## Internal Error
35    Unimplemented {
36        message: String,
37    },
38}
39
40pub fn by_key(key: impl Into<String>) -> impl FnOnce(Error) -> Error {
41    move |cause| Error::Enclosed {
42        key: key.into(),
43        causes: vec![cause],
44    }
45}
46
47pub fn with_key(key: impl Into<String>) -> impl FnOnce(Vec<Error>) -> Error {
48    move |causes| Error::Enclosed {
49        key: key.into(),
50        causes,
51    }
52}
53
54impl From<Vec<Error>> for Error {
55    fn from(mut causes: Vec<Error>) -> Self {
56        if causes.len() == 1 {
57            causes.remove(0)
58        } else {
59            Error::Multiple { causes }
60        }
61    }
62}
63
64#[macro_export]
65macro_rules! broken {
66    ($shape: expr) => {
67        $crate::conversions::Error::TransformBroken {
68            detail: format!(
69                "unprocessed shape found:\n  at {file}:{line}\n{shape:#?}",
70                file = file!(),
71                line = line!(),
72                shape = $shape,
73            ),
74        }
75    };
76}