1#[derive(Debug)]
4pub struct GraphQLApiFailure {
5 pub errors: Vec<cynic::GraphQlError>,
6}
7
8impl GraphQLApiFailure {
9 pub fn from_errors(
10 msg: impl Into<String>,
11 errors: Option<Vec<cynic::GraphQlError>>,
12 ) -> anyhow::Error {
13 let msg = msg.into();
14 if let Some(errs) = errors {
15 if !errs.is_empty() {
16 let err = GraphQLApiFailure { errors: errs };
17 return anyhow::Error::new(err).context(msg);
18 }
19 }
20 anyhow::anyhow!("{msg} - query did not return any data")
21 }
22}
23
24impl std::fmt::Display for GraphQLApiFailure {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 let errs = self
27 .errors
28 .iter()
29 .map(|err| err.to_string())
30 .collect::<Vec<_>>()
31 .join(", ");
32 write!(f, "GraphQL API failure: {}", errs)
33 }
34}
35
36impl std::error::Error for GraphQLApiFailure {}