growthbook_sdk_rust/
error.rs1use std::env::VarError;
2use std::error::Error;
3use std::fmt::{Display, Formatter};
4use std::num::ParseIntError;
5
6use chrono::OutOfRangeError;
7use reqwest::Response;
8
9#[derive(Debug)]
10pub enum GrowthbookErrorCode {
11 GenericError,
12 SerdeDeserialize,
13 ParseError,
14 DurationOutOfRangeError,
15 MissingEnvironmentVariable,
16 GrowthbookGateway,
17 GrowthbookGatewayDeserialize,
18 InvalidResponseValueType,
19 GrowthBookAttributeIsNotObject,
20}
21
22#[derive(Debug)]
23pub struct GrowthbookError {
24 pub code: GrowthbookErrorCode,
25 pub message: String,
26}
27
28impl GrowthbookError {
29 pub fn new(
30 code: GrowthbookErrorCode,
31 message: &str,
32 ) -> Self {
33 GrowthbookError { code, message: String::from(message) }
34 }
35}
36
37impl Display for GrowthbookError {
38 fn fmt(
39 &self,
40 f: &mut Formatter<'_>,
41 ) -> std::fmt::Result {
42 write!(f, "{}", self.message)
43 }
44}
45
46impl Error for GrowthbookError {
47 fn description(&self) -> &str {
48 &self.message
49 }
50}
51
52impl From<Box<dyn Error>> for GrowthbookError {
53 fn from(error: Box<dyn Error>) -> Self {
54 Self {
55 code: GrowthbookErrorCode::GenericError,
56 message: error.to_string(),
57 }
58 }
59}
60
61impl From<reqwest_middleware::Error> for GrowthbookError {
62 fn from(error: reqwest_middleware::Error) -> Self {
63 Self {
64 code: GrowthbookErrorCode::GrowthbookGateway,
65 message: error.to_string(),
66 }
67 }
68}
69
70impl From<reqwest::Error> for GrowthbookError {
71 fn from(error: reqwest::Error) -> Self {
72 Self {
73 code: GrowthbookErrorCode::GrowthbookGatewayDeserialize,
74 message: error.to_string(),
75 }
76 }
77}
78
79impl From<VarError> for GrowthbookError {
80 fn from(error: VarError) -> Self {
81 Self {
82 code: GrowthbookErrorCode::MissingEnvironmentVariable,
83 message: error.to_string(),
84 }
85 }
86}
87
88impl From<ParseIntError> for GrowthbookError {
89 fn from(error: ParseIntError) -> Self {
90 Self {
91 code: GrowthbookErrorCode::ParseError,
92 message: error.to_string(),
93 }
94 }
95}
96
97impl From<serde_json::Error> for GrowthbookError {
98 fn from(error: serde_json::Error) -> Self {
99 Self {
100 code: GrowthbookErrorCode::ParseError,
101 message: error.to_string(),
102 }
103 }
104}
105
106impl From<OutOfRangeError> for GrowthbookError {
107 fn from(error: OutOfRangeError) -> Self {
108 Self {
109 code: GrowthbookErrorCode::DurationOutOfRangeError,
110 message: error.to_string(),
111 }
112 }
113}
114
115impl From<Response> for GrowthbookError {
116 fn from(response: Response) -> Self {
117 Self {
118 code: GrowthbookErrorCode::GrowthbookGateway,
119 message: format!("Failed to get features. StatusCode={}", response.status()),
120 }
121 }
122}