Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): decode JsStackFrames using serde_v8 #9902

Merged
merged 5 commits into from
Mar 27, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
core: handle NativeJsError.stack as before since it's "special"
Its v8::Object::get returns None sometimes (not undefined), which isn't expected.

Might be able to handle this better in the future, but for now it's better to panic than to swallow the value
  • Loading branch information
AaronO committed Mar 26, 2021
commit ad8915a9b78bbfa50b6de4bf1a35ca112a57dca9
20 changes: 12 additions & 8 deletions core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ fn get_property<'a>(
struct NativeJsError {
name: Option<String>,
message: Option<String>,
stack: Option<String>,
// Warning! .stack is special so handled by itself
// stack: Option<String>,
}

impl JsError {
Expand All @@ -181,12 +182,8 @@ impl JsError {
let exception: v8::Local<v8::Object> =
exception.clone().try_into().unwrap();

// Decode native JS error, internally this should access error.stack
// ensuring that prepareStackTrace() has been called.
// Which in turn should populate error.__callSiteEvals.
let e: NativeJsError =
serde_v8::from_v8(scope, exception.into()).unwrap();

// Get the message by formatting error.name and error.message.
let name = e.name.unwrap_or_else(|| "Error".to_string());
let message_prop = e.message.unwrap_or_else(|| "".to_string());
Expand All @@ -200,18 +197,25 @@ impl JsError {
"Uncaught".to_string()
};

// 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());
frames_v8.and_then(|a| a.try_into().ok());

// Convert them into Vec<JSStack>
// Convert them into Vec<JsStackFrame>
let frames: Vec<JsStackFrame> = match frames_v8 {
Some(frames_v8) => serde_v8::from_v8(scope, frames_v8.into()).unwrap(),
None => vec![],
};
(message, frames, e.stack)
(message, frames, stack)
} else {
// The exception is not a JS Error object.
// Get the message given by V8::Exception::create_message(), and provide
Expand Down