summaryrefslogtreecommitdiff
path: root/cli/fmt_errors.rs
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2021-09-18 14:40:04 +0100
committerGitHub <noreply@github.com>2021-09-18 15:40:04 +0200
commit75ca013f076f443dce3d38f31a168295351ed0e5 (patch)
treea8f369429ff650516a513a0416e8984f457835a4 /cli/fmt_errors.rs
parentf840906943849f5a09981e172d57e84301b77386 (diff)
fix(cli/fmt_errors): Abbreviate long data URLs in stack traces (#12127)
Co-authored-by: Mike White <mike.white@auctane.com>
Diffstat (limited to 'cli/fmt_errors.rs')
-rw-r--r--cli/fmt_errors.rs31
1 files changed, 29 insertions, 2 deletions
diff --git a/cli/fmt_errors.rs b/cli/fmt_errors.rs
index 53a8535f0..2b95f0fc7 100644
--- a/cli/fmt_errors.rs
+++ b/cli/fmt_errors.rs
@@ -5,13 +5,40 @@ use crate::colors::italic_bold;
use crate::colors::red;
use crate::colors::yellow;
use deno_core::error::{AnyError, 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;
-// Keep in sync with `runtime/js/40_error_stack.js`.
+pub fn format_file_name(file_name: &str) -> String {
+ if file_name.len() > DATA_URL_ABBREV_THRESHOLD {
+ if let Ok(url) = Url::parse(file_name) {
+ if url.scheme() == "data" {
+ let data_path = url.path();
+ if let Some(data_pieces) = data_path.split_once(',') {
+ let data_length = data_pieces.1.len();
+ if let Some(data_start) = data_pieces.1.get(0..20) {
+ if let Some(data_end) = data_pieces.1.get(data_length - 20..) {
+ return format!(
+ "{}:{},{}......{}",
+ url.scheme(),
+ data_pieces.0,
+ data_start,
+ data_end
+ );
+ }
+ }
+ }
+ }
+ }
+ }
+ file_name.to_string()
+}
+
+// Keep in sync with `/core/error.js`.
pub fn format_location(frame: &JsStackFrame) -> String {
let _internal = frame
.file_name
@@ -22,7 +49,7 @@ pub fn format_location(frame: &JsStackFrame) -> String {
}
let mut result = String::new();
if let Some(file_name) = &frame.file_name {
- result += &cyan(&file_name).to_string();
+ result += &cyan(&format_file_name(file_name)).to_string();
} else {
if frame.is_eval {
result +=