Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: harmonize error code between ESM and CJS #48606

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions lib/internal/modules/package_json_reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
} = require('internal/errors').codes;
const { internalModuleReadJSON } = internalBinding('fs');
const { toNamespacedPath } = require('path');
const { kEmptyObject } = require('internal/util');
const { kEmptyObject, setOwnProperty } = require('internal/util');

const { fileURLToPath, pathToFileURL } = require('internal/url');

Expand Down Expand Up @@ -73,20 +73,14 @@ function read(jsonPath, { base, specifier, isESM } = kEmptyObject) {
let parsed;
try {
parsed = JSONParse(string);
} catch (error) {
if (isESM) {
throw new ERR_INVALID_PACKAGE_CONFIG(
jsonPath,
(base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier),
error.message,
);
} else {
// For backward compat, we modify the error returned by JSON.parse rather than creating a new one.
// TODO(aduh95): make it throw ERR_INVALID_PACKAGE_CONFIG in a semver-major with original error as cause
error.message = 'Error parsing ' + jsonPath + ': ' + error.message;
error.path = jsonPath;
throw error;
}
} catch (cause) {
const error = new ERR_INVALID_PACKAGE_CONFIG(
jsonPath,
isESM && (base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get rid of isESM?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe? If I simply remove it, it doesn't work because both base and specifier are undefined, which doesn't work for fileURLToPath. Why do you want to get rid of it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After #48605, this isESM check will be the only distinction between loaders. I'm just trying to make them consistent

cause.message,
);
setOwnProperty(error, 'cause', cause);
throw error;
}

result.exists = true;
Expand Down
2 changes: 1 addition & 1 deletion test/sequential/test-module-loading.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be for a separate issue/PR, but does anyone know of a reason why some of the negative module loading tests are in this file, while others are in test/parallel/test-module-loading-error.js?

Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ assert.throws(

assert.throws(
function() { require('../fixtures/packages/unparseable'); },
/^SyntaxError: Error parsing/
{ code: 'ERR_INVALID_PACKAGE_CONFIG' }
);

{
Expand Down