browser_automation_cli/
error.rs1use std::fmt;
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum ErrorKind {
26 Usage,
28 Data,
30 NoInput,
32 Unavailable,
34 Software,
36 Browser,
38 Protocol,
40 Timeout,
42 Cancelled,
44 BrokenPipe,
46 Config,
48 Io,
50}
51
52impl ErrorKind {
53 pub fn exit_code(self) -> u8 {
55 match self {
56 ErrorKind::Usage => 2,
57 ErrorKind::Data => 65,
58 ErrorKind::NoInput => 66,
59 ErrorKind::Unavailable => 69,
60 ErrorKind::Software | ErrorKind::Browser | ErrorKind::Protocol => 70,
61 ErrorKind::Config => 78,
62 ErrorKind::Io => 74,
63 ErrorKind::Timeout => 124,
64 ErrorKind::Cancelled => 130,
65 ErrorKind::BrokenPipe => 141,
66 }
67 }
68
69 pub fn as_str(self) -> &'static str {
71 match self {
72 ErrorKind::Usage => "usage",
73 ErrorKind::Data => "data",
74 ErrorKind::NoInput => "no-input",
75 ErrorKind::Unavailable => "unavailable",
76 ErrorKind::Software => "software",
77 ErrorKind::Browser => "browser",
78 ErrorKind::Protocol => "protocol",
79 ErrorKind::Timeout => "timeout",
80 ErrorKind::Cancelled => "cancelled",
81 ErrorKind::BrokenPipe => "broken-pipe",
82 ErrorKind::Config => "config",
83 ErrorKind::Io => "io",
84 }
85 }
86}
87
88#[derive(Debug, Clone)]
90pub struct CliError {
91 kind: ErrorKind,
92 message: String,
93 suggestion: Option<String>,
94 data: Option<serde_json::Value>,
96}
97
98impl CliError {
99 pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
101 Self {
102 kind,
103 message: message.into(),
104 suggestion: None,
105 data: None,
106 }
107 }
108
109 pub fn with_suggestion(
111 kind: ErrorKind,
112 message: impl Into<String>,
113 suggestion: impl Into<String>,
114 ) -> Self {
115 Self {
116 kind,
117 message: message.into(),
118 suggestion: Some(suggestion.into()),
119 data: None,
120 }
121 }
122
123 pub fn with_data(mut self, data: serde_json::Value) -> Self {
125 self.data = Some(data);
126 self
127 }
128
129 pub fn kind(&self) -> ErrorKind {
131 self.kind
132 }
133
134 pub fn message(&self) -> &str {
136 &self.message
137 }
138
139 pub fn suggestion(&self) -> Option<&str> {
141 self.suggestion.as_deref()
142 }
143
144 pub fn data(&self) -> Option<&serde_json::Value> {
146 self.data.as_ref()
147 }
148
149 pub fn exit_code(&self) -> u8 {
151 self.kind.exit_code()
152 }
153}
154
155impl fmt::Display for CliError {
156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 write!(f, "{}", self.message)
158 }
159}
160
161impl std::error::Error for CliError {}