Add ability to handle quickjs errors ourselves

This commit is contained in:
Tobie Morgan Hitchcock 2022-07-22 15:56:09 +01:00
parent 5c0a0ca556
commit e9476b9f85
3 changed files with 39 additions and 6 deletions

View 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(),
},
}
}
}

View file

@ -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

View file

@ -3,6 +3,7 @@
pub use main::run;
mod classes;
mod error;
mod executor;
mod from;
mod into;