22// License, v. 2.0. If a copy of the MPL was not distributed with this
33// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44
5- use crate :: error:: { AndromedaError , Result , extract_runtime_error_info, read_file_with_context} ;
6- use andromeda_core:: { HostData , Runtime , RuntimeConfig , RuntimeFile } ;
5+ use crate :: error:: { Result , read_file_with_context} ;
6+ use andromeda_core:: {
7+ AndromedaError , ErrorReporter , HostData , Runtime , RuntimeConfig , RuntimeFile ,
8+ } ;
79use andromeda_runtime:: {
810 recommended_builtins, recommended_eventloop_handler, recommended_extensions,
911} ;
@@ -12,7 +14,7 @@ use andromeda_runtime::{
1214pub fn run ( verbose : bool , no_strict : bool , files : Vec < RuntimeFile > ) -> Result < ( ) > {
1315 // Validate that we have files to run
1416 if files. is_empty ( ) {
15- return Err ( AndromedaError :: invalid_argument (
17+ return Err ( crate :: error :: AndromedaError :: invalid_argument (
1618 "files" . to_string ( ) ,
1719 "at least one file path" . to_string ( ) ,
1820 "empty list" . to_string ( ) ,
@@ -24,7 +26,7 @@ pub fn run(verbose: bool, no_strict: bool, files: Vec<RuntimeFile>) -> Result<()
2426 if let RuntimeFile :: Local { path } = file {
2527 let file_path = std:: path:: Path :: new ( path) ;
2628 if !file_path. exists ( ) {
27- return Err ( AndromedaError :: file_not_found (
29+ return Err ( crate :: error :: AndromedaError :: file_not_found (
2830 file_path. to_path_buf ( ) ,
2931 std:: io:: Error :: new ( std:: io:: ErrorKind :: NotFound , "File not found" ) ,
3032 ) ) ;
@@ -38,6 +40,15 @@ pub fn run(verbose: bool, no_strict: bool, files: Vec<RuntimeFile>) -> Result<()
3840 let ( macro_task_tx, macro_task_rx) = std:: sync:: mpsc:: channel ( ) ;
3941 let host_data = HostData :: new ( macro_task_tx) ;
4042
43+ // Store file information before moving files into runtime
44+ let first_file_info = files. first ( ) . and_then ( |file| {
45+ if let RuntimeFile :: Local { path } = file {
46+ Some ( path. clone ( ) )
47+ } else {
48+ None
49+ }
50+ } ) ;
51+
4152 let runtime = Runtime :: new (
4253 RuntimeConfig {
4354 no_strict,
@@ -74,33 +85,45 @@ pub fn run(verbose: bool, no_strict: bool, files: Vec<RuntimeFile>) -> Result<()
7485 . to_string ( )
7586 } ) ;
7687
77- // Try to extract file path from the error context if available
78- let file_path =
79- runtime_output
80- . agent
81- . run_in_realm ( & runtime_output. realm_root , |_agent, _gc| {
82- // This is a simplified approach - Nova might provide better error context in the future
83- None :: < String >
84- } ) ;
88+ // Try to get the first file from our runtime files to show source context
89+ let ( file_path, source_content) = if let Some ( path) = first_file_info {
90+ match read_file_with_context ( std:: path:: Path :: new ( & path) ) {
91+ Ok ( content) => ( Some ( path) , Some ( content) ) ,
92+ Err ( _) => ( Some ( path) , None ) ,
93+ }
94+ } else {
95+ ( None , None )
96+ } ;
8597
86- // Extract line and column information from the error message
87- let ( clean_message, line, column) =
88- extract_runtime_error_info ( & error_message, file_path. clone ( ) ) ;
98+ // Create an enhanced runtime error with source context if available
99+ let enhanced_error = if let ( Some ( path) , Some ( content) ) = ( file_path, source_content) {
100+ // Try to find a better source span by looking for the error location in the message
101+ let source_span = if error_message. contains ( "fertch" ) {
102+ // Find the position of 'fertch' in the source code
103+ if let Some ( pos) = content. find ( "fertch" ) {
104+ miette:: SourceSpan :: new ( pos. into ( ) , 6 ) // 'fertch' is 6 characters
105+ } else {
106+ miette:: SourceSpan :: new ( 0 . into ( ) , 1 )
107+ }
108+ } else {
109+ miette:: SourceSpan :: new ( 0 . into ( ) , 1 )
110+ } ;
89111
90- // Try to get source content if we have a file path
91- let source_content = if let Some ( ref path) = file_path {
92- read_file_with_context ( std:: path:: Path :: new ( path) ) . ok ( )
112+ AndromedaError :: runtime_error_with_location (
113+ error_message. clone ( ) ,
114+ & content,
115+ & path,
116+ source_span,
117+ )
93118 } else {
94- None
119+ AndromedaError :: runtime_error ( error_message . clone ( ) )
95120 } ;
96121
97- Err ( AndromedaError :: runtime_error (
98- clean_message,
99- file_path,
100- line,
101- column,
102- source_content,
103- ) )
122+ // Print the enhanced error using our error reporting system
123+ ErrorReporter :: print_error ( & enhanced_error) ;
124+
125+ // Exit directly instead of returning another error to avoid double printing
126+ std:: process:: exit ( 1 ) ;
104127 }
105128 }
106129}
0 commit comments