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
|
// The promise fulfilled successfully
|
||||||
Ok(v) => Ok(v),
|
Ok(v) => Ok(v),
|
||||||
// There was an error awaiting the promise
|
// There was an error awaiting the promise
|
||||||
Err(e) => Err(Error::InvalidScript {
|
Err(e) => Err(Error::from(e)),
|
||||||
message: e.to_string(),
|
|
||||||
}),
|
|
||||||
},
|
},
|
||||||
// There was an error running the script
|
// There was an error running the script
|
||||||
Err(e) => Err(Error::InvalidScript {
|
Err(e) => Err(Error::from(e)),
|
||||||
message: e.to_string(),
|
|
||||||
}),
|
|
||||||
};
|
};
|
||||||
// Return the result
|
// Return the result
|
||||||
res
|
res
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
pub use main::run;
|
pub use main::run;
|
||||||
|
|
||||||
mod classes;
|
mod classes;
|
||||||
|
mod error;
|
||||||
mod executor;
|
mod executor;
|
||||||
mod from;
|
mod from;
|
||||||
mod into;
|
mod into;
|
||||||
|
|
Loading…
Reference in a new issue