Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3ce51ae

Browse filesBrowse files
authored
module: harmonize error code between ESM and CJS
PR-URL: #48606 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 586fcff commit 3ce51ae
Copy full SHA for 3ce51ae

File tree

Expand file treeCollapse file tree

2 files changed

+10
-16
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+10
-16
lines changed
Open diff view settings
Collapse file

‎lib/internal/modules/package_json_reader.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/package_json_reader.js
+9-15Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {
1010
} = require('internal/errors').codes;
1111
const { internalModuleReadJSON } = internalBinding('fs');
1212
const { toNamespacedPath } = require('path');
13-
const { kEmptyObject } = require('internal/util');
13+
const { kEmptyObject, setOwnProperty } = require('internal/util');
1414

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

@@ -73,20 +73,14 @@ function read(jsonPath, { base, specifier, isESM } = kEmptyObject) {
7373
let parsed;
7474
try {
7575
parsed = JSONParse(string);
76-
} catch (error) {
77-
if (isESM) {
78-
throw new ERR_INVALID_PACKAGE_CONFIG(
79-
jsonPath,
80-
(base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier),
81-
error.message,
82-
);
83-
} else {
84-
// For backward compat, we modify the error returned by JSON.parse rather than creating a new one.
85-
// TODO(aduh95): make it throw ERR_INVALID_PACKAGE_CONFIG in a semver-major with original error as cause
86-
error.message = 'Error parsing ' + jsonPath + ': ' + error.message;
87-
error.path = jsonPath;
88-
throw error;
89-
}
76+
} catch (cause) {
77+
const error = new ERR_INVALID_PACKAGE_CONFIG(
78+
jsonPath,
79+
isESM && (base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier),
80+
cause.message,
81+
);
82+
setOwnProperty(error, 'cause', cause);
83+
throw error;
9084
}
9185

9286
result.exists = true;
Collapse file

‎test/sequential/test-module-loading.js‎

Copy file name to clipboardExpand all lines: test/sequential/test-module-loading.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ assert.throws(
128128

129129
assert.throws(
130130
function() { require('../fixtures/packages/unparseable'); },
131-
/^SyntaxError: Error parsing/
131+
{ code: 'ERR_INVALID_PACKAGE_CONFIG' }
132132
);
133133

134134
{

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.