gosh_dl/protocol/
error.rs1use serde::{Deserialize, Serialize};
8use std::fmt;
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
15pub enum ProtocolError {
16 NotFound {
18 id: String,
20 },
21
22 InvalidState {
24 action: String,
26 current_state: String,
28 },
29
30 InvalidInput {
32 field: String,
34 message: String,
36 },
37
38 Network {
40 message: String,
42 retryable: bool,
44 },
45
46 Storage {
48 message: String,
50 },
51
52 Shutdown,
54
55 Internal {
57 message: String,
59 },
60}
61
62impl fmt::Display for ProtocolError {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 match self {
65 Self::NotFound { id } => write!(f, "Download not found: {}", id),
66 Self::InvalidState {
67 action,
68 current_state,
69 } => write!(f, "Cannot {} while {}", action, current_state),
70 Self::InvalidInput { field, message } => {
71 write!(f, "Invalid input for '{}': {}", field, message)
72 }
73 Self::Network { message, .. } => write!(f, "Network error: {}", message),
74 Self::Storage { message } => write!(f, "Storage error: {}", message),
75 Self::Shutdown => write!(f, "Engine is shutting down"),
76 Self::Internal { message } => write!(f, "Internal error: {}", message),
77 }
78 }
79}
80
81impl std::error::Error for ProtocolError {}
82
83impl ProtocolError {
84 pub fn is_retryable(&self) -> bool {
86 match self {
87 Self::Network { retryable, .. } => *retryable,
88 _ => false,
89 }
90 }
91}
92
93pub type ProtocolResult<T> = std::result::Result<T, ProtocolError>;