From a75d67d8a97b4a26dfe8623fe3b50d2e87577f07 Mon Sep 17 00:00:00 2001 From: Nathan Whitaker Date: Mon, 28 Oct 2024 14:21:21 -0700 Subject: [PATCH 1/3] add code to error thrown by `op_require_resolve_exports` --- ext/node/polyfills/01_require.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js index 5b0980c310f6cf..c091b396099a21 100644 --- a/ext/node/polyfills/01_require.js +++ b/ext/node/polyfills/01_require.js @@ -505,6 +505,7 @@ Module.globalPaths = modulePaths; const CHAR_FORWARD_SLASH = 47; const TRAILING_SLASH_REGEX = /(?:^|\/)\.?\.$/; +const ERROR_CODE_REGEX = /^\[([0-9a-zA-Z_]+)\]/; // This only applies to requests of a specific form: // 1. name/.* @@ -527,14 +528,24 @@ function resolveExports( return false; } - return op_require_resolve_exports( - usesLocalNodeModulesDir, - modulesPath, - request, - name, - expansion, - parentPath, - ) ?? false; + try { + return op_require_resolve_exports( + usesLocalNodeModulesDir, + modulesPath, + request, + name, + expansion, + parentPath ?? "", + ) ?? false; + } catch (error) { + if (error instanceof Error) { + const m = error.message.match(ERROR_CODE_REGEX); + if (m) { + error.code = m[1]; + } + } + throw error; + } } Module._findPath = function (request, paths, isMain, parentPath) { @@ -1087,7 +1098,6 @@ function loadESMFromCJS(module, filename, code) { url.pathToFileURL(filename).toString(), code, ); - module.exports = namespace; } From af79d34885669e4dbd4d4303c58e1ecabcb0e752 Mon Sep 17 00:00:00 2001 From: Nathan Whitaker Date: Tue, 29 Oct 2024 16:39:30 -0700 Subject: [PATCH 2/3] add todo comment --- ext/node/polyfills/01_require.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js index c091b396099a21..8afc422a76aee4 100644 --- a/ext/node/polyfills/01_require.js +++ b/ext/node/polyfills/01_require.js @@ -539,6 +539,8 @@ function resolveExports( ) ?? false; } catch (error) { if (error instanceof Error) { + // TODO(nathanwhit): properly throw an error with the `code` property set + // from rust once the error rework lands const m = error.message.match(ERROR_CODE_REGEX); if (m) { error.code = m[1]; @@ -1098,6 +1100,7 @@ function loadESMFromCJS(module, filename, code) { url.pathToFileURL(filename).toString(), code, ); + module.exports = namespace; } From d6e3c65352c5f4050933a9ca848e5894c3a7b58b Mon Sep 17 00:00:00 2001 From: Nathan Whitaker Date: Tue, 29 Oct 2024 16:42:43 -0700 Subject: [PATCH 3/3] cleaner regex --- ext/node/polyfills/01_require.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js index 8afc422a76aee4..b39c4655217e51 100644 --- a/ext/node/polyfills/01_require.js +++ b/ext/node/polyfills/01_require.js @@ -505,7 +505,7 @@ Module.globalPaths = modulePaths; const CHAR_FORWARD_SLASH = 47; const TRAILING_SLASH_REGEX = /(?:^|\/)\.?\.$/; -const ERROR_CODE_REGEX = /^\[([0-9a-zA-Z_]+)\]/; +const ERROR_CODE_REGEX = /^\[(\w+)\]/; // This only applies to requests of a specific form: // 1. name/.*