gesha_core/conversions/
error.rs

1use openapi_types::v3_0::{OpenApiDataType, SchemaObject};
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        object: SchemaObject,
38    },
39}
40
41pub fn by_key(key: impl Into<String>) -> impl FnOnce(Error) -> Error {
42    move |cause| Error::Enclosed {
43        key: key.into(),
44        causes: vec![cause],
45    }
46}
47
48pub fn with_key(key: impl Into<String>) -> impl FnOnce(Vec<Error>) -> Error {
49    move |causes| Error::Enclosed {
50        key: key.into(),
51        causes,
52    }
53}
54
55impl From<Vec<Error>> for Error {
56    fn from(mut causes: Vec<Error>) -> Self {
57        if causes.len() == 1 {
58            causes.remove(0)
59        } else {
60            Error::Multiple { causes }
61        }
62    }
63}
64
65#[macro_export]
66macro_rules! broken {
67    ($shape: expr) => {
68        $crate::conversions::Error::TransformBroken {
69            detail: format!(
70                "unprocessed shape found:\n  at {file}:{line}\n{shape:#?}",
71                file = file!(),
72                line = line!(),
73                shape = $shape,
74            ),
75        }
76    };
77}