Skip to content

Commit

Permalink
improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyuyureka committed Oct 8, 2024
1 parent 9c3a37f commit 9a96133
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 76 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ quick-xml = { version = "0.36.0", features = ["serialize"] }
serde = { version = "1.0", features = [ "derive" ] }
ssh2 = "0.9"
ssh2-config = "0.2"
thiserror = "1.0.64"

[lib]
name = "rucli"
Expand Down
10 changes: 8 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ fn main() {

netconf_session.lock_configuration().unwrap();

netconf_session.load_configuration(data).unwrap();
if let Err(e) = netconf_session.load_configuration(data) {
eprintln!("Config load failed: {}", e);
std::process::exit(1);
}

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

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

netconf_session.load_configuration(data).unwrap();
if let Err(e) = netconf_session.load_configuration(data) {
eprintln!("Config load failed: {}", e);
std::process::exit(1);
}

let diff_reply = netconf_session
.diff_configuration("text".to_string())
Expand Down
40 changes: 11 additions & 29 deletions src/netconf/error.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
use super::xml::RPCError;
use crate::netconf::RPCReplyCommand;

#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum NETCONFError {
IoError(std::io::Error),
XmlError(quick_xml::Error),
XmlDeError(quick_xml::DeError),
#[error("{0}")]
IoError(#[from] std::io::Error),
#[error("{0}")]
XmlError(#[from] quick_xml::Error),
#[error("{0}")]
XmlDeError(#[from] quick_xml::DeError),
#[error("Missing OK")]
MissingOk,
#[error("Unexpected command: {0}")]
UnexpectedCommand(RPCReplyCommand),
RpcError(RPCError),
#[error("{0}")]
RpcError(#[from] RPCError),
}

pub type NETCONFResult<T> = Result<T, NETCONFError>;

impl From<std::io::Error> for NETCONFError {
fn from(err: std::io::Error) -> Self {
NETCONFError::IoError(err)
}
}

impl From<quick_xml::Error> for NETCONFError {
fn from(err: quick_xml::Error) -> Self {
NETCONFError::XmlError(err)
}
}

impl From<quick_xml::DeError> for NETCONFError {
fn from(err: quick_xml::DeError) -> Self {
NETCONFError::XmlDeError(err)
}
}

impl From<RPCError> for NETCONFError {
fn from(err: RPCError) -> Self {
NETCONFError::RpcError(err)
}
}
32 changes: 4 additions & 28 deletions src/netconf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,7 @@ impl NETCONFClient {
match result {
RPCReplyCommand::RPCError(error) => {
if error.error_severity == "warning" {
let mut msg = "Warning: ".to_string();
if let Some(error_path) = error.error_path {
msg.push_str(&error_path);
msg.push_str(&" ");
}
msg.push_str(&error.error_message);
eprintln!("{}", msg);
eprintln!("{}", error);
} else {
return Err(error.into());
}
Expand Down Expand Up @@ -172,13 +166,7 @@ impl NETCONFClient {
match result {
RPCReplyCommand::RPCError(error) => {
if error.error_severity == "warning" {
let mut msg = "Warning: ".to_string();
if let Some(error_path) = error.error_path {
msg.push_str(&error_path);
msg.push_str(&" ");
}
msg.push_str(&error.error_message);
eprintln!("{}", msg);
eprintln!("{}", error);
} else {
return Err(error.into());
}
Expand All @@ -201,13 +189,7 @@ impl NETCONFClient {
match result {
RPCReplyCommand::RPCError(error) => {
if error.error_severity == "warning" {
let mut msg = "Warning: ".to_string();
if let Some(error_path) = error.error_path {
msg.push_str(&error_path);
msg.push_str(&" ");
}
msg.push_str(&error.error_message);
eprintln!("{}", msg);
eprintln!("{}", error);
} else {
return Err(error.into());
}
Expand Down Expand Up @@ -246,13 +228,7 @@ impl NETCONFClient {
match result {
LoadConfigurationResultsEnum::RPCError(error) => {
if error.error_severity == "warning" {
let mut msg = "Warning: ".to_string();
if let Some(error_path) = error.error_path {
msg.push_str(&error_path);
msg.push_str(&" ");
}
msg.push_str(&error.error_message);
eprintln!("{}", msg);
eprintln!("{}", error);
} else {
return Err(error.into());
}
Expand Down
46 changes: 29 additions & 17 deletions src/netconf/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,20 @@ impl Display for RPCReplyCommand {
}
}

#[derive(Debug, Deserialize, Serialize)]

pub struct RPCErrorList {
element: Vec<RPCError>,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RPCError {
#[serde(rename = "error-severity")]
pub error_severity: String,
#[serde(rename = "error-path")]
pub error_path: Option<String>,
#[serde(rename = "error-message")]
pub error_message: String,

#[serde(rename = "error-path")]
pub error_path: Option<String>,
#[serde(rename = "error-type")]
pub error_type: Option<String>,
#[serde(rename = "error-tag")]
pub error_tag: Option<String>,
#[serde(rename = "error-info")]
pub error_info: Option<RPCErrorInfo>,
#[serde(rename = "source-daemon")]
Expand All @@ -215,21 +214,34 @@ pub struct RPCError {
#[serde(deny_unknown_fields)]
pub struct RPCErrorInfo {
#[serde(rename = "bad-element")]
pub bad_element: Option<String>,
pub bad_element: String,
}

impl Display for RPCError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(
write!(
f,
"{} at {:?} {:?}",
self.error_severity,
self.error_path,
self.error_info
.as_ref()
.map(|error_info| &error_info.bad_element)
"{}",
self.error_severity
)?;
writeln!(f, "{}", self.error_message)?;
if let Some(error_path) = &self.error_path {
write!(
f,
" {}",
error_path
)?;
}
write!(f, ": {}", self.error_message)?;
if let Some(error_info) = &self.error_info {
write!(
f,
" (bad element: {})",
error_info.bad_element
)?;
}
write!(f, "")?;
Ok(())
}
}

impl std::error::Error for RPCError { }

0 comments on commit 9a96133

Please sign in to comment.