use crate::runtime::CompiledFunction;
pub fn map_v8_exception(
scope: &mut deno_core::v8::HandleScope,
exception: deno_core::v8::Local<deno_core::v8::Value>,
) -> String {
let exception_obj = if exception.is_object() {
exception.to_object(scope)
} else {
None
};
if let Some(obj) = &exception_obj {
let stack_key = deno_core::v8::String::new(scope, "stack").unwrap();
if let Some(stack_val) = obj.get(scope, stack_key.into()) {
if stack_val.is_string() {
let stack_str = stack_val.to_rust_string_lossy(scope);
if !stack_str.is_empty() {
return stack_str;
}
}
}
}
if let Some(obj) = &exception_obj {
let msg_key = deno_core::v8::String::new(scope, "message").unwrap();
if let Some(msg_val) = obj.get(scope, msg_key.into()) {
if msg_val.is_string() {
let msg_str = msg_val.to_rust_string_lossy(scope);
if !msg_str.is_empty() {
return msg_str;
}
}
}
}
exception.to_rust_string_lossy(scope)
}
pub fn format_ts_error(
scope: &mut deno_core::v8::HandleScope,
exception: deno_core::v8::Local<deno_core::v8::Value>,
func: &CompiledFunction,
) -> String {
let raw = map_v8_exception(scope, exception);
format!("TypeScript error in '{}': {}", func.name, raw)
}
pub fn format_ts_error_str(func: &CompiledFunction, msg: &str) -> String {
format!("TypeScript error in '{}': {}", func.name, msg)
}