statsig_rust/networking/
network_error.rs1use serde::Serialize;
2use std::fmt;
3
4use crate::logging_utils::sanitize_secret_key;
5
6type RequestUrl = String;
7
8#[derive(PartialEq, Debug, Clone, Serialize)]
9pub enum NetworkError {
10 ShutdownError(RequestUrl),
11 DisableNetworkOn(RequestUrl),
12 SerializationError(RequestUrl, String),
13
14 RequestFailed(RequestUrl, Option<u16>, String),
15 RetriesExhausted(RequestUrl, Option<u16>, u32, String),
16 RequestNotRetryable(RequestUrl, Option<u16>, String),
17}
18
19impl NetworkError {
20 pub fn name(&self) -> &'static str {
21 match self {
22 NetworkError::ShutdownError(_) => "ShutdownError",
23 NetworkError::DisableNetworkOn(_) => "DisableNetworkOn",
24 NetworkError::SerializationError(_, _) => "SerializationError",
25 NetworkError::RequestFailed(_, _, _) => "RequestFailed",
26 NetworkError::RetriesExhausted(_, _, _, _) => "RetriesExhausted",
27 NetworkError::RequestNotRetryable(_, _, _) => "RequestNotRetryable",
28 }
29 }
30}
31
32impl fmt::Display for NetworkError {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 match self {
35 NetworkError::ShutdownError(url) => {
36 let url = sanitize_secret_key(url);
37 write!(f, "ShutdownError: {url}")
38 }
39 NetworkError::DisableNetworkOn(url) => {
40 let url = sanitize_secret_key(url);
41 write!(f, "DisableNetworkOn: {url}")
42 }
43 NetworkError::SerializationError(url, s) => {
44 let url = sanitize_secret_key(url);
45 let s = sanitize_secret_key(s);
46 write!(f, "SerializationError: {url} {s}")
47 }
48
49 NetworkError::RequestFailed(url, status, message) => {
50 let url = sanitize_secret_key(url);
51 let message = sanitize_secret_key(message);
52 let status_display = match status {
53 Some(code) => code.to_string(),
54 None => "None".to_string(),
55 };
56 write!(f, "RequestFailed: {url} {status_display} {message}")
57 }
58 NetworkError::RetriesExhausted(url, status, attempts, message) => {
59 let url = sanitize_secret_key(url);
60 let message = sanitize_secret_key(message);
61 let status_display = match status {
62 Some(code) => code.to_string(),
63 None => "None".to_string(),
64 };
65 write!(
66 f,
67 "RetriesExhausted: {url} status({status_display}) attempts({attempts}) {message}"
68 )
69 }
70 NetworkError::RequestNotRetryable(url, status, message) => {
71 let url = sanitize_secret_key(url);
72 let message = sanitize_secret_key(message);
73 let status_display = match status {
74 Some(code) => code.to_string(),
75 None => "None".to_string(),
76 };
77 write!(
78 f,
79 "RequestNotRetryable: {url} status({status_display}) {message}"
80 )
81 }
82 }
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn test_sanitize_secret_key_in_error_display() {
92 let err = NetworkError::RetriesExhausted(
93 "https://api.statsigcdn.com/v2/download_config_specs/secret-fakeO1234567890.json"
94 .to_string(),
95 None,
96 1,
97 "Invalid array length".to_string(),
98 );
99
100 let message = err.to_string();
101 assert!(message.contains("secret-fakeO*****.json"));
102 assert!(!message.contains("secret-fakeO1234567890"));
103 }
104}