mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 04:39:10 +00:00
refactor: Remove PrettyJsError and js_error_create_fn (#14378)
This commit: - removes "fmt_errors::PrettyJsError" in favor of "format_js_error" fn - removes "deno_core::JsError::create" and "deno_core::RuntimeOptions::js_error_create_fn" - adds new option to "deno_runtime::ops::worker_host::init"
This commit is contained in:
parent
58eab0e2b3
commit
9853c96cc4
15 changed files with 104 additions and 78 deletions
|
@ -4,11 +4,8 @@ use crate::colors::cyan;
|
|||
use crate::colors::italic_bold;
|
||||
use crate::colors::red;
|
||||
use crate::colors::yellow;
|
||||
use deno_core::error::{AnyError, JsError, JsStackFrame};
|
||||
use deno_core::error::{JsError, JsStackFrame};
|
||||
use deno_core::url::Url;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::ops::Deref;
|
||||
|
||||
const SOURCE_ABBREV_THRESHOLD: usize = 150;
|
||||
const DATA_URL_ABBREV_THRESHOLD: usize = 150;
|
||||
|
@ -181,12 +178,12 @@ fn format_maybe_source_line(
|
|||
format!("\n{}{}\n{}{}", indent, source_line, indent, color_underline)
|
||||
}
|
||||
|
||||
fn format_js_error(js_error: &JsError, is_child: bool) -> String {
|
||||
fn format_js_error_inner(js_error: &JsError, is_child: bool) -> String {
|
||||
let mut s = String::new();
|
||||
s.push_str(&js_error.exception_message);
|
||||
if let Some(aggregated) = &js_error.aggregated {
|
||||
for aggregated_error in aggregated {
|
||||
let error_string = format_js_error(aggregated_error, true);
|
||||
let error_string = format_js_error_inner(aggregated_error, true);
|
||||
for line in error_string.trim_start_matches("Uncaught ").lines() {
|
||||
s.push_str(&format!("\n {}", line));
|
||||
}
|
||||
|
@ -209,7 +206,7 @@ fn format_js_error(js_error: &JsError, is_child: bool) -> String {
|
|||
s.push_str(&format!("\n at {}", format_frame(frame)));
|
||||
}
|
||||
if let Some(cause) = &js_error.cause {
|
||||
let error_string = format_js_error(cause, true);
|
||||
let error_string = format_js_error_inner(cause, true);
|
||||
s.push_str(&format!(
|
||||
"\nCaused by: {}",
|
||||
error_string.trim_start_matches("Uncaught ")
|
||||
|
@ -218,33 +215,10 @@ fn format_js_error(js_error: &JsError, is_child: bool) -> String {
|
|||
s
|
||||
}
|
||||
|
||||
/// Wrapper around deno_core::JsError which provides colorful
|
||||
/// string representation.
|
||||
#[derive(Debug)]
|
||||
pub struct PrettyJsError(JsError);
|
||||
|
||||
impl PrettyJsError {
|
||||
pub fn create(js_error: JsError) -> AnyError {
|
||||
let pretty_js_error = Self(js_error);
|
||||
pretty_js_error.into()
|
||||
}
|
||||
pub fn format_js_error(js_error: &JsError) -> String {
|
||||
format_js_error_inner(js_error, false)
|
||||
}
|
||||
|
||||
impl Deref for PrettyJsError {
|
||||
type Target = JsError;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for PrettyJsError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", &format_js_error(&self.0, false))
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for PrettyJsError {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue