1
1
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
2
//! This mod provides DenoError to unify errors across Deno.
3
- use deno_core:: error:: format_file_name ;
3
+ use deno_core:: error:: format_frame ;
4
4
use deno_core:: error:: JsError ;
5
- use deno_core:: error:: JsStackFrame ;
6
5
use deno_terminal:: colors:: cyan;
7
6
use deno_terminal:: colors:: italic_bold;
8
7
use deno_terminal:: colors:: red;
@@ -21,96 +20,22 @@ struct IndexedErrorReference<'a> {
21
20
index : usize ,
22
21
}
23
22
24
- // Keep in sync with `/core/error.js`.
25
- pub fn format_location ( frame : & JsStackFrame ) -> String {
26
- let _internal = frame
27
- . file_name
28
- . as_ref ( )
29
- . map ( |f| f. starts_with ( "ext:" ) )
30
- . unwrap_or ( false ) ;
31
- if frame. is_native {
32
- return cyan ( "native" ) . to_string ( ) ;
33
- }
34
- let mut result = String :: new ( ) ;
35
- let file_name = frame. file_name . clone ( ) . unwrap_or_default ( ) ;
36
- if !file_name. is_empty ( ) {
37
- result += & cyan ( & format_file_name ( & file_name) ) . to_string ( ) ;
38
- } else {
39
- if frame. is_eval {
40
- result +=
41
- & ( cyan ( & frame. eval_origin . as_ref ( ) . unwrap ( ) ) . to_string ( ) + ", " ) ;
42
- }
43
- result += & cyan ( "<anonymous>" ) . to_string ( ) ;
44
- }
45
- if let Some ( line_number) = frame. line_number {
46
- write ! ( result, ":{}" , yellow( & line_number. to_string( ) ) ) . unwrap ( ) ;
47
- if let Some ( column_number) = frame. column_number {
48
- write ! ( result, ":{}" , yellow( & column_number. to_string( ) ) ) . unwrap ( ) ;
49
- }
50
- }
51
- result
52
- }
53
-
54
- fn format_frame ( frame : & JsStackFrame ) -> String {
55
- let _internal = frame
56
- . file_name
57
- . as_ref ( )
58
- . map ( |f| f. starts_with ( "ext:" ) )
59
- . unwrap_or ( false ) ;
60
- let is_method_call =
61
- !( frame. is_top_level . unwrap_or_default ( ) || frame. is_constructor ) ;
62
- let mut result = String :: new ( ) ;
63
- if frame. is_async {
64
- result += "async " ;
65
- }
66
- if frame. is_promise_all {
67
- result += & italic_bold ( & format ! (
68
- "Promise.all (index {})" ,
69
- frame. promise_index. unwrap_or_default( )
70
- ) )
71
- . to_string ( ) ;
72
- return result;
73
- }
74
- if is_method_call {
75
- let mut formatted_method = String :: new ( ) ;
76
- if let Some ( function_name) = & frame. function_name {
77
- if let Some ( type_name) = & frame. type_name {
78
- if !function_name. starts_with ( type_name) {
79
- write ! ( formatted_method, "{type_name}." ) . unwrap ( ) ;
80
- }
81
- }
82
- formatted_method += function_name;
83
- if let Some ( method_name) = & frame. method_name {
84
- if !function_name. ends_with ( method_name) {
85
- write ! ( formatted_method, " [as {method_name}]" ) . unwrap ( ) ;
86
- }
87
- }
88
- } else {
89
- if let Some ( type_name) = & frame. type_name {
90
- write ! ( formatted_method, "{type_name}." ) . unwrap ( ) ;
23
+ struct AnsiColors ;
24
+
25
+ impl deno_core:: error:: ErrorFormat for AnsiColors {
26
+ fn fmt_element (
27
+ element : deno_core:: error:: ErrorElement ,
28
+ s : & str ,
29
+ ) -> std:: borrow:: Cow < ' _ , str > {
30
+ use deno_core:: error:: ErrorElement :: * ;
31
+ match element {
32
+ Anonymous | NativeFrame | FileName | EvalOrigin => {
33
+ cyan ( s) . to_string ( ) . into ( )
91
34
}
92
- if let Some ( method_name) = & frame. method_name {
93
- formatted_method += method_name
94
- } else {
95
- formatted_method += "<anonymous>" ;
96
- }
97
- }
98
- result += & italic_bold ( & formatted_method) . to_string ( ) ;
99
- } else if frame. is_constructor {
100
- result += "new " ;
101
- if let Some ( function_name) = & frame. function_name {
102
- write ! ( result, "{}" , italic_bold( & function_name) ) . unwrap ( ) ;
103
- } else {
104
- result += & cyan ( "<anonymous>" ) . to_string ( ) ;
35
+ LineNumber | ColumnNumber => yellow ( s) . to_string ( ) . into ( ) ,
36
+ FunctionName | PromiseAll => italic_bold ( s) . to_string ( ) . into ( ) ,
105
37
}
106
- } else if let Some ( function_name) = & frame. function_name {
107
- result += & italic_bold ( & function_name) . to_string ( ) ;
108
- } else {
109
- result += & format_location ( frame) ;
110
- return result;
111
38
}
112
- write ! ( result, " ({})" , format_location( frame) ) . unwrap ( ) ;
113
- result
114
39
}
115
40
116
41
/// Take an optional source line and associated information to format it into
@@ -254,7 +179,7 @@ fn format_js_error_inner(
254
179
0 ,
255
180
) ) ;
256
181
for frame in & js_error. frames {
257
- write ! ( s, "\n at {}" , format_frame( frame) ) . unwrap ( ) ;
182
+ write ! ( s, "\n at {}" , format_frame:: < AnsiColors > ( frame) ) . unwrap ( ) ;
258
183
}
259
184
if let Some ( cause) = & js_error. cause {
260
185
let is_caused_by_circular = circular
0 commit comments