Skip to content

Commit

Permalink
Change: better error handling.
Browse files Browse the repository at this point in the history
Embeed the RedisError instead of wrapping it. Also defined more DbError types.
  • Loading branch information
jjnicola committed Nov 23, 2022
1 parent dc7040d commit 18aaf2a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
36 changes: 23 additions & 13 deletions rust/nvtcache/src/dberror.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
use redis::*;
use std::error;
use std::fmt;

pub type Result<T> = std::result::Result<T, DbError>;

#[derive(Debug)]
pub enum DbError {
RedisErr(RedisError),
RedisErr {
source: String,
detail: String,
kind: String,
},
MaxDbErr(String),
NoAvailDbErr(String),
CustomErr(String),
}

impl fmt::Display for DbError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &*self {
DbError::RedisErr(..) => write!(f, "Redis Error"),
DbError::RedisErr {
source,
detail,
kind,
} => write!(f, "Redis Error. {kind}: {source}. {detail}"),
DbError::MaxDbErr(e) => write!(f, "Error: {}", e),
DbError::NoAvailDbErr(e) => write!(f, "Error: {}", e),
DbError::CustomErr(e) => write!(f, "Error: {}", e),
}
}
}

impl error::Error for DbError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
DbError::RedisErr(ref e) => Some(e),
DbError::CustomErr(_) => None,
}
}
}

impl From<RedisError> for DbError {
fn from(err: RedisError) -> DbError {
DbError::RedisErr(err)
let mut detail = "";
if let Some(d) = err.detail() {
detail = d;
}
DbError::RedisErr {
source: err.to_string(),
detail: detail.to_string(),
kind: err.category().to_string(),
}
}
}
10 changes: 5 additions & 5 deletions rust/nvtcache/src/redisconnector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ impl RedisCtx {
return Ok(self.maxdb);
}
Err(_) => {
return Err(DbError::CustomErr(String::from(
"Not possible to select a free database.",
return Err(DbError::MaxDbErr(String::from(
"Not possible to get the Max. database index.",
)))
}
}
Expand All @@ -109,14 +109,14 @@ impl RedisCtx {
Ok(db)
}

fn set_namespace(&mut self, db_index: u32) -> Result<String> {
fn set_namespace(&mut self, db_index: u32) -> Result<()> {
Cmd::new()
.arg("SELECT")
.arg(db_index.to_string())
.query(&mut self.kb)?;

self.db = db_index;
return Ok(String::from("ok"));
Ok(())
}

fn try_database(&mut self, dbi: u32) -> Result<u32> {
Expand All @@ -141,7 +141,7 @@ impl RedisCtx {
self.set_namespace(selected_db)?;
return Ok(self.db);
}
return Err(DbError::CustomErr(String::from(
return Err(DbError::NoAvailDbErr(String::from(
"Not possible to select a free db",
)));
}
Expand Down
5 changes: 0 additions & 5 deletions rust/nvtcache/tests/nvtcache_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use ::nvtcache::dberror::Result;
use ::nvtcache::nvt::Nvt;
use ::nvtcache::redisconnector::KbNvtPos;
use nvtcache::nvtcache;
use std::error::Error;

//test
#[cfg(test)]
Expand All @@ -26,10 +25,6 @@ mod test {
Ok(nc) => nvtcache = nc,
Err(e) => {
println!("{}", e);
if let Some(source) = e.source() {
println!("{}", source);
}

panic!("Error")
}
}
Expand Down

0 comments on commit 18aaf2a

Please sign in to comment.