Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more Error variants to CollectorError
Browse files Browse the repository at this point in the history
BytePaws committed Oct 23, 2024
1 parent ce771e6 commit 3788bb4
Showing 3 changed files with 34 additions and 11 deletions.
30 changes: 21 additions & 9 deletions src/collectors/collector_exceptions.rs
Original file line number Diff line number Diff line change
@@ -12,16 +12,16 @@
//! |------|-----------|-----------------------------|
//! |`20` |DmiError |Unable to execute `dmidecode`|
//! |`21` |UnableToCollectDataError|Unspecified Error with data collection. Usually appears when subprocess fails or an output is malformed.|
//! |`22` |--Undefined--|--Undefined--|
//! |`23` |--Undefined--|--Undefined--|
//! |`22` |InvalidNetworkInterfaceError|Unable to identify a Network Interface as such.|
//! |`23` |NoNetworkInterfacesException|Unable to find any Network Interfaces.|
//! |`24` |--Undefined--|--Undefined--|
//! |`25` |InvalidNetworkInterfaceError|Unable to identify a Network Interface as such.|
//! |`26` |NoNetworkInterfacesException|Unable to find any Network Interfaces.|
//! |`27` |--Undefined--|--Undefined--|
//! |`28` |--Undefined--|--Undefined--|
//! |`25` |--Undefined--|--Undefined--|
//! |`26` |UnableToParseUTF8|Nazara was unable to parse the output of your plugin from utf8.|
//! |`27` |InvalidPluginOutputError|Your Plugin did not return valid JSON output.|
//! |`28` |PluginExecutionError|Nazara was unable to execute your Plugin script.|
//! |`29` |--Reserved--|Used for the `Other` error type if no other code can be defined.|
//!
use std::{error::Error, fmt, process};
use std::{error::Error, fmt, process, string::FromUtf8Error};

use serde_json::Error as SerdeJsonError;

@@ -41,6 +41,7 @@ pub enum CollectorError {
UnableToCollectDataError(String),
InvalidNetworkInterfaceError(String),
NoNetworkInterfacesError(String),
UnableToParseUTF8(FromUtf8Error),
InvalidPluginOutputError(SerdeJsonError),
PluginExecutionError(String),
Other(String),
@@ -61,6 +62,9 @@ impl fmt::Display for CollectorError {
CollectorError::NoNetworkInterfacesError(ref err) => {
write!(f, "\x1b[31m[error]\x1b[0m Network Collector Error: {}", err)
}
CollectorError::UnableToParseUTF8(ref err) => {
write!(f, "\x1b[31m[error]\x1b[0m Unable to parse stdout from UTF8 to string: {}", err)
}
CollectorError::InvalidPluginOutputError(ref err) => {
write!(
f,
@@ -89,6 +93,7 @@ impl Error for CollectorError {
CollectorError::UnableToCollectDataError(_) => None,
CollectorError::InvalidNetworkInterfaceError(_) => None,
CollectorError::NoNetworkInterfacesError(_) => None,
CollectorError::UnableToParseUTF8(ref err) => Some(err),
CollectorError::InvalidPluginOutputError(ref err) => Some(err),
CollectorError::PluginExecutionError(_) => None,
CollectorError::Other(_) => None,
@@ -102,6 +107,12 @@ impl From<SerdeJsonError> for CollectorError {
}
}

impl From<FromUtf8Error> for CollectorError {
fn from(err: FromUtf8Error) -> Self {
CollectorError::UnableToParseUTF8(err)
}
}

impl CollectorError {
/// Abort the process, if necessary.
///
@@ -133,8 +144,9 @@ impl CollectorError {
match &self {
CollectorError::DmiError(_) => 20,
CollectorError::UnableToCollectDataError(_) => 21,
CollectorError::InvalidNetworkInterfaceError(_) => 25,
CollectorError::NoNetworkInterfacesError(_) => 26,
CollectorError::InvalidNetworkInterfaceError(_) => 22,
CollectorError::NoNetworkInterfacesError(_) => 23,
CollectorError::UnableToParseUTF8(_) => 26,
CollectorError::InvalidPluginOutputError(_) => 27,
CollectorError::PluginExecutionError(_) => 28,
CollectorError::Other(_) => 29,
13 changes: 12 additions & 1 deletion src/collectors/pluginhandler.rs
Original file line number Diff line number Diff line change
@@ -17,6 +17,11 @@ use serde_json::{self, Value};
///
/// * `path: PathBuf` - The Path of the script to execute relative to the CWD. (If none, plugins
/// directory will be searched.)
///
/// # Returns
///
/// * `Ok(Value)` - Returns a JSON Value if the plugin script returns valid JSON.
/// * `Error` - If the execution of the plugin fails or it does not return a valid JSON.
pub fn execute(path: &str) -> Result<Value, Box<dyn Error>> {
println!("Attempting to execute plugin at path '{}'...", path);
if !fs::metadata(path)?.is_file() {
@@ -32,7 +37,13 @@ pub fn execute(path: &str) -> Result<Value, Box<dyn Error>> {

let stdout_str = String::from_utf8(output.stdout)?;

let json_output: Value = serde_json::from_str(&stdout_str)?;
let json_output: Value = match serde_json::from_str(&stdout_str) {
Ok(content) => content,
Err(e) => {
let err: CollectorError = CollectorError::InvalidPluginOutputError(e);
return Err(err);
}
};

Ok(json_output)
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -118,7 +118,7 @@ fn main() {
network_information,
custom_information: match args.plugin {
Some(path) => {
Some(pluginhandler::execute(path.as_ref())?)
todo!()
},
None => None,
}

0 comments on commit 3788bb4

Please sign in to comment.