2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
use anyhow::Error;
|
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
|
|
|
|
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 {
|
2021-04-26 15:39:55 -04:00
|
|
|
type_error(format!("Invalid hostname: '{}'", hostname))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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.
|
2019-04-01 15:09:59 -04:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2020-09-06 15:44:29 -04:00
|
|
|
pub struct JsError {
|
2019-02-26 17:36:05 -05:00
|
|
|
pub message: String,
|
2021-12-29 13:34:13 -05:00
|
|
|
pub cause: Option<Box<JsError>>,
|
2019-02-26 17:36:05 -05:00
|
|
|
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-09-06 15:44:29 -04:00
|
|
|
pub frames: Vec<JsStackFrame>,
|
2020-09-22 17:30:03 -04:00
|
|
|
pub stack: Option<String>,
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2021-03-26 20:25:48 -04:00
|
|
|
#[derive(Debug, PartialEq, Clone, serde::Deserialize)]
|
|
|
|
#[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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-26 20:25:48 -04:00
|
|
|
#[derive(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 {
|
2021-11-16 09:02:28 -05:00
|
|
|
pub(crate) fn create(js_error: Self) -> Error {
|
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>,
|
2021-12-29 13:34:13 -05:00
|
|
|
) -> Self {
|
|
|
|
Self::inner_from_v8_exception(scope, exception, Default::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inner_from_v8_exception<'a>(
|
|
|
|
scope: &'a mut v8::HandleScope,
|
|
|
|
exception: v8::Local<'a, v8::Value>,
|
|
|
|
mut seen: HashSet<v8::Local<'a, v8::Value>>,
|
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);
|
|
|
|
|
2021-12-29 13:34:13 -05:00
|
|
|
let (message, frames, stack, cause) =
|
|
|
|
if is_instance_of_error(scope, exception) {
|
|
|
|
// 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 =
|
|
|
|
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());
|
|
|
|
let message = if !name.is_empty() && !message_prop.is_empty() {
|
|
|
|
format!("Uncaught {}: {}", name, message_prop)
|
|
|
|
} else if !name.is_empty() {
|
|
|
|
format!("Uncaught {}", name)
|
|
|
|
} else if !message_prop.is_empty() {
|
|
|
|
format!("Uncaught {}", message_prop)
|
|
|
|
} else {
|
|
|
|
"Uncaught".to_string()
|
|
|
|
};
|
|
|
|
let cause = cause.and_then(|cause| {
|
|
|
|
if cause.is_undefined() || seen.contains(&cause) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
seen.insert(cause);
|
|
|
|
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>
|
|
|
|
let frames: Vec<JsStackFrame> = match frames_v8 {
|
|
|
|
Some(frames_v8) => {
|
|
|
|
serde_v8::from_v8(scope, frames_v8.into()).unwrap()
|
|
|
|
}
|
|
|
|
None => vec![],
|
|
|
|
};
|
|
|
|
(message, frames, stack, cause)
|
2020-10-08 05:05:19 -04:00
|
|
|
} else {
|
2021-12-29 13:34:13 -05: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![],
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
2021-03-26 20:25:48 -04:00
|
|
|
};
|
2020-04-10 12:26:52 -04:00
|
|
|
|
|
|
|
Self {
|
2020-04-19 09:17:22 -04:00
|
|
|
message,
|
2021-12-29 13:34:13 -05:00
|
|
|
cause,
|
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-09-22 17:30:03 -04:00
|
|
|
stack,
|
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-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-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 {
|
2020-10-31 14:57:19 -04:00
|
|
|
return write!(f, "{}", stack);
|
2020-09-22 17:30:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 14:57:19 -04:00
|
|
|
write!(f, "{}", self.message)?;
|
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() {
|
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
|
|
|
);
|
2020-10-31 14:57:19 -04:00
|
|
|
write!(f, "\n at {}", source_loc)?;
|
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,
|
2021-11-16 09:02:28 -05:00
|
|
|
err: Error,
|
2020-05-28 13:36:43 -04:00
|
|
|
handle: v8::Local<v8::Value>,
|
2021-11-16 09:02:28 -05:00
|
|
|
) -> Error {
|
2020-05-28 13:36:43 -04:00
|
|
|
ErrWithV8Handle::new(scope, err, handle).into()
|
|
|
|
}
|
|
|
|
|
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`.
|
2021-10-19 12:26:45 -04:00
|
|
|
pub(crate) fn is_instance_of_error<'s>(
|
2021-09-06 16:59:20 -04:00
|
|
|
scope: &mut v8::HandleScope<'s>,
|
|
|
|
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 {
|
|
|
|
if prototype.strict_equals(error_prototype) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
maybe_prototype = prototype
|
|
|
|
.to_object(scope)
|
|
|
|
.and_then(|o| o.get_prototype(scope));
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:36:43 -04:00
|
|
|
// TODO(piscisaureus): rusty_v8 should implement the Error trait on
|
|
|
|
// values of type v8::Global<T>.
|
2020-09-15 10:24:13 -04:00
|
|
|
pub(crate) struct ErrWithV8Handle {
|
2021-11-16 09:02:28 -05:00
|
|
|
err: Error,
|
2020-05-28 13:36:43 -04:00
|
|
|
handle: v8::Global<v8::Value>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ErrWithV8Handle {
|
|
|
|
pub fn new(
|
2020-06-20 07:18:08 -04:00
|
|
|
scope: &mut v8::Isolate,
|
2021-11-16 09:02:28 -05:00
|
|
|
err: Error,
|
2020-05-28 13:36:43 -04:00
|
|
|
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 {}
|
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
impl std::error::Error for ErrWithV8Handle {}
|
2020-05-28 13:36:43 -04:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
impl Display for ErrWithV8Handle {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2021-11-16 09:02:28 -05:00
|
|
|
<Error as Display>::fmt(&self.err, f)
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
impl Debug for ErrWithV8Handle {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
<Self as Display>::fmt(self, f)
|
2020-05-28 13:36:43 -04:00
|
|
|
}
|
|
|
|
}
|
2020-08-25 18:22:15 -04:00
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|