1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-21 23:04:45 -05:00

refactor(cli): rename fmt_errors::JsError to PrettyJsError (#8435)

This commit renames "fmt_errors::JsError" to "PrettyJsError"
to avoid confusion with "deno_core::JsError".

Consequently "CoreJsError" aliases to "deno_core::JsError"
were removed.

Additionally source mapping step has been removed from
"PrettyJsError::create" to better separate domains.
This commit is contained in:
Bartek Iwańczuk 2020-11-19 20:37:22 +01:00 committed by GitHub
parent 9eaa1fb71d
commit e582796f42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 28 deletions

View file

@ -1,13 +1,10 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
//! This mod provides DenoError to unify errors across Deno.
use crate::colors;
use crate::source_maps::apply_source_map;
use crate::source_maps::SourceMapGetter;
use deno_core::error::{AnyError, JsError as CoreJsError, JsStackFrame};
use deno_core::error::{AnyError, JsError, JsStackFrame};
use std::error::Error;
use std::fmt;
use std::ops::Deref;
use std::sync::Arc;
const SOURCE_ABBREV_THRESHOLD: usize = 150;
@ -231,29 +228,26 @@ fn format_maybe_source_line(
format!("\n{}{}\n{}{}", indent, source_line, indent, color_underline)
}
/// Wrapper around deno_core::JsError which provides color to_string.
/// Wrapper around deno_core::JsError which provides colorful
/// string representation.
#[derive(Debug)]
pub struct JsError(CoreJsError);
pub struct PrettyJsError(JsError);
impl JsError {
pub fn create(
core_js_error: CoreJsError,
source_map_getter: Arc<impl SourceMapGetter>,
) -> AnyError {
let core_js_error = apply_source_map(&core_js_error, source_map_getter);
let js_error = Self(core_js_error);
js_error.into()
impl PrettyJsError {
pub fn create(js_error: JsError) -> AnyError {
let pretty_js_error = Self(js_error);
pretty_js_error.into()
}
}
impl Deref for JsError {
type Target = CoreJsError;
impl Deref for PrettyJsError {
type Target = JsError;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl fmt::Display for JsError {
impl fmt::Display for PrettyJsError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut frames = self.0.frames.clone();
@ -290,7 +284,7 @@ impl fmt::Display for JsError {
}
}
impl Error for JsError {}
impl Error for PrettyJsError {}
#[cfg(test)]
mod tests {

View file

@ -1,7 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::colors;
use crate::fmt_errors::JsError;
use crate::fmt_errors::PrettyJsError;
use crate::ops::io::get_stdio;
use crate::permissions::Permissions;
use crate::program_state::ProgramState;
@ -301,7 +301,7 @@ fn serialize_worker_event(event: WorkerEvent) -> Value {
}
});
if let Ok(js_error) = error.downcast::<JsError>() {
if let Ok(js_error) = error.downcast::<PrettyJsError>() {
serialized_error = json!({
"type": "terminalError",
"error": {
@ -323,7 +323,7 @@ fn serialize_worker_event(event: WorkerEvent) -> Value {
}
});
if let Ok(js_error) = error.downcast::<JsError>() {
if let Ok(js_error) = error.downcast::<PrettyJsError>() {
serialized_error = json!({
"type": "error",
"error": {

View file

@ -2,7 +2,7 @@
//! This mod provides functions to remap a `JsError` based on a source map.
use deno_core::error::JsError as CoreJsError;
use deno_core::error::JsError;
use sourcemap::SourceMap;
use std::collections::HashMap;
use std::str;
@ -26,9 +26,9 @@ pub type CachedMaps = HashMap<String, Option<SourceMap>>;
/// file names and line/column numbers point to the location in the original
/// source, rather than the transpiled source code.
pub fn apply_source_map<G: SourceMapGetter>(
js_error: &CoreJsError,
js_error: &JsError,
getter: Arc<G>,
) -> CoreJsError {
) -> JsError {
// Note that js_error.frames has already been source mapped in
// prepareStackTrace().
let mut mappings_map: CachedMaps = HashMap::new();
@ -71,7 +71,7 @@ pub fn apply_source_map<G: SourceMapGetter>(
_ => js_error.source_line.clone(),
};
CoreJsError {
JsError {
message: js_error.message.clone(),
source_line,
script_resource_name,
@ -198,7 +198,7 @@ mod tests {
#[test]
fn apply_source_map_line() {
let e = CoreJsError {
let e = JsError {
message: "TypeError: baz".to_string(),
source_line: Some("foo".to_string()),
script_resource_name: Some("foo_bar.ts".to_string()),

View file

@ -1,7 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
use crate::colors;
use crate::fmt_errors::JsError;
use crate::fmt_errors::PrettyJsError;
use crate::inspector::DenoInspector;
use crate::inspector::InspectorSession;
use crate::js;
@ -11,6 +11,7 @@ use crate::ops;
use crate::ops::io::get_stdio;
use crate::permissions::Permissions;
use crate::program_state::ProgramState;
use crate::source_maps::apply_source_map;
use deno_core::error::AnyError;
use deno_core::futures::channel::mpsc;
use deno_core::futures::future::poll_fn;
@ -121,7 +122,9 @@ impl Worker {
module_loader: Some(module_loader),
startup_snapshot: Some(startup_snapshot),
js_error_create_fn: Some(Box::new(move |core_js_error| {
JsError::create(core_js_error, global_state_.clone())
let source_mapped_error =
apply_source_map(&core_js_error, global_state_.clone());
PrettyJsError::create(source_mapped_error)
})),
..Default::default()
});