2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2020-08-25 18:22:15 -04:00
|
|
|
use std::borrow::Cow;
|
2021-12-29 13:34:13 -05:00
|
|
|
use std::collections::HashSet;
|
2019-02-26 17:36:05 -05:00
|
|
|
use std::fmt;
|
2020-09-14 12:48:57 -04:00
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::fmt::Display;
|
|
|
|
use std::fmt::Formatter;
|
2020-05-28 13:36:43 -04:00
|
|
|
|
2023-01-05 14:29:50 -05:00
|
|
|
use anyhow::Error;
|
|
|
|
|
2023-04-08 11:00:02 -04:00
|
|
|
use crate::realm::JsRealm;
|
2023-01-05 14:29:50 -05:00
|
|
|
use crate::runtime::GetErrorClassFn;
|
|
|
|
use crate::runtime::JsRuntime;
|
|
|
|
use crate::source_map::apply_source_map;
|
|
|
|
use crate::source_map::get_source_line;
|
|
|
|
use crate::url::Url;
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
/// A generic wrapper that can encapsulate any concrete error type.
|
2021-11-16 09:02:28 -05:00
|
|
|
// TODO(ry) Deprecate AnyError and encourage deno_core::anyhow::Error instead.
|
2020-09-14 12:48:57 -04:00
|
|
|
pub type AnyError = anyhow::Error;
|
|
|
|
|
|
|
|
/// Creates a new error with a caller-specified error class name and message.
|
|
|
|
pub fn custom_error(
|
|
|
|
class: &'static str,
|
|
|
|
message: impl Into<Cow<'static, str>>,
|
2021-11-16 09:02:28 -05:00
|
|
|
) -> Error {
|
2020-09-14 12:48:57 -04:00
|
|
|
CustomError {
|
|
|
|
class,
|
|
|
|
message: message.into(),
|
2020-08-25 18:22:15 -04:00
|
|
|
}
|
2020-09-14 12:48:57 -04:00
|
|
|
.into()
|
|
|
|
}
|
2020-08-25 18:22:15 -04:00
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
pub fn generic_error(message: impl Into<Cow<'static, str>>) -> Error {
|
2020-09-14 12:48:57 -04:00
|
|
|
custom_error("Error", message)
|
|
|
|
}
|
2020-08-25 18:22:15 -04:00
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
pub fn type_error(message: impl Into<Cow<'static, str>>) -> Error {
|
2020-09-14 12:48:57 -04:00
|
|
|
custom_error("TypeError", message)
|
|
|
|
}
|
2020-08-25 18:22:15 -04:00
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
pub fn range_error(message: impl Into<Cow<'static, str>>) -> Error {
|
2021-06-05 17:10:07 -04:00
|
|
|
custom_error("RangeError", message)
|
|
|
|
}
|
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
pub fn invalid_hostname(hostname: &str) -> Error {
|
2023-01-27 10:43:16 -05:00
|
|
|
type_error(format!("Invalid hostname: '{hostname}'"))
|
2021-04-26 15:39:55 -04:00
|
|
|
}
|
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
pub fn uri_error(message: impl Into<Cow<'static, str>>) -> Error {
|
2020-09-14 12:48:57 -04:00
|
|
|
custom_error("URIError", message)
|
|
|
|
}
|
2020-08-25 18:22:15 -04:00
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
pub fn bad_resource(message: impl Into<Cow<'static, str>>) -> Error {
|
2020-09-14 12:48:57 -04:00
|
|
|
custom_error("BadResource", message)
|
|
|
|
}
|
2020-08-25 18:22:15 -04:00
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
pub fn bad_resource_id() -> Error {
|
2020-09-14 12:48:57 -04:00
|
|
|
custom_error("BadResource", "Bad resource ID")
|
|
|
|
}
|
2020-08-25 18:22:15 -04:00
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
pub fn not_supported() -> Error {
|
2020-10-08 15:40:49 -04:00
|
|
|
custom_error("NotSupported", "The operation is not supported")
|
2020-09-14 12:48:57 -04:00
|
|
|
}
|
2020-05-28 13:36:43 -04:00
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
pub fn resource_unavailable() -> Error {
|
2020-09-14 12:48:57 -04:00
|
|
|
custom_error(
|
|
|
|
"Busy",
|
|
|
|
"Resource is unavailable because it is in use by a promise",
|
|
|
|
)
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
/// A simple error type that lets the creator specify both the error message and
|
|
|
|
/// the error class name. This type is private; externally it only ever appears
|
2021-11-16 09:02:28 -05:00
|
|
|
/// wrapped in an `anyhow::Error`. To retrieve the error class name from a wrapped
|
2020-09-14 12:48:57 -04:00
|
|
|
/// `CustomError`, use the function `get_custom_error_class()`.
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct CustomError {
|
|
|
|
class: &'static str,
|
|
|
|
message: Cow<'static, str>,
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
impl Display for CustomError {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
f.write_str(&self.message)
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
impl std::error::Error for CustomError {}
|
2020-09-14 12:48:57 -04:00
|
|
|
|
|
|
|
/// If this error was crated with `custom_error()`, return the specified error
|
|
|
|
/// class name. In all other cases this function returns `None`.
|
2021-11-16 09:02:28 -05:00
|
|
|
pub fn get_custom_error_class(error: &Error) -> Option<&'static str> {
|
2020-09-14 12:48:57 -04:00
|
|
|
error.downcast_ref::<CustomError>().map(|e| e.class)
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2022-08-11 05:57:20 -04:00
|
|
|
pub fn to_v8_error<'a>(
|
|
|
|
scope: &mut v8::HandleScope<'a>,
|
|
|
|
get_class: GetErrorClassFn,
|
|
|
|
error: &Error,
|
|
|
|
) -> v8::Local<'a, v8::Value> {
|
2022-12-13 16:59:21 -05:00
|
|
|
let tc_scope = &mut v8::TryCatch::new(scope);
|
fix(core): Have custom errors be created in the right realm (#17050)
Although PR #16366 did not fully revert `deno_core`'s support for
realms, since `JsRealm` still existed after that, it did remove the
`ContextState` struct, originally introduced in #14734. This change made
`js_build_custom_error_cb`, among other properties, a per-runtime
callback, rather than per-realm, which cause a bug where errors thrown
from an op would always be constructed in the main realm, rather than in
the current one.
This change adds back `ContextState` to fix this bug, adds back the
`known_realms` field of `JsRuntimeState` (needed to be able to drop the
callback when snapshotting), and also relands #14750, which adds the
`js_realm_sync_ops` test for this bug that was removed in #16366.
2022-12-20 14:09:16 -05:00
|
|
|
let cb = JsRealm::state_from_scope(tc_scope)
|
2022-08-11 05:57:20 -04:00
|
|
|
.borrow()
|
|
|
|
.js_build_custom_error_cb
|
|
|
|
.clone()
|
|
|
|
.expect("Custom error builder must be set");
|
2022-12-13 16:59:21 -05:00
|
|
|
let cb = cb.open(tc_scope);
|
|
|
|
let this = v8::undefined(tc_scope).into();
|
|
|
|
let class = v8::String::new(tc_scope, get_class(error)).unwrap();
|
2023-01-27 10:43:16 -05:00
|
|
|
let message = v8::String::new(tc_scope, &format!("{error:#}")).unwrap();
|
2022-08-11 05:57:20 -04:00
|
|
|
let mut args = vec![class.into(), message.into()];
|
|
|
|
if let Some(code) = crate::error_codes::get_error_code(error) {
|
2022-12-13 16:59:21 -05:00
|
|
|
args.push(v8::String::new(tc_scope, code).unwrap().into());
|
|
|
|
}
|
|
|
|
let maybe_exception = cb.call(tc_scope, this, &args);
|
|
|
|
|
|
|
|
match maybe_exception {
|
|
|
|
Some(exception) => exception,
|
|
|
|
None => {
|
|
|
|
let mut msg =
|
|
|
|
"Custom error class must have a builder registered".to_string();
|
|
|
|
if tc_scope.has_caught() {
|
|
|
|
let e = tc_scope.exception().unwrap();
|
|
|
|
let js_error = JsError::from_v8_exception(tc_scope, e);
|
|
|
|
msg = format!("{}: {}", msg, js_error.exception_message);
|
|
|
|
}
|
|
|
|
panic!("{}", msg);
|
|
|
|
}
|
2022-08-11 05:57:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 15:44:29 -04:00
|
|
|
/// A `JsError` represents an exception coming from V8, with stack frames and
|
|
|
|
/// line numbers. The deno_cli crate defines another `JsError` type, which wraps
|
2020-04-10 12:26:52 -04:00
|
|
|
/// the one defined here, that adds source map support and colorful formatting.
|
2022-10-26 09:37:45 -04:00
|
|
|
/// When updating this struct, also update errors_are_equal_without_cause() in
|
|
|
|
/// fmt_error.rs.
|
2022-04-13 05:50:57 -04:00
|
|
|
#[derive(Debug, PartialEq, Clone, serde::Deserialize, serde::Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-09-06 15:44:29 -04:00
|
|
|
pub struct JsError {
|
2022-04-13 10:41:39 -04:00
|
|
|
pub name: Option<String>,
|
|
|
|
pub message: Option<String>,
|
2022-04-15 10:08:09 -04:00
|
|
|
pub stack: Option<String>,
|
2021-12-29 13:34:13 -05:00
|
|
|
pub cause: Option<Box<JsError>>,
|
2022-04-15 10:08:09 -04:00
|
|
|
pub exception_message: String,
|
2020-09-06 15:44:29 -04:00
|
|
|
pub frames: Vec<JsStackFrame>,
|
2022-04-15 10:08:09 -04:00
|
|
|
pub source_line: Option<String>,
|
|
|
|
pub source_line_frame_index: Option<usize>,
|
2022-04-16 10:12:26 -04:00
|
|
|
pub aggregated: Option<Vec<JsError>>,
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
#[derive(Debug, Eq, PartialEq, Clone, serde::Deserialize, serde::Serialize)]
|
2021-03-26 20:25:48 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-09-06 15:44:29 -04:00
|
|
|
pub struct JsStackFrame {
|
2020-04-13 10:54:16 -04:00
|
|
|
pub type_name: Option<String>,
|
|
|
|
pub function_name: Option<String>,
|
|
|
|
pub method_name: Option<String>,
|
|
|
|
pub file_name: Option<String>,
|
|
|
|
pub line_number: Option<i64>,
|
|
|
|
pub column_number: Option<i64>,
|
|
|
|
pub eval_origin: Option<String>,
|
2021-03-26 20:25:48 -04:00
|
|
|
// Warning! isToplevel has inconsistent snake<>camel case, "typo" originates in v8:
|
|
|
|
// https://source.chromium.org/search?q=isToplevel&sq=&ss=chromium%2Fchromium%2Fsrc:v8%2F
|
|
|
|
#[serde(rename = "isToplevel")]
|
2020-04-13 10:54:16 -04:00
|
|
|
pub is_top_level: Option<bool>,
|
2020-03-02 17:20:16 -05:00
|
|
|
pub is_eval: bool,
|
2020-04-13 10:54:16 -04:00
|
|
|
pub is_native: bool,
|
2020-03-02 17:20:16 -05:00
|
|
|
pub is_constructor: bool,
|
2020-04-10 12:26:52 -04:00
|
|
|
pub is_async: bool,
|
2020-04-13 10:54:16 -04:00
|
|
|
pub is_promise_all: bool,
|
|
|
|
pub promise_index: Option<i64>,
|
2020-04-10 12:26:52 -04:00
|
|
|
}
|
|
|
|
|
2020-09-22 13:01:30 -04:00
|
|
|
impl JsStackFrame {
|
|
|
|
pub fn from_location(
|
|
|
|
file_name: Option<String>,
|
|
|
|
line_number: Option<i64>,
|
|
|
|
column_number: Option<i64>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
type_name: None,
|
|
|
|
function_name: None,
|
|
|
|
method_name: None,
|
|
|
|
file_name,
|
|
|
|
line_number,
|
|
|
|
column_number,
|
|
|
|
eval_origin: None,
|
|
|
|
is_top_level: None,
|
|
|
|
is_eval: false,
|
|
|
|
is_native: false,
|
|
|
|
is_constructor: false,
|
|
|
|
is_async: false,
|
|
|
|
is_promise_all: false,
|
|
|
|
promise_index: None,
|
|
|
|
}
|
|
|
|
}
|
2023-04-12 07:45:36 -04:00
|
|
|
|
|
|
|
/// Gets the source mapped stack frame corresponding to the
|
|
|
|
/// (script_resource_name, line_number, column_number) from a v8 message.
|
|
|
|
/// For non-syntax errors, it should also correspond to the first stack frame.
|
|
|
|
pub fn from_v8_message<'a>(
|
|
|
|
scope: &'a mut v8::HandleScope,
|
|
|
|
message: v8::Local<'a, v8::Message>,
|
|
|
|
) -> Option<Self> {
|
|
|
|
let f = message.get_script_resource_name(scope)?;
|
|
|
|
let f: v8::Local<v8::String> = f.try_into().ok()?;
|
|
|
|
let f = f.to_rust_string_lossy(scope);
|
|
|
|
let l = message.get_line_number(scope)? as i64;
|
|
|
|
// V8's column numbers are 0-based, we want 1-based.
|
|
|
|
let c = message.get_start_column() as i64 + 1;
|
2023-05-31 10:19:06 -04:00
|
|
|
let state_rc = JsRuntime::state_from(scope);
|
2023-04-18 18:52:12 -04:00
|
|
|
let (getter, cache) = {
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
(
|
|
|
|
state.source_map_getter.clone(),
|
|
|
|
state.source_map_cache.clone(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(source_map_getter) = getter {
|
|
|
|
let mut cache = cache.borrow_mut();
|
|
|
|
let (f, l, c) =
|
|
|
|
apply_source_map(f, l, c, &mut cache, &**source_map_getter);
|
2023-04-12 07:45:36 -04:00
|
|
|
Some(JsStackFrame::from_location(Some(f), Some(l), Some(c)))
|
|
|
|
} else {
|
|
|
|
Some(JsStackFrame::from_location(Some(f), Some(l), Some(c)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn maybe_format_location(&self) -> Option<String> {
|
|
|
|
Some(format!(
|
|
|
|
"{}:{}:{}",
|
|
|
|
self.file_name.as_ref()?,
|
|
|
|
self.line_number?,
|
|
|
|
self.column_number?
|
|
|
|
))
|
|
|
|
}
|
2020-09-22 13:01:30 -04:00
|
|
|
}
|
|
|
|
|
2020-04-10 12:26:52 -04:00
|
|
|
fn get_property<'a>(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope<'a>,
|
2020-04-10 12:26:52 -04:00
|
|
|
object: v8::Local<v8::Object>,
|
|
|
|
key: &str,
|
|
|
|
) -> Option<v8::Local<'a, v8::Value>> {
|
|
|
|
let key = v8::String::new(scope, key).unwrap();
|
2020-06-20 07:18:08 -04:00
|
|
|
object.get(scope, key.into())
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2022-05-08 17:03:00 -04:00
|
|
|
#[derive(Default, serde::Deserialize)]
|
2021-12-15 13:22:36 -05:00
|
|
|
pub(crate) struct NativeJsError {
|
|
|
|
pub name: Option<String>,
|
|
|
|
pub message: Option<String>,
|
2021-03-26 20:25:48 -04:00
|
|
|
// Warning! .stack is special so handled by itself
|
|
|
|
// stack: Option<String>,
|
|
|
|
}
|
|
|
|
|
2020-09-06 15:44:29 -04:00
|
|
|
impl JsError {
|
2020-03-02 17:20:16 -05:00
|
|
|
pub fn from_v8_exception(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::HandleScope,
|
2020-03-02 17:20:16 -05:00
|
|
|
exception: v8::Local<v8::Value>,
|
2021-12-29 13:34:13 -05:00
|
|
|
) -> Self {
|
|
|
|
Self::inner_from_v8_exception(scope, exception, Default::default())
|
|
|
|
}
|
|
|
|
|
2022-11-28 17:07:23 -05:00
|
|
|
pub fn from_v8_message<'a>(
|
|
|
|
scope: &'a mut v8::HandleScope,
|
|
|
|
msg: v8::Local<'a, v8::Message>,
|
|
|
|
) -> Self {
|
|
|
|
// Create a new HandleScope because we're creating a lot of new local
|
|
|
|
// handles below.
|
|
|
|
let scope = &mut v8::HandleScope::new(scope);
|
|
|
|
|
|
|
|
let exception_message = msg.get(scope).to_rust_string_lossy(scope);
|
|
|
|
|
|
|
|
// Convert them into Vec<JsStackFrame>
|
|
|
|
let mut frames: Vec<JsStackFrame> = vec![];
|
|
|
|
let mut source_line = None;
|
|
|
|
let mut source_line_frame_index = None;
|
2023-04-12 07:45:36 -04:00
|
|
|
|
|
|
|
if let Some(stack_frame) = JsStackFrame::from_v8_message(scope, msg) {
|
|
|
|
frames = vec![stack_frame];
|
|
|
|
}
|
2022-11-28 17:07:23 -05:00
|
|
|
{
|
2023-05-31 10:19:06 -04:00
|
|
|
let state_rc = JsRuntime::state_from(scope);
|
2023-04-18 18:52:12 -04:00
|
|
|
let (getter, cache) = {
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
(
|
|
|
|
state.source_map_getter.clone(),
|
|
|
|
state.source_map_cache.clone(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if let Some(source_map_getter) = getter {
|
|
|
|
let mut cache = cache.borrow_mut();
|
2022-11-28 17:07:23 -05:00
|
|
|
for (i, frame) in frames.iter().enumerate() {
|
|
|
|
if let (Some(file_name), Some(line_number)) =
|
|
|
|
(&frame.file_name, frame.line_number)
|
|
|
|
{
|
2023-03-08 06:44:54 -05:00
|
|
|
if !file_name.trim_start_matches('[').starts_with("ext:") {
|
2022-11-28 17:07:23 -05:00
|
|
|
source_line = get_source_line(
|
|
|
|
file_name,
|
|
|
|
line_number,
|
2023-04-18 18:52:12 -04:00
|
|
|
&mut cache,
|
|
|
|
&**source_map_getter,
|
2022-11-28 17:07:23 -05:00
|
|
|
);
|
|
|
|
source_line_frame_index = Some(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Self {
|
|
|
|
name: None,
|
|
|
|
message: None,
|
|
|
|
exception_message,
|
|
|
|
cause: None,
|
|
|
|
source_line,
|
|
|
|
source_line_frame_index,
|
|
|
|
frames,
|
|
|
|
stack: None,
|
|
|
|
aggregated: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-29 13:34:13 -05:00
|
|
|
fn inner_from_v8_exception<'a>(
|
|
|
|
scope: &'a mut v8::HandleScope,
|
|
|
|
exception: v8::Local<'a, v8::Value>,
|
2022-10-16 15:16:46 -04:00
|
|
|
mut seen: HashSet<v8::Local<'a, v8::Object>>,
|
2020-03-02 17:20:16 -05:00
|
|
|
) -> Self {
|
|
|
|
// Create a new HandleScope because we're creating a lot of new local
|
|
|
|
// handles below.
|
2020-06-20 07:18:08 -04:00
|
|
|
let scope = &mut v8::HandleScope::new(scope);
|
2020-03-02 17:20:16 -05:00
|
|
|
|
|
|
|
let msg = v8::Exception::create_message(scope, exception);
|
|
|
|
|
2022-06-06 14:26:57 -04:00
|
|
|
let mut exception_message = None;
|
2023-01-14 22:39:00 -05:00
|
|
|
let context_state_rc = JsRealm::state_from_scope(scope);
|
2022-07-30 10:09:42 -04:00
|
|
|
|
|
|
|
let js_format_exception_cb =
|
2023-01-14 22:39:00 -05:00
|
|
|
context_state_rc.borrow().js_format_exception_cb.clone();
|
2022-07-30 10:09:42 -04:00
|
|
|
if let Some(format_exception_cb) = js_format_exception_cb {
|
|
|
|
let format_exception_cb = format_exception_cb.open(scope);
|
|
|
|
let this = v8::undefined(scope).into();
|
|
|
|
let formatted = format_exception_cb.call(scope, this, &[exception]);
|
|
|
|
if let Some(formatted) = formatted {
|
|
|
|
if formatted.is_string() {
|
|
|
|
exception_message = Some(formatted.to_rust_string_lossy(scope));
|
2022-06-06 14:26:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 05:50:57 -04:00
|
|
|
if is_instance_of_error(scope, exception) {
|
2022-12-20 19:18:49 -05:00
|
|
|
let v8_exception = exception;
|
2022-04-13 05:50:57 -04:00
|
|
|
// The exception is a JS Error object.
|
|
|
|
let exception: v8::Local<v8::Object> = exception.try_into().unwrap();
|
|
|
|
let cause = get_property(scope, exception, "cause");
|
|
|
|
let e: NativeJsError =
|
2022-05-08 17:03:00 -04:00
|
|
|
serde_v8::from_v8(scope, exception.into()).unwrap_or_default();
|
2022-04-13 05:50:57 -04:00
|
|
|
// Get the message by formatting error.name and error.message.
|
2022-04-13 10:41:39 -04:00
|
|
|
let name = e.name.clone().unwrap_or_else(|| "Error".to_string());
|
2022-11-17 20:59:10 -05:00
|
|
|
let message_prop = e.message.clone().unwrap_or_default();
|
2022-06-06 14:26:57 -04:00
|
|
|
let exception_message = exception_message.unwrap_or_else(|| {
|
|
|
|
if !name.is_empty() && !message_prop.is_empty() {
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("Uncaught {name}: {message_prop}")
|
2022-06-06 14:26:57 -04:00
|
|
|
} else if !name.is_empty() {
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("Uncaught {name}")
|
2022-06-06 14:26:57 -04:00
|
|
|
} else if !message_prop.is_empty() {
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("Uncaught {message_prop}")
|
2022-06-06 14:26:57 -04:00
|
|
|
} else {
|
|
|
|
"Uncaught".to_string()
|
|
|
|
}
|
|
|
|
});
|
2022-04-13 05:50:57 -04:00
|
|
|
let cause = cause.and_then(|cause| {
|
2022-10-16 15:16:46 -04:00
|
|
|
if cause.is_undefined() || seen.contains(&exception) {
|
2022-04-13 05:50:57 -04:00
|
|
|
None
|
|
|
|
} else {
|
2022-10-16 15:16:46 -04:00
|
|
|
seen.insert(exception);
|
2022-04-13 05:50:57 -04:00
|
|
|
Some(Box::new(JsError::inner_from_v8_exception(
|
|
|
|
scope, cause, seen,
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Access error.stack to ensure that prepareStackTrace() has been called.
|
|
|
|
// This should populate error.__callSiteEvals.
|
|
|
|
let stack = get_property(scope, exception, "stack");
|
|
|
|
let stack: Option<v8::Local<v8::String>> =
|
|
|
|
stack.and_then(|s| s.try_into().ok());
|
|
|
|
let stack = stack.map(|s| s.to_rust_string_lossy(scope));
|
|
|
|
|
|
|
|
// Read an array of structured frames from error.__callSiteEvals.
|
|
|
|
let frames_v8 = get_property(scope, exception, "__callSiteEvals");
|
|
|
|
// Ignore non-array values
|
|
|
|
let frames_v8: Option<v8::Local<v8::Array>> =
|
|
|
|
frames_v8.and_then(|a| a.try_into().ok());
|
|
|
|
|
|
|
|
// Convert them into Vec<JsStackFrame>
|
2022-04-15 10:08:09 -04:00
|
|
|
let mut frames: Vec<JsStackFrame> = match frames_v8 {
|
2022-04-13 05:50:57 -04:00
|
|
|
Some(frames_v8) => serde_v8::from_v8(scope, frames_v8.into()).unwrap(),
|
|
|
|
None => vec![],
|
|
|
|
};
|
2022-04-15 10:08:09 -04:00
|
|
|
let mut source_line = None;
|
|
|
|
let mut source_line_frame_index = None;
|
2023-04-12 07:45:36 -04:00
|
|
|
|
|
|
|
// When the stack frame array is empty, but the source location given by
|
|
|
|
// (script_resource_name, line_number, start_column + 1) exists, this is
|
|
|
|
// likely a syntax error. For the sake of formatting we treat it like it
|
|
|
|
// was given as a single stack frame.
|
|
|
|
if frames.is_empty() {
|
|
|
|
if let Some(stack_frame) = JsStackFrame::from_v8_message(scope, msg) {
|
|
|
|
frames = vec![stack_frame];
|
|
|
|
}
|
|
|
|
}
|
2022-06-20 08:42:20 -04:00
|
|
|
{
|
2023-05-31 10:19:06 -04:00
|
|
|
let state_rc = JsRuntime::state_from(scope);
|
2023-04-18 18:52:12 -04:00
|
|
|
let (getter, cache) = {
|
|
|
|
let state = state_rc.borrow();
|
|
|
|
(
|
|
|
|
state.source_map_getter.clone(),
|
|
|
|
state.source_map_cache.clone(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if let Some(source_map_getter) = getter {
|
|
|
|
let mut cache = cache.borrow_mut();
|
|
|
|
|
2022-06-20 08:42:20 -04:00
|
|
|
for (i, frame) in frames.iter().enumerate() {
|
|
|
|
if let (Some(file_name), Some(line_number)) =
|
|
|
|
(&frame.file_name, frame.line_number)
|
|
|
|
{
|
2023-03-08 06:44:54 -05:00
|
|
|
if !file_name.trim_start_matches('[').starts_with("ext:") {
|
2022-06-20 08:42:20 -04:00
|
|
|
source_line = get_source_line(
|
|
|
|
file_name,
|
|
|
|
line_number,
|
2023-04-18 18:52:12 -04:00
|
|
|
&mut cache,
|
|
|
|
&**source_map_getter,
|
2022-06-20 08:42:20 -04:00
|
|
|
);
|
|
|
|
source_line_frame_index = Some(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let Some(frame) = frames.first() {
|
|
|
|
if let Some(file_name) = &frame.file_name {
|
2023-03-08 06:44:54 -05:00
|
|
|
if !file_name.trim_start_matches('[').starts_with("ext:") {
|
2022-06-20 08:42:20 -04:00
|
|
|
source_line = msg
|
|
|
|
.get_source_line(scope)
|
|
|
|
.map(|v| v.to_rust_string_lossy(scope));
|
|
|
|
source_line_frame_index = Some(0);
|
|
|
|
}
|
2022-04-15 10:08:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 10:12:26 -04:00
|
|
|
let mut aggregated: Option<Vec<JsError>> = None;
|
2022-12-20 19:18:49 -05:00
|
|
|
if is_aggregate_error(scope, v8_exception) {
|
|
|
|
// Read an array of stored errors, this is only defined for `AggregateError`
|
|
|
|
let aggregated_errors = get_property(scope, exception, "errors");
|
|
|
|
let aggregated_errors: Option<v8::Local<v8::Array>> =
|
|
|
|
aggregated_errors.and_then(|a| a.try_into().ok());
|
|
|
|
|
|
|
|
if let Some(errors) = aggregated_errors {
|
|
|
|
if errors.length() > 0 {
|
|
|
|
let mut agg = vec![];
|
|
|
|
for i in 0..errors.length() {
|
|
|
|
let error = errors.get_index(scope, i).unwrap();
|
|
|
|
let js_error = Self::from_v8_exception(scope, error);
|
|
|
|
agg.push(js_error);
|
|
|
|
}
|
|
|
|
aggregated = Some(agg);
|
2022-04-16 10:12:26 -04:00
|
|
|
}
|
|
|
|
}
|
2022-12-20 19:18:49 -05:00
|
|
|
};
|
2022-04-16 10:12:26 -04:00
|
|
|
|
2022-04-13 05:50:57 -04:00
|
|
|
Self {
|
2022-04-13 10:41:39 -04:00
|
|
|
name: e.name,
|
|
|
|
message: e.message,
|
|
|
|
exception_message,
|
2022-04-13 05:50:57 -04:00
|
|
|
cause,
|
2022-04-15 10:08:09 -04:00
|
|
|
source_line,
|
|
|
|
source_line_frame_index,
|
2022-04-13 05:50:57 -04:00
|
|
|
frames,
|
|
|
|
stack,
|
2022-04-16 10:12:26 -04:00
|
|
|
aggregated,
|
2022-04-13 05:50:57 -04:00
|
|
|
}
|
|
|
|
} else {
|
2022-06-06 14:26:57 -04:00
|
|
|
let exception_message = exception_message
|
|
|
|
.unwrap_or_else(|| msg.get(scope).to_rust_string_lossy(scope));
|
2022-04-13 05:50:57 -04:00
|
|
|
// The exception is not a JS Error object.
|
|
|
|
// Get the message given by V8::Exception::create_message(), and provide
|
|
|
|
// empty frames.
|
|
|
|
Self {
|
2022-04-13 10:41:39 -04:00
|
|
|
name: None,
|
|
|
|
message: None,
|
2022-06-06 14:26:57 -04:00
|
|
|
exception_message,
|
2022-04-13 05:50:57 -04:00
|
|
|
cause: None,
|
|
|
|
source_line: None,
|
2022-04-15 10:08:09 -04:00
|
|
|
source_line_frame_index: None,
|
2022-04-13 05:50:57 -04:00
|
|
|
frames: vec![],
|
|
|
|
stack: None,
|
2022-04-16 10:12:26 -04:00
|
|
|
aggregated: None,
|
2022-04-13 05:50:57 -04:00
|
|
|
}
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
impl std::error::Error for JsError {}
|
2019-07-10 18:53:48 -04:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
impl Display for JsError {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2020-09-22 17:30:03 -04:00
|
|
|
if let Some(stack) = &self.stack {
|
|
|
|
let stack_lines = stack.lines();
|
|
|
|
if stack_lines.count() > 1 {
|
2023-01-27 10:43:16 -05:00
|
|
|
return write!(f, "{stack}");
|
2020-09-22 17:30:03 -04:00
|
|
|
}
|
|
|
|
}
|
2022-04-13 10:41:39 -04:00
|
|
|
write!(f, "{}", self.exception_message)?;
|
2023-04-12 07:45:36 -04:00
|
|
|
let location = self.frames.first().and_then(|f| f.maybe_format_location());
|
|
|
|
if let Some(location) = location {
|
|
|
|
write!(f, "\n at {location}")?;
|
2019-07-10 18:53:48 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 13:36:43 -04:00
|
|
|
|
2022-04-26 09:28:42 -04:00
|
|
|
// TODO(piscisaureus): rusty_v8 should implement the Error trait on
|
|
|
|
// values of type v8::Global<T>.
|
|
|
|
pub(crate) fn to_v8_type_error(
|
|
|
|
scope: &mut v8::HandleScope,
|
2021-11-16 09:02:28 -05:00
|
|
|
err: Error,
|
2022-04-26 09:28:42 -04:00
|
|
|
) -> v8::Global<v8::Value> {
|
2022-12-22 00:26:28 -05:00
|
|
|
let err_string = err.to_string();
|
|
|
|
let error_chain = err
|
|
|
|
.chain()
|
|
|
|
.skip(1)
|
|
|
|
.filter(|e| e.to_string() != err_string)
|
|
|
|
.map(|e| e.to_string())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let message = if !error_chain.is_empty() {
|
|
|
|
format!(
|
|
|
|
"{}\n Caused by:\n {}",
|
|
|
|
err_string,
|
|
|
|
error_chain.join("\n ")
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
err_string
|
|
|
|
};
|
|
|
|
|
2022-04-26 09:28:42 -04:00
|
|
|
let message = v8::String::new(scope, &message).unwrap();
|
|
|
|
let exception = v8::Exception::type_error(scope, message);
|
|
|
|
v8::Global::new(scope, exception)
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
|
|
|
|
2021-09-06 16:59:20 -04:00
|
|
|
/// Implements `value instanceof primordials.Error` in JS. Similar to
|
|
|
|
/// `Value::is_native_error()` but more closely matches the semantics
|
|
|
|
/// of `instanceof`. `Value::is_native_error()` also checks for static class
|
|
|
|
/// inheritance rather than just scanning the prototype chain, which doesn't
|
|
|
|
/// work with our WebIDL implementation of `DOMException`.
|
2023-01-27 10:43:16 -05:00
|
|
|
pub(crate) fn is_instance_of_error(
|
|
|
|
scope: &mut v8::HandleScope,
|
2021-09-06 16:59:20 -04:00
|
|
|
value: v8::Local<v8::Value>,
|
|
|
|
) -> bool {
|
|
|
|
if !value.is_object() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let message = v8::String::empty(scope);
|
|
|
|
let error_prototype = v8::Exception::error(scope, message)
|
|
|
|
.to_object(scope)
|
|
|
|
.unwrap()
|
|
|
|
.get_prototype(scope)
|
|
|
|
.unwrap();
|
|
|
|
let mut maybe_prototype =
|
|
|
|
value.to_object(scope).unwrap().get_prototype(scope);
|
|
|
|
while let Some(prototype) = maybe_prototype {
|
2022-04-13 05:50:57 -04:00
|
|
|
if !prototype.is_object() {
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-06 16:59:20 -04:00
|
|
|
if prototype.strict_equals(error_prototype) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
maybe_prototype = prototype
|
|
|
|
.to_object(scope)
|
|
|
|
.and_then(|o| o.get_prototype(scope));
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2022-12-20 19:18:49 -05:00
|
|
|
/// Implements `value instanceof primordials.AggregateError` in JS,
|
|
|
|
/// by walking the prototype chain, and comparing each links constructor `name` property.
|
|
|
|
///
|
|
|
|
/// NOTE: There is currently no way to detect `AggregateError` via `rusty_v8`,
|
|
|
|
/// as v8 itself doesn't expose `v8__Exception__AggregateError`,
|
|
|
|
/// and we cannot create bindings for it. This forces us to rely on `name` inference.
|
2023-01-27 10:43:16 -05:00
|
|
|
pub(crate) fn is_aggregate_error(
|
|
|
|
scope: &mut v8::HandleScope,
|
2022-12-20 19:18:49 -05:00
|
|
|
value: v8::Local<v8::Value>,
|
|
|
|
) -> bool {
|
|
|
|
let mut maybe_prototype = Some(value);
|
|
|
|
while let Some(prototype) = maybe_prototype {
|
|
|
|
if !prototype.is_object() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let prototype = prototype.to_object(scope).unwrap();
|
|
|
|
let prototype_name = match get_property(scope, prototype, "constructor") {
|
|
|
|
Some(constructor) => {
|
|
|
|
let ctor = constructor.to_object(scope).unwrap();
|
|
|
|
get_property(scope, ctor, "name").map(|v| v.to_rust_string_lossy(scope))
|
|
|
|
}
|
|
|
|
None => return false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if prototype_name == Some(String::from("AggregateError")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
maybe_prototype = prototype.get_prototype(scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2022-05-03 13:45:57 -04:00
|
|
|
const DATA_URL_ABBREV_THRESHOLD: usize = 150;
|
|
|
|
|
|
|
|
pub fn format_file_name(file_name: &str) -> String {
|
|
|
|
abbrev_file_name(file_name).unwrap_or_else(|| file_name.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn abbrev_file_name(file_name: &str) -> Option<String> {
|
|
|
|
if file_name.len() <= DATA_URL_ABBREV_THRESHOLD {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let url = Url::parse(file_name).ok()?;
|
|
|
|
if url.scheme() != "data" {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let (head, tail) = url.path().split_once(',')?;
|
|
|
|
let len = tail.len();
|
|
|
|
let start = tail.get(0..20)?;
|
|
|
|
let end = tail.get(len - 20..)?;
|
|
|
|
Some(format!("{}:{},{}......{}", url.scheme(), head, start, end))
|
|
|
|
}
|
|
|
|
|
2020-09-05 20:34:02 -04:00
|
|
|
#[cfg(test)]
|
2020-08-25 18:22:15 -04:00
|
|
|
mod tests {
|
2020-09-05 20:34:02 -04:00
|
|
|
use super::*;
|
|
|
|
|
2020-08-25 18:22:15 -04:00
|
|
|
#[test]
|
|
|
|
fn test_bad_resource() {
|
2020-09-14 12:48:57 -04:00
|
|
|
let err = bad_resource("Resource has been closed");
|
2020-08-25 18:22:15 -04:00
|
|
|
assert_eq!(err.to_string(), "Resource has been closed");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bad_resource_id() {
|
2020-09-14 12:48:57 -04:00
|
|
|
let err = bad_resource_id();
|
2020-08-25 18:22:15 -04:00
|
|
|
assert_eq!(err.to_string(), "Bad resource ID");
|
|
|
|
}
|
|
|
|
}
|