Skip to content

Commit

Permalink
Parser: improved message when a module cannot be loaded.
Browse files Browse the repository at this point in the history
This is less confusing because it covers more cases, including when
there are not enough file access rights.
  • Loading branch information
xeioex committed Oct 19, 2024
1 parent 1985390 commit ea9b2a7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/njs_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ njs_module_require(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,

module = njs_module_find(vm, &name, 0);
if (njs_slow_path(module == NULL)) {
njs_error(vm, "Cannot find module \"%V\"", &name);
njs_error(vm, "Cannot load module \"%V\"", &name);

return NJS_ERROR;
}
Expand Down
7 changes: 4 additions & 3 deletions src/njs_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -8114,7 +8114,7 @@ njs_parser_module(njs_parser_t *parser, njs_str_t *name)
vm = parser->vm;

if (name->length == 0) {
njs_parser_syntax_error(parser, "Cannot find module \"%V\"", name);
njs_parser_syntax_error(parser, "Cannot load module \"%V\"", name);
return NULL;
}

Expand All @@ -8124,13 +8124,14 @@ njs_parser_module(njs_parser_t *parser, njs_str_t *name)
}

if (vm->module_loader == NULL) {
njs_parser_syntax_error(parser, "Cannot load module \"%V\"", name);
njs_parser_syntax_error(parser,
"Module loader callback is not provided");
return NULL;
}

module = vm->module_loader(vm, vm->module_loader_opaque, name);
if (module == NULL) {
njs_parser_syntax_error(parser, "Cannot find module \"%V\"", name);
njs_parser_syntax_error(parser, "Cannot load module \"%V\"", name);
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/njs_unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -18975,7 +18975,7 @@ static njs_unit_test_t njs_test[] =
/* require(). */

{ njs_str("require('unknown_module')"),
njs_str("Error: Cannot find module \"unknown_module\"") },
njs_str("Error: Cannot load module \"unknown_module\"") },

{ njs_str("require()"),
njs_str("TypeError: missing path") },
Expand Down Expand Up @@ -19045,7 +19045,7 @@ static njs_unit_test_t njs_test[] =
njs_str("SyntaxError: Unexpected token \"{\" in 1") },

{ njs_str("import x from ''"),
njs_str("SyntaxError: Cannot find module \"\" in 1") },
njs_str("SyntaxError: Cannot load module \"\" in 1") },

{ njs_str("export"),
njs_str("SyntaxError: Illegal export statement in 1") },
Expand Down
4 changes: 2 additions & 2 deletions test/shell_test_njs.exp
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ njs_test {
# quiet mode

njs_run {"-q" "test/js/import_chain.t.js"} \
"SyntaxError: Cannot find module \"lib2.js\" in 7"
"SyntaxError: Cannot load module \"lib2.js\" in 7"

# sandboxing

njs_test {
{"var fs = require('fs')\r\n"
"Error: Cannot find module \"fs\"\r\n"}
"Error: Cannot load module \"fs\"\r\n"}
} "-s"

njs_test {
Expand Down

0 comments on commit ea9b2a7

Please sign in to comment.