@@ -10,6 +10,7 @@ use deno_core::{
1010 ModuleSpecifier ,
1111} ;
1212use sourcemap:: SourceMap ;
13+ use value:: ConvexValue ;
1314
1415use crate :: {
1516 environment:: IsolateEnvironment ,
@@ -43,6 +44,16 @@ impl<'a, 'b, RT: Runtime, E: IsolateEnvironment<RT>> ExecutionScope<'a, 'b, RT,
4344 Ok ( error)
4445 }
4546
47+ fn extract_source_mapped_error (
48+ & mut self ,
49+ exception : v8:: Local < v8:: Value > ,
50+ ) -> anyhow:: Result < JsError > {
51+ let ( message, frame_data, custom_data) = extract_source_mapped_error ( self , exception) ?;
52+ JsError :: from_frames ( message, frame_data, custom_data, |s| {
53+ self . lookup_source_map ( s)
54+ } )
55+ }
56+
4657 pub fn lookup_source_map (
4758 & mut self ,
4859 specifier : & ModuleSpecifier ,
@@ -56,64 +67,59 @@ impl<'a, 'b, RT: Runtime, E: IsolateEnvironment<RT>> ExecutionScope<'a, 'b, RT,
5667 } ;
5768 Ok ( Some ( SourceMap :: from_slice ( source_map. as_bytes ( ) ) ?) )
5869 }
70+ }
5971
60- fn extract_source_mapped_error (
61- & mut self ,
62- exception : v8:: Local < v8:: Value > ,
63- ) -> anyhow:: Result < JsError > {
64- if !( is_instance_of_error ( self , exception) ) {
65- anyhow:: bail!( "Exception wasn't an instance of `Error`" ) ;
66- }
67- let exception_obj: v8:: Local < v8:: Object > = exception. try_into ( ) ?;
68-
69- // Get the message by formatting error.name and error.message.
70- let name = get_property ( self , exception_obj, "name" ) ?
71- . filter ( |v| !v. is_undefined ( ) )
72- . and_then ( |m| m. to_string ( self ) )
73- . map ( |s| s. to_rust_string_lossy ( self ) )
74- . unwrap_or_else ( || "Error" . to_string ( ) ) ;
75- let message_prop = get_property ( self , exception_obj, "message" ) ?
76- . filter ( |v| !v. is_undefined ( ) )
77- . and_then ( |m| m. to_string ( self ) )
78- . map ( |s| s. to_rust_string_lossy ( self ) )
79- . unwrap_or_else ( || "" . to_string ( ) ) ;
80- let message = format_uncaught_error ( message_prop, name) ;
72+ pub fn extract_source_mapped_error (
73+ scope : & mut v8:: HandleScope < ' _ > ,
74+ exception : v8:: Local < v8:: Value > ,
75+ ) -> anyhow:: Result < ( String , Vec < FrameData > , Option < ConvexValue > ) > {
76+ if !( is_instance_of_error ( scope, exception) ) {
77+ anyhow:: bail!( "Exception wasn't an instance of `Error`" ) ;
78+ }
79+ let exception_obj: v8:: Local < v8:: Object > = exception. try_into ( ) ?;
8180
82- // Access the `stack` property to ensure `prepareStackTrace` has been called.
83- // NOTE if this is the first time accessing `stack`, it will call the op
84- // `error/stack` which does a redundant source map lookup.
85- let _stack: v8:: Local < v8:: String > = get_property ( self , exception_obj, "stack" ) ?
86- . ok_or_else ( || anyhow:: anyhow!( "Exception was missing the `stack` property" ) ) ?
87- . try_into ( ) ?;
81+ // Get the message by formatting error.name and error.message.
82+ let name = get_property ( scope, exception_obj, "name" ) ?
83+ . filter ( |v| !v. is_undefined ( ) )
84+ . and_then ( |m| m. to_string ( scope) )
85+ . map ( |s| s. to_rust_string_lossy ( scope) )
86+ . unwrap_or_else ( || "Error" . to_string ( ) ) ;
87+ let message_prop = get_property ( scope, exception_obj, "message" ) ?
88+ . filter ( |v| !v. is_undefined ( ) )
89+ . and_then ( |m| m. to_string ( scope) )
90+ . map ( |s| s. to_rust_string_lossy ( scope) )
91+ . unwrap_or_else ( || "" . to_string ( ) ) ;
92+ let message = format_uncaught_error ( message_prop, name) ;
8893
89- let frame_data: v8:: Local < v8:: String > = get_property ( self , exception_obj, "__frameData" ) ?
90- . ok_or_else ( || anyhow:: anyhow!( "Exception was missing the `__frameData` property" ) ) ?
91- . try_into ( ) ?;
92- let frame_data = to_rust_string ( self , & frame_data) ?;
93- let frame_data: Vec < FrameData > = serde_json:: from_str ( & frame_data) ?;
94+ // Access the `stack` property to ensure `prepareStackTrace` has been called.
95+ // NOTE if this is the first time accessing `stack`, it will call the op
96+ // `error/stack` which does a redundant source map lookup.
97+ let _stack: v8:: Local < v8:: String > = get_property ( scope, exception_obj, "stack" ) ?
98+ . ok_or_else ( || anyhow:: anyhow!( "Exception was missing the `stack` property" ) ) ?
99+ . try_into ( ) ?;
94100
95- // error[error.ConvexErrorSymbol] === true
96- let convex_error_symbol = get_property ( self , exception_obj, "ConvexErrorSymbol" ) ?;
97- let is_convex_error = convex_error_symbol. map_or ( false , |symbol| {
98- exception_obj
99- . get ( self , symbol)
100- . map_or ( false , |v| v. is_true ( ) )
101- } ) ;
101+ let frame_data: v8:: Local < v8:: String > = get_property ( scope, exception_obj, "__frameData" ) ?
102+ . ok_or_else ( || anyhow:: anyhow!( "Exception was missing the `__frameData` property" ) ) ?
103+ . try_into ( ) ?;
104+ let frame_data = to_rust_string ( scope, & frame_data) ?;
105+ let frame_data: Vec < FrameData > = serde_json:: from_str ( & frame_data) ?;
102106
103- let custom_data = if is_convex_error {
104- let custom_data: v8:: Local < v8:: String > = get_property ( self , exception_obj, "data" ) ?
105- . ok_or_else ( || {
106- anyhow:: anyhow!( "The thrown ConvexError is missing `data` property" )
107- } ) ?
108- . try_into ( ) ?;
109- Some ( to_rust_string ( self , & custom_data) ?)
110- } else {
111- None
112- } ;
113- let ( message, custom_data) = deserialize_udf_custom_error ( message, custom_data) ?;
107+ // error[error.ConvexErrorSymbol] === true
108+ let convex_error_symbol = get_property ( scope, exception_obj, "ConvexErrorSymbol" ) ?;
109+ let is_convex_error = convex_error_symbol. map_or ( false , |symbol| {
110+ exception_obj
111+ . get ( scope, symbol)
112+ . map_or ( false , |v| v. is_true ( ) )
113+ } ) ;
114114
115- JsError :: from_frames ( message, frame_data, custom_data, |s| {
116- self . lookup_source_map ( s)
117- } )
118- }
115+ let custom_data = if is_convex_error {
116+ let custom_data: v8:: Local < v8:: String > = get_property ( scope, exception_obj, "data" ) ?
117+ . ok_or_else ( || anyhow:: anyhow!( "The thrown ConvexError is missing `data` property" ) ) ?
118+ . try_into ( ) ?;
119+ Some ( to_rust_string ( scope, & custom_data) ?)
120+ } else {
121+ None
122+ } ;
123+ let ( message, custom_data) = deserialize_udf_custom_error ( message, custom_data) ?;
124+ Ok ( ( message, frame_data, custom_data) )
119125}
0 commit comments