diff --git a/lib/src/fnc/script/error.rs b/lib/src/fnc/script/error.rs new file mode 100644 index 00000000..6ed09c67 --- /dev/null +++ b/lib/src/fnc/script/error.rs @@ -0,0 +1,36 @@ +use crate::err::Error; + +impl From for Error { + fn from(e: js::Error) -> Error { + match e { + js::Error::Exception { + message, + stack, + file, + line, + } => Error::InvalidScript { + message: format!( + "An exception occured{}: {}{}", + match file.is_empty() { + false => format!(" at {}:{}", file, line), + true => String::default(), + }, + match message.is_empty() { + false => message, + true => String::default(), + }, + match stack.is_empty() { + false => format!("\n{}", stack), + true => String::default(), + } + ), + }, + js::Error::Unknown => Error::InvalidScript { + message: "An unknown error occured".to_string(), + }, + _ => Error::InvalidScript { + message: e.to_string(), + }, + } + } +} diff --git a/lib/src/fnc/script/main.rs b/lib/src/fnc/script/main.rs index aa72b5a2..d2ceeb94 100644 --- a/lib/src/fnc/script/main.rs +++ b/lib/src/fnc/script/main.rs @@ -53,14 +53,10 @@ pub async fn run( // The promise fulfilled successfully Ok(v) => Ok(v), // There was an error awaiting the promise - Err(e) => Err(Error::InvalidScript { - message: e.to_string(), - }), + Err(e) => Err(Error::from(e)), }, // There was an error running the script - Err(e) => Err(Error::InvalidScript { - message: e.to_string(), - }), + Err(e) => Err(Error::from(e)), }; // Return the result res diff --git a/lib/src/fnc/script/mod.rs b/lib/src/fnc/script/mod.rs index df8848eb..4b0dcc9c 100644 --- a/lib/src/fnc/script/mod.rs +++ b/lib/src/fnc/script/mod.rs @@ -3,6 +3,7 @@ pub use main::run; mod classes; +mod error; mod executor; mod from; mod into;