Skip to content

Commit 9a96133

Browse files
committed
improve error messages
1 parent 9c3a37f commit 9a96133

File tree

6 files changed

+54
-76
lines changed

6 files changed

+54
-76
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ quick-xml = { version = "0.36.0", features = ["serialize"] }
1212
serde = { version = "1.0", features = [ "derive" ] }
1313
ssh2 = "0.9"
1414
ssh2-config = "0.2"
15+
thiserror = "1.0.64"
1516

1617
[lib]
1718
name = "rucli"

src/main.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ fn main() {
114114

115115
netconf_session.lock_configuration().unwrap();
116116

117-
netconf_session.load_configuration(data).unwrap();
117+
if let Err(e) = netconf_session.load_configuration(data) {
118+
eprintln!("Config load failed: {}", e);
119+
std::process::exit(1);
120+
}
118121

119122
let diff_reply = netconf_session
120123
.diff_configuration("text".to_string())
@@ -139,7 +142,10 @@ fn main() {
139142

140143
let _ = netconf_session.lock_configuration().unwrap();
141144

142-
netconf_session.load_configuration(data).unwrap();
145+
if let Err(e) = netconf_session.load_configuration(data) {
146+
eprintln!("Config load failed: {}", e);
147+
std::process::exit(1);
148+
}
143149

144150
let diff_reply = netconf_session
145151
.diff_configuration("text".to_string())

src/netconf/error.rs

+11-29
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
11
use super::xml::RPCError;
22
use crate::netconf::RPCReplyCommand;
33

4-
#[derive(Debug)]
4+
#[derive(Debug, thiserror::Error)]
55
pub enum NETCONFError {
6-
IoError(std::io::Error),
7-
XmlError(quick_xml::Error),
8-
XmlDeError(quick_xml::DeError),
6+
#[error("{0}")]
7+
IoError(#[from] std::io::Error),
8+
#[error("{0}")]
9+
XmlError(#[from] quick_xml::Error),
10+
#[error("{0}")]
11+
XmlDeError(#[from] quick_xml::DeError),
12+
#[error("Missing OK")]
913
MissingOk,
14+
#[error("Unexpected command: {0}")]
1015
UnexpectedCommand(RPCReplyCommand),
11-
RpcError(RPCError),
16+
#[error("{0}")]
17+
RpcError(#[from] RPCError),
1218
}
1319

1420
pub type NETCONFResult<T> = Result<T, NETCONFError>;
15-
16-
impl From<std::io::Error> for NETCONFError {
17-
fn from(err: std::io::Error) -> Self {
18-
NETCONFError::IoError(err)
19-
}
20-
}
21-
22-
impl From<quick_xml::Error> for NETCONFError {
23-
fn from(err: quick_xml::Error) -> Self {
24-
NETCONFError::XmlError(err)
25-
}
26-
}
27-
28-
impl From<quick_xml::DeError> for NETCONFError {
29-
fn from(err: quick_xml::DeError) -> Self {
30-
NETCONFError::XmlDeError(err)
31-
}
32-
}
33-
34-
impl From<RPCError> for NETCONFError {
35-
fn from(err: RPCError) -> Self {
36-
NETCONFError::RpcError(err)
37-
}
38-
}

src/netconf/mod.rs

+4-28
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,7 @@ impl NETCONFClient {
102102
match result {
103103
RPCReplyCommand::RPCError(error) => {
104104
if error.error_severity == "warning" {
105-
let mut msg = "Warning: ".to_string();
106-
if let Some(error_path) = error.error_path {
107-
msg.push_str(&error_path);
108-
msg.push_str(&" ");
109-
}
110-
msg.push_str(&error.error_message);
111-
eprintln!("{}", msg);
105+
eprintln!("{}", error);
112106
} else {
113107
return Err(error.into());
114108
}
@@ -172,13 +166,7 @@ impl NETCONFClient {
172166
match result {
173167
RPCReplyCommand::RPCError(error) => {
174168
if error.error_severity == "warning" {
175-
let mut msg = "Warning: ".to_string();
176-
if let Some(error_path) = error.error_path {
177-
msg.push_str(&error_path);
178-
msg.push_str(&" ");
179-
}
180-
msg.push_str(&error.error_message);
181-
eprintln!("{}", msg);
169+
eprintln!("{}", error);
182170
} else {
183171
return Err(error.into());
184172
}
@@ -201,13 +189,7 @@ impl NETCONFClient {
201189
match result {
202190
RPCReplyCommand::RPCError(error) => {
203191
if error.error_severity == "warning" {
204-
let mut msg = "Warning: ".to_string();
205-
if let Some(error_path) = error.error_path {
206-
msg.push_str(&error_path);
207-
msg.push_str(&" ");
208-
}
209-
msg.push_str(&error.error_message);
210-
eprintln!("{}", msg);
192+
eprintln!("{}", error);
211193
} else {
212194
return Err(error.into());
213195
}
@@ -246,13 +228,7 @@ impl NETCONFClient {
246228
match result {
247229
LoadConfigurationResultsEnum::RPCError(error) => {
248230
if error.error_severity == "warning" {
249-
let mut msg = "Warning: ".to_string();
250-
if let Some(error_path) = error.error_path {
251-
msg.push_str(&error_path);
252-
msg.push_str(&" ");
253-
}
254-
msg.push_str(&error.error_message);
255-
eprintln!("{}", msg);
231+
eprintln!("{}", error);
256232
} else {
257233
return Err(error.into());
258234
}

src/netconf/xml.rs

+29-17
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,20 @@ impl Display for RPCReplyCommand {
190190
}
191191
}
192192

193-
#[derive(Debug, Deserialize, Serialize)]
194-
195-
pub struct RPCErrorList {
196-
element: Vec<RPCError>,
197-
}
198-
199193
#[derive(Debug, Deserialize, Serialize)]
200194
#[serde(deny_unknown_fields)]
201195
pub struct RPCError {
202196
#[serde(rename = "error-severity")]
203197
pub error_severity: String,
204-
#[serde(rename = "error-path")]
205-
pub error_path: Option<String>,
206198
#[serde(rename = "error-message")]
207199
pub error_message: String,
200+
201+
#[serde(rename = "error-path")]
202+
pub error_path: Option<String>,
203+
#[serde(rename = "error-type")]
204+
pub error_type: Option<String>,
205+
#[serde(rename = "error-tag")]
206+
pub error_tag: Option<String>,
208207
#[serde(rename = "error-info")]
209208
pub error_info: Option<RPCErrorInfo>,
210209
#[serde(rename = "source-daemon")]
@@ -215,21 +214,34 @@ pub struct RPCError {
215214
#[serde(deny_unknown_fields)]
216215
pub struct RPCErrorInfo {
217216
#[serde(rename = "bad-element")]
218-
pub bad_element: Option<String>,
217+
pub bad_element: String,
219218
}
220219

221220
impl Display for RPCError {
222221
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
223-
writeln!(
222+
write!(
224223
f,
225-
"{} at {:?} {:?}",
226-
self.error_severity,
227-
self.error_path,
228-
self.error_info
229-
.as_ref()
230-
.map(|error_info| &error_info.bad_element)
224+
"{}",
225+
self.error_severity
231226
)?;
232-
writeln!(f, "{}", self.error_message)?;
227+
if let Some(error_path) = &self.error_path {
228+
write!(
229+
f,
230+
" {}",
231+
error_path
232+
)?;
233+
}
234+
write!(f, ": {}", self.error_message)?;
235+
if let Some(error_info) = &self.error_info {
236+
write!(
237+
f,
238+
" (bad element: {})",
239+
error_info.bad_element
240+
)?;
241+
}
242+
write!(f, "")?;
233243
Ok(())
234244
}
235245
}
246+
247+
impl std::error::Error for RPCError { }

0 commit comments

Comments
 (0)