diff --git a/lib/internal/fs/recursive_watch.js b/lib/internal/fs/recursive_watch.js index 38c0505797c3ad..29d8c23fdfbe31 100644 --- a/lib/internal/fs/recursive_watch.js +++ b/lib/internal/fs/recursive_watch.js @@ -68,7 +68,7 @@ class FSWatcher extends EventEmitter { if (encoding != null) { // This is required since on macOS and Windows it throws ERR_INVALID_ARG_VALUE if (typeof encoding !== 'string') { - throw new ERR_INVALID_ARG_VALUE(encoding, 'options.encoding'); + throw new ERR_INVALID_ARG_VALUE('options.encoding', encoding); } } diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 53644740144570..fc9a390f812a5e 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -153,7 +153,7 @@ function lazyLoadFs() { function assertEncoding(encoding) { if (encoding && !Buffer.isEncoding(encoding)) { const reason = 'is invalid encoding'; - throw new ERR_INVALID_ARG_VALUE(encoding, 'encoding', reason); + throw new ERR_INVALID_ARG_VALUE('encoding', encoding, reason); } } diff --git a/lib/internal/fs/watchers.js b/lib/internal/fs/watchers.js index 411eab8136d595..d1d8bcf372631f 100644 --- a/lib/internal/fs/watchers.js +++ b/lib/internal/fs/watchers.js @@ -318,7 +318,7 @@ async function* watch(filename, options = kEmptyObject) { if (encoding && !isEncoding(encoding)) { const reason = 'is invalid encoding'; - throw new ERR_INVALID_ARG_VALUE(encoding, 'encoding', reason); + throw new ERR_INVALID_ARG_VALUE('encoding', encoding, reason); } if (signal?.aborted) diff --git a/lib/internal/quic/quic.js b/lib/internal/quic/quic.js index afe057de5bd951..5b96dbc80be92c 100644 --- a/lib/internal/quic/quic.js +++ b/lib/internal/quic/quic.js @@ -2069,7 +2069,7 @@ function processSessionOptions(options, forServer = false) { if (cc !== undefined) { validateString(cc, 'options.cc'); if (cc !== 'reno' || cc !== 'bbr' || cc !== 'cubic') { - throw new ERR_INVALID_ARG_VALUE(cc, 'options.cc'); + throw new ERR_INVALID_ARG_VALUE('options.cc', cc); } } diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 4e99d56aa4be7e..662eba6fbdf409 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -529,7 +529,7 @@ function newStreamReadableFromReadableStream(readableStream, options = kEmptyObj } = options; if (encoding !== undefined && !Buffer.isEncoding(encoding)) - throw new ERR_INVALID_ARG_VALUE(encoding, 'options.encoding'); + throw new ERR_INVALID_ARG_VALUE('options.encoding', encoding); validateBoolean(objectMode, 'options.objectMode'); const reader = readableStream.getReader(); @@ -686,7 +686,7 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options = validateBoolean(objectMode, 'options.objectMode'); if (encoding !== undefined && !Buffer.isEncoding(encoding)) - throw new ERR_INVALID_ARG_VALUE(encoding, 'options.encoding'); + throw new ERR_INVALID_ARG_VALUE('options.encoding', encoding); const writer = writableStream.getWriter(); const reader = readableStream.getReader(); diff --git a/test/parallel/test-fs-internal-assertencoding.js b/test/parallel/test-fs-internal-assertencoding.js new file mode 100644 index 00000000000000..ea88517b908861 --- /dev/null +++ b/test/parallel/test-fs-internal-assertencoding.js @@ -0,0 +1,15 @@ +'use strict'; + +// Tests to verify that a correctly formatted invalid encoding error +// is thrown. Originally the internal assertEncoding utility was +// reversing the arguments when constructing the ERR_INVALID_ARG_VALUE +// error. + +require('../common'); +const { opendirSync } = require('node:fs'); +const { throws } = require('node:assert'); + +throws(() => opendirSync('.', { encoding: 'no' }), { + code: 'ERR_INVALID_ARG_VALUE', + message: 'The argument \'encoding\' is invalid encoding. Received \'no\'', +});