@@ -19,6 +19,12 @@ JS::CompileOptions *COMPILE_OPTS;
1919
2020using host_api::HostString;
2121
22+ namespace ScriptLoaderErrors {
23+ DEF_ERR (ModuleLoadingError, JSEXN_REFERENCEERR,
24+ " Error loading module \" {0}\" (resolved path \" {1}\" ): {2}" , 3 )
25+ DEF_ERR (BuiltinModuleExists, JSEXN_TYPEERR, " Builtin module \" {0}\" already exists" , 1 )
26+ };
27+
2228class AutoCloseFile {
2329 FILE* file;
2430
@@ -371,8 +377,7 @@ bool ScriptLoader::define_builtin_module(const char* id, HandleValue builtin) {
371377 return false ;
372378 }
373379 if (already_exists) {
374- fprintf (stderr, " Unable to define builtin %s, as it already exists" , id);
375- return false ;
380+ return api::throw_error (cx, ScriptLoaderErrors::BuiltinModuleExists, " id" );
376381 }
377382 if (!MapSet (cx, builtinModules, id_val, builtin)) {
378383 return false ;
@@ -389,31 +394,30 @@ bool ScriptLoader::load_resolved_script(JSContext *cx, const char *specifier,
389394 JS::SourceText<mozilla::Utf8Unit> &script) {
390395 FILE *file = fopen (resolved_path, " r" );
391396 if (!file) {
392- std::cerr << " Error opening file " << specifier << " (resolved to " << resolved_path << " ): "
393- << std::strerror (errno) << std::endl;
394- return false ;
397+ return api::throw_error (cx, ScriptLoaderErrors::ModuleLoadingError,
398+ specifier, resolved_path, std::strerror (errno));
395399 }
396400
397401 AutoCloseFile autoclose (file);
398402 if (fseek (file, 0 , SEEK_END) != 0 ) {
399- std::cerr << " Error seeking file " << resolved_path << std::endl;
400- return false ;
403+ return api::throw_error (cx, ScriptLoaderErrors::ModuleLoadingError,
404+ specifier, resolved_path, " can't read from file " ) ;
401405 }
402406 size_t len = ftell (file);
403407 if (fseek (file, 0 , SEEK_SET) != 0 ) {
404- std::cerr << " Error seeking file " << resolved_path << std::endl;
405- return false ;
408+ return api::throw_error (cx, ScriptLoaderErrors::ModuleLoadingError,
409+ specifier, resolved_path, " can't read from file " ) ;
406410 }
407411
408412 UniqueChars buf (js_pod_malloc<char >(len + 1 ));
409413 if (!buf) {
410- std::cerr << " Out of memory reading " << resolved_path << std::endl;
411- return false ;
414+ return api::throw_error (cx, ScriptLoaderErrors::ModuleLoadingError,
415+ specifier, resolved_path, " out of memory while reading file " ) ;
412416 }
413417 size_t cc = fread (buf.get (), sizeof (char ), len, file);
414418 if (cc != len) {
415- std::cerr << " Error reading file " << resolved_path << std::endl;
416- return false ;
419+ return api::throw_error (cx, ScriptLoaderErrors::ModuleLoadingError,
420+ specifier, resolved_path, " error reading file " ) ;
417421 }
418422
419423 return script.init (cx, std::move (buf), len);
0 commit comments