Skip to main content

shopify_storefront/
error.rs

1use thiserror::Error;
2
3/// A GraphQL error returned by the Shopify Storefront API.
4#[derive(Debug, serde::Deserialize)]
5pub struct GraphqlError {
6    pub message: String,
7    #[serde(default)]
8    pub extensions: Option<serde_json::Value>,
9}
10
11impl std::fmt::Display for GraphqlError {
12    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13        write!(f, "{}", self.message)
14    }
15}
16
17/// Errors that can occur when interacting with the Shopify Storefront API.
18#[derive(Debug, Error)]
19pub enum Error {
20    /// An HTTP-level error from reqwest.
21    #[error("HTTP error: {0}")]
22    Http(#[from] reqwest::Error),
23
24    /// The API returned one or more GraphQL errors.
25    #[error("GraphQL errors: {}", format_errors(.0))]
26    GraphQL(Vec<GraphqlError>),
27}
28
29fn format_errors(errors: &[GraphqlError]) -> String {
30    errors
31        .iter()
32        .map(|e| e.message.as_str())
33        .collect::<Vec<_>>()
34        .join(", ")
35}