Skip to content

Commit

Permalink
Change Option<NaslValue> to ReturnBehavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Nov 11, 2024
1 parent adbf987 commit b161913
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
20 changes: 13 additions & 7 deletions rust/src/nasl/utils/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ use crate::storage::StorageError;
pub struct FnError {
#[source]
pub kind: FnErrorKind,
return_value: Option<NaslValue>,
return_behavior: ReturnBehavior,
retryable: bool,
}

#[derive(Debug)]
pub enum ReturnBehavior {
ExitScript,
ReturnValue(NaslValue),
}

impl FnError {
pub fn return_value(&self) -> &Option<NaslValue> {
&self.return_value
pub fn return_behavior(&self) -> &ReturnBehavior {
&self.return_behavior
}

pub fn retryable(&self) -> bool {
Expand All @@ -30,7 +36,7 @@ impl FnError {
fn from_kind(kind: FnErrorKind) -> FnError {
Self {
kind,
return_value: None,
return_behavior: ReturnBehavior::ExitScript,
retryable: false,
}
}
Expand All @@ -53,7 +59,7 @@ impl From<BuiltinError> for FnError {
FnError {
kind: FnErrorKind::Builtin(kind),
retryable: false,
return_value: Some(NaslValue::Null),
return_behavior: ReturnBehavior::ReturnValue(NaslValue::Null),
}
}
}
Expand All @@ -64,7 +70,7 @@ impl From<InternalError> for FnError {
Self {
kind: FnErrorKind::Internal(kind),
retryable,
return_value: None,
return_behavior: ReturnBehavior::ExitScript,
}
}
}
Expand Down Expand Up @@ -175,7 +181,7 @@ impl<T: Into<NaslValue>, E: Into<FnError>> WithErrorInfo<ReturnValue<T>> for E {
fn with(self, val: ReturnValue<T>) -> Self::Error {
let mut e = self.into();
let val = val.0.into();
e.return_value = Some(val);
e.return_behavior = ReturnBehavior::ReturnValue(val);
e
}
}
Expand Down
11 changes: 8 additions & 3 deletions rust/src/scannerctl/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
};

use futures::StreamExt;
use scannerlib::nasl::interpreter::CodeInterpreter;
use scannerlib::nasl::{interpreter::CodeInterpreter, utils::error::ReturnBehavior};
use scannerlib::nasl::{
interpreter::InterpretErrorKind,
prelude::*,
Expand Down Expand Up @@ -140,8 +140,13 @@ where
Ok(x) => x,
Err(e) => {
if let InterpretErrorKind::FunctionCallError(ref fe) = e.kind {
tracing::warn!("{}", e.to_string());
fe.kind.return_value().clone().unwrap_or_default()
match fe.kind.return_behavior() {
ReturnBehavior::ExitScript => return Err(e.into()),
ReturnBehavior::ReturnValue(val) => {
tracing::warn!("{}", e.to_string());
val.clone()
}
}
} else {
return Err(e.into());
}
Expand Down

0 comments on commit b161913

Please sign in to comment.