Add ability to handle quickjs errors ourselves
This commit is contained in:
parent
5c0a0ca556
commit
e9476b9f85
3 changed files with 39 additions and 6 deletions
36
lib/src/fnc/script/error.rs
Normal file
36
lib/src/fnc/script/error.rs
Normal file
|
@ -0,0 +1,36 @@
|
|||
use crate::err::Error;
|
||||
|
||||
impl From<js::Error> 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(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
pub use main::run;
|
||||
|
||||
mod classes;
|
||||
mod error;
|
||||
mod executor;
|
||||
mod from;
|
||||
mod into;
|
||||
|
|
Loading…
Reference in a new issue