Skip to content

Commit

Permalink
errors: faster ERR_MODULE_NOT_FOUND error
Browse files Browse the repository at this point in the history
The function calls are more expensive than using the error printf
format style.

Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
BridgeAR committed Mar 2, 2023
1 parent ee8e40f commit 40ab9c3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
4 changes: 1 addition & 3 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1383,9 +1383,7 @@ E('ERR_MISSING_ARGS',
return `${msg} must be specified`;
}, TypeError);
E('ERR_MISSING_OPTION', '%s is required', TypeError);
E('ERR_MODULE_NOT_FOUND', (path, base, type = 'package') => {
return `Cannot find ${type} '${path}' imported from ${base}`;
}, Error);
E('ERR_MODULE_NOT_FOUND', "Cannot find %s '%s' imported from %s", Error);
E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times', Error);
E('ERR_NAPI_CONS_FUNCTION', 'Constructor must be a function', TypeError);
E('ERR_NAPI_INVALID_DATAVIEW_ARGS',
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/fetch_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function fetchWithRedirects(parsed) {
return entry;
}
if (res.statusCode === 404) {
const err = new ERR_MODULE_NOT_FOUND(parsed.href, null);
const err = new ERR_MODULE_NOT_FOUND('package', parsed.href, null);
err.message = `Cannot find module '${parsed.href}', HTTP 404`;
throw err;
}
Expand Down
9 changes: 6 additions & 3 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ function legacyMainResolve(packageJSONUrl, packageConfig, base) {
}
// Not found.
throw new ERR_MODULE_NOT_FOUND(
fileURLToPath(new URL('.', packageJSONUrl)), fileURLToPath(base));
'package',
fileURLToPath(new URL('.', packageJSONUrl)),
fileURLToPath(base),
);
}

const encodedSepRegEx = /%2F|%5C/i;
Expand Down Expand Up @@ -229,7 +232,7 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
process.send({ 'watch:require': [path || resolved.pathname] });
}
throw new ERR_MODULE_NOT_FOUND(
path || resolved.pathname, base && fileURLToPath(base), 'module');
'module', path || resolved.pathname, base && fileURLToPath(base));
}

if (!preserveSymlinks) {
Expand Down Expand Up @@ -791,7 +794,7 @@ function packageResolve(specifier, base, conditions) {

// eslint can't handle the above code.
// eslint-disable-next-line no-unreachable
throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base));
throw new ERR_MODULE_NOT_FOUND('package', packageName, fileURLToPath(base));
}

/**
Expand Down

0 comments on commit 40ab9c3

Please sign in to comment.