statsig_rust/networking/
network_error.rs1use serde::Serialize;
2use std::fmt;
3
4type RequestUrl = String;
5
6#[derive(PartialEq, Debug, Clone, Serialize)]
7pub enum NetworkError {
8 ShutdownError(RequestUrl),
9 DisableNetworkOn(RequestUrl),
10 SerializationError(RequestUrl, String),
11
12 RequestFailed(RequestUrl, Option<u16>, String),
13 RetriesExhausted(RequestUrl, Option<u16>, u32, String),
14 RequestNotRetryable(RequestUrl, Option<u16>, String),
15}
16
17impl NetworkError {
18 pub fn name(&self) -> &'static str {
19 match self {
20 NetworkError::ShutdownError(_) => "ShutdownError",
21 NetworkError::DisableNetworkOn(_) => "DisableNetworkOn",
22 NetworkError::SerializationError(_, _) => "SerializationError",
23 NetworkError::RequestFailed(_, _, _) => "RequestFailed",
24 NetworkError::RetriesExhausted(_, _, _, _) => "RetriesExhausted",
25 NetworkError::RequestNotRetryable(_, _, _) => "RequestNotRetryable",
26 }
27 }
28}
29
30impl fmt::Display for NetworkError {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 NetworkError::ShutdownError(url) => write!(f, "ShutdownError: {url}"),
34 NetworkError::DisableNetworkOn(url) => write!(f, "DisableNetworkOn: {url}"),
35 NetworkError::SerializationError(url, s) => write!(f, "SerializationError: {url} {s}"),
36
37 NetworkError::RequestFailed(url, status, message) => {
38 let status_display = match status {
39 Some(code) => code.to_string(),
40 None => "None".to_string(),
41 };
42 write!(f, "RequestFailed: {url} {status_display} {message}")
43 }
44 NetworkError::RetriesExhausted(url, status, attempts, message) => {
45 let status_display = match status {
46 Some(code) => code.to_string(),
47 None => "None".to_string(),
48 };
49 write!(
50 f,
51 "RetriesExhausted: {url} status({status_display}) attempts({attempts}) {message}"
52 )
53 }
54 NetworkError::RequestNotRetryable(url, status, message) => {
55 let status_display = match status {
56 Some(code) => code.to_string(),
57 None => "None".to_string(),
58 };
59 write!(
60 f,
61 "RequestNotRetryable: {url} status({status_display}) {message}"
62 )
63 }
64 }
65 }
66}