2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2020-03-02 17:20:16 -05:00
|
|
|
use rusty_v8 as v8;
|
2020-05-28 13:36:43 -04:00
|
|
|
use std::any::Any;
|
|
|
|
use std::any::TypeId;
|
2020-08-25 18:22:15 -04:00
|
|
|
use std::borrow::Cow;
|
2020-03-02 17:20:16 -05:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::convert::TryInto;
|
2019-07-10 18:53:48 -04:00
|
|
|
use std::error::Error;
|
2019-02-26 17:36:05 -05:00
|
|
|
use std::fmt;
|
2020-08-25 18:22:15 -04:00
|
|
|
use std::io;
|
2020-05-28 13:36:43 -04:00
|
|
|
|
|
|
|
// The Send and Sync traits are required because deno is multithreaded and we
|
|
|
|
// need to be able to handle errors across threads.
|
|
|
|
pub trait AnyError: Any + Error + Send + Sync + 'static {}
|
|
|
|
impl<T> AnyError for T where T: Any + Error + Send + Sync + Sized + 'static {}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2020-08-25 18:22:15 -04:00
|
|
|
pub enum ErrBox {
|
|
|
|
Simple {
|
|
|
|
class: &'static str,
|
|
|
|
message: Cow<'static, str>,
|
|
|
|
},
|
|
|
|
Boxed(Box<dyn AnyError>),
|
|
|
|
}
|
2020-05-28 13:36:43 -04:00
|
|
|
|
2020-08-25 18:22:15 -04:00
|
|
|
impl ErrBox {
|
|
|
|
pub fn new(
|
|
|
|
class: &'static str,
|
|
|
|
message: impl Into<Cow<'static, str>>,
|
|
|
|
) -> Self {
|
|
|
|
Self::Simple {
|
|
|
|
class,
|
|
|
|
message: message.into(),
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 18:22:15 -04:00
|
|
|
pub fn bad_resource(message: impl Into<Cow<'static, str>>) -> Self {
|
|
|
|
Self::new("BadResource", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bad_resource_id() -> Self {
|
|
|
|
Self::new("BadResource", "Bad resource ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn error(message: impl Into<Cow<'static, str>>) -> Self {
|
|
|
|
Self::new("Error", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn not_supported() -> Self {
|
|
|
|
Self::new("NotSupported", "The operation is supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resource_unavailable() -> Self {
|
|
|
|
Self::new(
|
|
|
|
"Busy",
|
|
|
|
"Resource is unavailable because it is in use by a promise",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn type_error(message: impl Into<Cow<'static, str>>) -> Self {
|
|
|
|
Self::new("TypeError", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_os_error() -> Self {
|
|
|
|
Self::from(io::Error::last_os_error())
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:36:43 -04:00
|
|
|
pub fn downcast<T: AnyError>(self) -> Result<T, Self> {
|
2020-08-25 18:22:15 -04:00
|
|
|
match self {
|
|
|
|
Self::Boxed(error) if Any::type_id(&*error) == TypeId::of::<T>() => {
|
|
|
|
let error = Box::into_raw(error) as *mut T;
|
|
|
|
let error = unsafe { Box::from_raw(error) };
|
|
|
|
Ok(*error)
|
|
|
|
}
|
|
|
|
other => Err(other),
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 18:22:15 -04:00
|
|
|
pub fn downcast_ref<T: AnyError>(&self) -> Option<&T> {
|
|
|
|
match self {
|
|
|
|
Self::Boxed(error) if Any::type_id(&**error) == TypeId::of::<T>() => {
|
|
|
|
let error = &**error as *const dyn AnyError as *const T;
|
|
|
|
let error = unsafe { &*error };
|
|
|
|
Some(error)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 18:22:15 -04:00
|
|
|
impl fmt::Display for ErrBox {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Simple { message, .. } => f.write_str(message),
|
|
|
|
Self::Boxed(error) => error.fmt(f),
|
|
|
|
}
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AnyError> From<T> for ErrBox {
|
|
|
|
fn from(error: T) -> Self {
|
2020-08-25 18:22:15 -04:00
|
|
|
Self::Boxed(Box::new(error))
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Box<dyn AnyError>> for ErrBox {
|
|
|
|
fn from(boxed: Box<dyn AnyError>) -> Self {
|
2020-08-25 18:22:15 -04:00
|
|
|
Self::Boxed(boxed)
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2020-03-02 17:20:16 -05: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.
|
2019-04-01 15:09:59 -04:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2020-03-02 17:20:16 -05:00
|
|
|
pub struct JSError {
|
2019-02-26 17:36:05 -05:00
|
|
|
pub message: String,
|
|
|
|
pub source_line: Option<String>,
|
|
|
|
pub script_resource_name: Option<String>,
|
|
|
|
pub line_number: Option<i64>,
|
2020-04-13 10:54:16 -04:00
|
|
|
pub start_column: Option<i64>, // 0-based
|
|
|
|
pub end_column: Option<i64>, // 0-based
|
2020-03-02 17:20:16 -05:00
|
|
|
pub frames: Vec<JSStackFrame>,
|
2020-04-11 02:08:11 -04:00
|
|
|
pub formatted_frames: Vec<String>,
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2019-07-10 18:53:48 -04:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2020-03-02 17:20:16 -05: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>,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-02 17:20:16 -05:00
|
|
|
impl JSError {
|
|
|
|
pub(crate) fn create(js_error: Self) -> ErrBox {
|
2020-08-25 18:22:15 -04:00
|
|
|
js_error.into()
|
2019-04-04 05:33:32 -04:00
|
|
|
}
|
2019-02-26 17:36:05 -05:00
|
|
|
|
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>,
|
|
|
|
) -> 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);
|
|
|
|
|
2020-04-19 09:17:22 -04:00
|
|
|
let (message, frames, formatted_frames) = if exception.is_native_error() {
|
|
|
|
// The exception is a JS Error object.
|
|
|
|
let exception: v8::Local<v8::Object> =
|
|
|
|
exception.clone().try_into().unwrap();
|
2020-04-10 12:26:52 -04:00
|
|
|
|
2020-04-19 09:17:22 -04:00
|
|
|
// Get the message by formatting error.name and error.message.
|
2020-06-20 07:18:08 -04:00
|
|
|
let name = get_property(scope, exception, "name")
|
2020-04-19 09:17:22 -04:00
|
|
|
.and_then(|m| m.to_string(scope))
|
|
|
|
.map(|s| s.to_rust_string_lossy(scope))
|
|
|
|
.unwrap_or_else(|| "undefined".to_string());
|
2020-06-20 07:18:08 -04:00
|
|
|
let message_prop = get_property(scope, exception, "message")
|
2020-04-19 09:17:22 -04:00
|
|
|
.and_then(|m| m.to_string(scope))
|
|
|
|
.map(|s| s.to_rust_string_lossy(scope))
|
|
|
|
.unwrap_or_else(|| "undefined".to_string());
|
|
|
|
let message = format!("Uncaught {}: {}", name, message_prop);
|
2020-04-10 12:26:52 -04:00
|
|
|
|
2020-04-19 09:17:22 -04:00
|
|
|
// Access error.stack to ensure that prepareStackTrace() has been called.
|
|
|
|
// This should populate error.__callSiteEvals and error.__formattedFrames.
|
2020-06-20 07:18:08 -04:00
|
|
|
let _ = get_property(scope, exception, "stack");
|
2020-04-19 09:17:22 -04:00
|
|
|
|
|
|
|
// Read an array of structured frames from error.__callSiteEvals.
|
2020-06-20 07:18:08 -04:00
|
|
|
let frames_v8 = get_property(scope, exception, "__callSiteEvals");
|
2020-04-19 09:17:22 -04:00
|
|
|
let frames_v8: Option<v8::Local<v8::Array>> =
|
|
|
|
frames_v8.and_then(|a| a.try_into().ok());
|
2020-04-11 02:08:11 -04:00
|
|
|
|
2020-04-19 09:17:22 -04:00
|
|
|
// Read an array of pre-formatted frames from error.__formattedFrames.
|
2020-04-11 02:08:11 -04:00
|
|
|
let formatted_frames_v8 =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, exception, "__formattedFrames");
|
2020-04-19 09:17:22 -04:00
|
|
|
let formatted_frames_v8: Option<v8::Local<v8::Array>> =
|
|
|
|
formatted_frames_v8.and_then(|a| a.try_into().ok());
|
|
|
|
|
|
|
|
// Convert them into Vec<JSStack> and Vec<String> respectively.
|
|
|
|
let mut frames: Vec<JSStackFrame> = vec![];
|
|
|
|
let mut formatted_frames: Vec<String> = vec![];
|
|
|
|
if let (Some(frames_v8), Some(formatted_frames_v8)) =
|
|
|
|
(frames_v8, formatted_frames_v8)
|
|
|
|
{
|
|
|
|
for i in 0..frames_v8.length() {
|
2020-06-20 07:18:08 -04:00
|
|
|
let call_site: v8::Local<v8::Object> =
|
|
|
|
frames_v8.get_index(scope, i).unwrap().try_into().unwrap();
|
2020-04-19 09:17:22 -04:00
|
|
|
let type_name: Option<v8::Local<v8::String>> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "typeName")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.ok();
|
|
|
|
let type_name = type_name.map(|s| s.to_rust_string_lossy(scope));
|
|
|
|
let function_name: Option<v8::Local<v8::String>> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "functionName")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.ok();
|
|
|
|
let function_name =
|
|
|
|
function_name.map(|s| s.to_rust_string_lossy(scope));
|
|
|
|
let method_name: Option<v8::Local<v8::String>> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "methodName")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.ok();
|
|
|
|
let method_name = method_name.map(|s| s.to_rust_string_lossy(scope));
|
|
|
|
let file_name: Option<v8::Local<v8::String>> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "fileName")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.ok();
|
|
|
|
let file_name = file_name.map(|s| s.to_rust_string_lossy(scope));
|
|
|
|
let line_number: Option<v8::Local<v8::Integer>> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "lineNumber")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.ok();
|
|
|
|
let line_number = line_number.map(|n| n.value());
|
|
|
|
let column_number: Option<v8::Local<v8::Integer>> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "columnNumber")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.ok();
|
|
|
|
let column_number = column_number.map(|n| n.value());
|
|
|
|
let eval_origin: Option<v8::Local<v8::String>> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "evalOrigin")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.ok();
|
|
|
|
let eval_origin = eval_origin.map(|s| s.to_rust_string_lossy(scope));
|
|
|
|
let is_top_level: Option<v8::Local<v8::Boolean>> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "isTopLevel")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.ok();
|
|
|
|
let is_top_level = is_top_level.map(|b| b.is_true());
|
|
|
|
let is_eval: v8::Local<v8::Boolean> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "isEval")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
let is_eval = is_eval.is_true();
|
|
|
|
let is_native: v8::Local<v8::Boolean> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "isNative")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
let is_native = is_native.is_true();
|
|
|
|
let is_constructor: v8::Local<v8::Boolean> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "isConstructor")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
let is_constructor = is_constructor.is_true();
|
|
|
|
let is_async: v8::Local<v8::Boolean> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "isAsync")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
let is_async = is_async.is_true();
|
|
|
|
let is_promise_all: v8::Local<v8::Boolean> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "isPromiseAll")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
let is_promise_all = is_promise_all.is_true();
|
|
|
|
let promise_index: Option<v8::Local<v8::Integer>> =
|
2020-06-20 07:18:08 -04:00
|
|
|
get_property(scope, call_site, "columnNumber")
|
2020-04-19 09:17:22 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.ok();
|
|
|
|
let promise_index = promise_index.map(|n| n.value());
|
|
|
|
frames.push(JSStackFrame {
|
|
|
|
type_name,
|
|
|
|
function_name,
|
|
|
|
method_name,
|
|
|
|
file_name,
|
|
|
|
line_number,
|
|
|
|
column_number,
|
|
|
|
eval_origin,
|
|
|
|
is_top_level,
|
|
|
|
is_eval,
|
|
|
|
is_native,
|
|
|
|
is_constructor,
|
|
|
|
is_async,
|
|
|
|
is_promise_all,
|
|
|
|
promise_index,
|
|
|
|
});
|
|
|
|
let formatted_frame: v8::Local<v8::String> = formatted_frames_v8
|
2020-06-20 07:18:08 -04:00
|
|
|
.get_index(scope, i)
|
2020-04-13 10:54:16 -04:00
|
|
|
.unwrap()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
2020-04-19 09:17:22 -04:00
|
|
|
let formatted_frame = formatted_frame.to_rust_string_lossy(scope);
|
|
|
|
formatted_frames.push(formatted_frame)
|
|
|
|
}
|
2020-04-10 12:26:52 -04:00
|
|
|
}
|
2020-04-19 09:17:22 -04:00
|
|
|
(message, frames, formatted_frames)
|
2020-04-10 12:26:52 -04:00
|
|
|
} else {
|
2020-04-19 09:17:22 -04:00
|
|
|
// The exception is not a JS Error object.
|
|
|
|
// Get the message given by V8::Exception::create_message(), and provide
|
|
|
|
// empty frames.
|
|
|
|
(msg.get(scope).to_rust_string_lossy(scope), vec![], vec![])
|
2020-04-10 12:26:52 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Self {
|
2020-04-19 09:17:22 -04:00
|
|
|
message,
|
2020-04-10 12:26:52 -04:00
|
|
|
script_resource_name: msg
|
|
|
|
.get_script_resource_name(scope)
|
|
|
|
.and_then(|v| v8::Local::<v8::String>::try_from(v).ok())
|
|
|
|
.map(|v| v.to_rust_string_lossy(scope)),
|
|
|
|
source_line: msg
|
2020-06-20 07:18:08 -04:00
|
|
|
.get_source_line(scope)
|
2020-04-10 12:26:52 -04:00
|
|
|
.map(|v| v.to_rust_string_lossy(scope)),
|
2020-06-20 07:18:08 -04:00
|
|
|
line_number: msg.get_line_number(scope).and_then(|v| v.try_into().ok()),
|
2020-04-10 12:26:52 -04:00
|
|
|
start_column: msg.get_start_column().try_into().ok(),
|
|
|
|
end_column: msg.get_end_column().try_into().ok(),
|
|
|
|
frames,
|
2020-04-11 02:08:11 -04:00
|
|
|
formatted_frames,
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 17:20:16 -05:00
|
|
|
impl Error for JSError {}
|
2019-07-10 18:53:48 -04:00
|
|
|
|
2020-03-02 17:20:16 -05:00
|
|
|
fn format_source_loc(
|
2020-04-13 10:54:16 -04:00
|
|
|
file_name: &str,
|
2020-03-02 17:20:16 -05:00
|
|
|
line_number: i64,
|
2020-04-13 10:54:16 -04:00
|
|
|
column_number: i64,
|
2020-03-02 17:20:16 -05:00
|
|
|
) -> String {
|
2020-04-13 10:54:16 -04:00
|
|
|
let line_number = line_number;
|
|
|
|
let column_number = column_number;
|
|
|
|
format!("{}:{}:{}", file_name, line_number, column_number)
|
2019-07-10 18:53:48 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 17:20:16 -05:00
|
|
|
impl fmt::Display for JSError {
|
2019-07-10 18:53:48 -04:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2020-05-16 09:41:32 -04:00
|
|
|
if let Some(script_resource_name) = &self.script_resource_name {
|
2020-03-02 17:20:16 -05:00
|
|
|
if self.line_number.is_some() && self.start_column.is_some() {
|
|
|
|
assert!(self.line_number.is_some());
|
|
|
|
assert!(self.start_column.is_some());
|
2019-07-10 18:53:48 -04:00
|
|
|
let source_loc = format_source_loc(
|
|
|
|
script_resource_name,
|
2020-04-13 10:54:16 -04:00
|
|
|
self.line_number.unwrap(),
|
|
|
|
self.start_column.unwrap(),
|
2019-07-10 18:53:48 -04:00
|
|
|
);
|
|
|
|
write!(f, "{}", source_loc)?;
|
|
|
|
}
|
2020-03-02 17:20:16 -05:00
|
|
|
if self.source_line.is_some() {
|
2020-05-01 13:03:54 -04:00
|
|
|
let source_line = self.source_line.as_ref().unwrap();
|
|
|
|
write!(f, "\n{}\n", source_line)?;
|
2019-07-10 18:53:48 -04:00
|
|
|
let mut s = String::new();
|
2020-03-02 17:20:16 -05:00
|
|
|
for i in 0..self.end_column.unwrap() {
|
|
|
|
if i >= self.start_column.unwrap() {
|
2019-07-10 18:53:48 -04:00
|
|
|
s.push('^');
|
2020-05-01 13:03:54 -04:00
|
|
|
} else if source_line.chars().nth(i as usize).unwrap() == '\t' {
|
|
|
|
s.push('\t');
|
2019-07-10 18:53:48 -04:00
|
|
|
} else {
|
|
|
|
s.push(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeln!(f, "{}", s)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 17:20:16 -05:00
|
|
|
write!(f, "{}", self.message)?;
|
2019-07-10 18:53:48 -04:00
|
|
|
|
2020-04-11 02:08:11 -04:00
|
|
|
for formatted_frame in &self.formatted_frames {
|
|
|
|
// TODO: Strip ANSI color from formatted_frame.
|
|
|
|
write!(f, "\n at {}", formatted_frame)?;
|
2019-07-10 18:53:48 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 13:36:43 -04:00
|
|
|
|
|
|
|
pub(crate) fn attach_handle_to_error(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::Isolate,
|
2020-05-28 13:36:43 -04:00
|
|
|
err: ErrBox,
|
|
|
|
handle: v8::Local<v8::Value>,
|
|
|
|
) -> ErrBox {
|
2020-08-25 18:22:15 -04:00
|
|
|
// TODO(bartomieju): this is a special case...
|
2020-05-28 13:36:43 -04:00
|
|
|
ErrWithV8Handle::new(scope, err, handle).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(piscisaureus): rusty_v8 should implement the Error trait on
|
|
|
|
// values of type v8::Global<T>.
|
|
|
|
pub struct ErrWithV8Handle {
|
|
|
|
err: ErrBox,
|
|
|
|
handle: v8::Global<v8::Value>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ErrWithV8Handle {
|
|
|
|
pub fn new(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::Isolate,
|
2020-05-28 13:36:43 -04:00
|
|
|
err: ErrBox,
|
|
|
|
handle: v8::Local<v8::Value>,
|
|
|
|
) -> Self {
|
2020-07-18 16:32:11 -04:00
|
|
|
let handle = v8::Global::new(scope, handle);
|
2020-05-28 13:36:43 -04:00
|
|
|
Self { err, handle }
|
|
|
|
}
|
|
|
|
|
2020-07-18 16:32:11 -04:00
|
|
|
pub fn get_handle<'s>(
|
|
|
|
&self,
|
|
|
|
scope: &mut v8::HandleScope<'s>,
|
|
|
|
) -> v8::Local<'s, v8::Value> {
|
|
|
|
v8::Local::new(scope, &self.handle)
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for ErrWithV8Handle {}
|
|
|
|
unsafe impl Sync for ErrWithV8Handle {}
|
|
|
|
|
|
|
|
impl Error for ErrWithV8Handle {}
|
|
|
|
|
|
|
|
impl fmt::Display for ErrWithV8Handle {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.err.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for ErrWithV8Handle {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.err.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 18:22:15 -04:00
|
|
|
|
|
|
|
#[cfg(tests)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn test_bad_resource() {
|
2020-08-25 18:48:25 -04:00
|
|
|
let err = ErrBox::bad_resource("Resource has been closed");
|
2020-08-25 18:22:15 -04:00
|
|
|
assert_eq!(err.1, "BadResource");
|
|
|
|
assert_eq!(err.to_string(), "Resource has been closed");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bad_resource_id() {
|
|
|
|
let err = ErrBox::bad_resource_id();
|
|
|
|
assert_eq!(err.1, "BadResource");
|
|
|
|
assert_eq!(err.to_string(), "Bad resource ID");
|
|
|
|
}
|
|
|
|
}
|