cx_sdk/error.rs
1// Copyright 2024 Coralogix Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use http::uri::InvalidUri;
16use tonic::transport::Error as TonicError;
17
18/// The result type for the SDK.
19pub type Result<T> = std::result::Result<T, SdkError>;
20
21/// An error returned by the underlying API.
22#[derive(Debug, thiserror::Error)]
23pub struct SdkApiError {
24 /// The status code of the error.
25 pub status: tonic::Status,
26 /// The endpoint that the error occurred on.
27 pub endpoint: String,
28 /// The feature group the endpoint belongs to.
29 pub feature_group: String,
30}
31
32impl std::fmt::Display for SdkApiError {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 write!(f, "API error on {}: {}", self.endpoint, self.status)
35 }
36}
37
38/// The error type for the SDK.
39#[derive(Debug, thiserror::Error)]
40pub enum SdkError {
41 /// This error is returned when the team that an operation refers to is not found.
42 #[error("Invalid endpoint URI: {0}")]
43 TeamNotFound(#[from] InvalidUri),
44
45 /// This error is returned when an endpoint is not found.
46 #[error("Endpoint not found: {0}")]
47 EndpointNotFound(#[from] TonicError),
48
49 /// This error is returned when an underying API throws an error.
50 #[error("API error: {0}")]
51 ApiError(#[from] SdkApiError),
52
53 /// This error is returned when an environment variable is not found or is invalid.
54 #[error("Invalid environment variable for CORALOGIX_REGION: {0}")]
55 EnvConfigurationError(#[from] std::env::VarError),
56}