mz_cloud_api/
error.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! This module defines custom error types and structs related to the Materialize cloud API.
11//!
12//! [`ApiError`] is an error struct that represents an error returned by the
13//! Materialize cloud API. It contains information about the HTTP status code and
14//! a vector of error messages.
15//!
16//! [`Error`](`enum@Error`) is a custom error type
17//!
18//! It contains three variants:
19//! * [`Error::Transport`]: indicates a transport error from the `reqwest`
20//!   crate during a network request.
21//! * [`Error::Api`]: indicates a Materialize cloud API error while
22//!   processing the request.
23//! * [`Error::EmptyRegion`]: indicates an error when no region is
24//!   available in a requested cloud region.
25//! * [`Error::CloudProviderRegionParseError`]: indicates an error trying to parse
26//!   a cloud provider region. Always make sure the string is correctly formatted.
27
28use std::fmt;
29
30use reqwest::StatusCode;
31use thiserror::Error;
32use url::ParseError;
33
34/// An error returned by the Materialize cloud API.
35#[derive(Debug, Clone)]
36pub struct ApiError {
37    /// The HTTP status code.
38    pub status_code: StatusCode,
39    /// A detailed message about the error conditions.
40    pub messages: Vec<String>,
41}
42
43impl fmt::Display for ApiError {
44    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45        write!(
46            f,
47            "{} (status {})",
48            self.messages.join(","),
49            self.status_code
50        )
51    }
52}
53
54impl std::error::Error for ApiError {}
55
56/// A custom error type containing all the possible errors in the crate for the Materialize cloud API.
57#[derive(Error, Debug)]
58pub enum Error {
59    /// Indicates a transport error from the `reqwest`
60    /// crate during a network request.
61    #[error("Network error during a Materialize cloud API request: {0}")]
62    Transport(#[from] reqwest::Error),
63    /// Indicates a Materialize cloud API error from a request.
64    #[error("API error during a Materialize cloud API request: {0}")]
65    Api(#[from] ApiError),
66    /// Indicates a Materialize admin error from a request.
67    #[error("API error during a Materialize cloud API request: {0}")]
68    AdminApi(#[from] mz_frontegg_client::error::Error),
69    /// Indicates an error when no customer region is
70    /// available in a requested region.
71    #[error("No region available in this cloud region.")]
72    EmptyRegion,
73    /// Indicates an error trying to parse a
74    /// cloud provider region.
75    /// Always make sure the string is correctly formatted.
76    #[error("Error parsing cloud provider.")]
77    CloudProviderRegionParseError,
78    /// Indicates an error when trying to retrieve the
79    /// domain from the client endpoint
80    #[error("Failed to retrieve domain from client endpoint.")]
81    InvalidEndpointDomain,
82    /// Indicates a Materialize cloud API error from a request.
83    #[error("Error trying to parse the url: {0}")]
84    UrlParseError(#[from] ParseError),
85    /// Indicates the URL is cannot-be-a-base.
86    #[error("Error while manipulating URL. The URL is cannot-be-a-base.")]
87    UrlBaseError,
88    /// Indicates that a timeout has been reached.
89    #[error("Timeout reached.")]
90    TimeoutError,
91    /// Indicates that the request was executed successfully but returns no content (204).
92    /// E.g.: It happens when trying to request information from a not enabled region.
93    #[error("Succesfull but no content.")]
94    SuccesfullButNoContent,
95}