diff --git a/src/bun.js/bindings/ErrorCode.cpp b/src/bun.js/bindings/ErrorCode.cpp index 69fe35cd5f..5a0e41286e 100644 --- a/src/bun.js/bindings/ErrorCode.cpp +++ b/src/bun.js/bindings/ErrorCode.cpp @@ -367,7 +367,7 @@ WTF::String ERR_INVALID_ARG_TYPE(JSC::ThrowScope& scope, JSC::JSGlobalObject* gl result.append("The "_s); - if (arg_name.contains(' ')) { + if (arg_name.endsWith(" argument"_s)) { result.append(arg_name); } else { result.append("\""_s); @@ -379,13 +379,17 @@ WTF::String ERR_INVALID_ARG_TYPE(JSC::ThrowScope& scope, JSC::JSGlobalObject* gl unsigned length = expected_types.size(); if (length == 1) { result.append(expected_types.at(0).toWTFString(globalObject)); + } else if (length == 2) { + result.append(expected_types.at(0).toWTFString(globalObject)); + result.append(" or "_s); + result.append(expected_types.at(1).toWTFString(globalObject)); } else { for (unsigned i = 0; i < length - 1; i++) { JSValue expected_type = expected_types.at(i); - if (i > 0) result.append(", "_s); result.append(expected_type.toWTFString(globalObject)); + result.append(", "_s); } - result.append(" or "_s); + result.append("or "_s); result.append(expected_types.at(length - 1).toWTFString(globalObject)); } @@ -1021,6 +1025,17 @@ JSC_DEFINE_HOST_FUNCTION(Bun::jsFunctionMakeErrorWithCode, (JSC::JSGlobalObject return JSC::JSValue::encode(createError(globalObject, error, message)); } + case ErrorCode::ERR_BUFFER_OUT_OF_BOUNDS: { + auto arg0 = callFrame->argument(1); + if (!arg0.isUndefined()) { + auto str0 = arg0.toWTFString(globalObject); + RETURN_IF_EXCEPTION(scope, {}); + auto message = makeString("\""_s, str0, "\" is outside of buffer bounds"_s); + return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_BUFFER_OUT_OF_BOUNDS, message)); + } + return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_BUFFER_OUT_OF_BOUNDS, "Attempt to access memory outside buffer bounds"_s)); + } + case ErrorCode::ERR_IPC_DISCONNECTED: return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_IPC_DISCONNECTED, "IPC channel is already disconnected"_s)); case ErrorCode::ERR_SERVER_NOT_RUNNING: @@ -1031,8 +1046,6 @@ JSC_DEFINE_HOST_FUNCTION(Bun::jsFunctionMakeErrorWithCode, (JSC::JSGlobalObject return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_SOCKET_BAD_TYPE, "Bad socket type specified. Valid types are: udp4, udp6"_s)); case ErrorCode::ERR_ZLIB_INITIALIZATION_FAILED: return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_ZLIB_INITIALIZATION_FAILED, "Initialization failed"_s)); - case ErrorCode::ERR_BUFFER_OUT_OF_BOUNDS: - return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_BUFFER_OUT_OF_BOUNDS, "Attempt to access memory outside buffer bounds"_s)); case ErrorCode::ERR_IPC_ONE_PIPE: return JSC::JSValue::encode(createError(globalObject, ErrorCode::ERR_IPC_ONE_PIPE, "Child process can have only one IPC pipe"_s)); case ErrorCode::ERR_SOCKET_ALREADY_BOUND: diff --git a/src/js/node/child_process.ts b/src/js/node/child_process.ts index 2a719ba727..a1fe447c8c 100644 --- a/src/js/node/child_process.ts +++ b/src/js/node/child_process.ts @@ -553,7 +553,7 @@ function spawnSync(file, args, options) { } else if (typeof input === "string") { bunStdio[0] = Buffer.from(input, encoding || "utf8"); } else { - throw $ERR_INVALID_ARG_TYPE(`options.stdio[0]`, ["Buffer", "TypedArray", "DataView", "string"], input); + throw $ERR_INVALID_ARG_TYPE(`options.stdio[0]`, ["string", "Buffer", "TypedArray", "DataView"], input); } } diff --git a/src/js/node/dgram.ts b/src/js/node/dgram.ts index e68678c2ea..2ae58c42df 100644 --- a/src/js/node/dgram.ts +++ b/src/js/node/dgram.ts @@ -456,7 +456,7 @@ function sliceBuffer(buffer, offset, length) { if (typeof buffer === "string") { buffer = Buffer.from(buffer); } else if (!ArrayBuffer.isView(buffer)) { - throw $ERR_INVALID_ARG_TYPE("buffer", ["Buffer", "TypedArray", "DataView", "string"], buffer); + throw $ERR_INVALID_ARG_TYPE("buffer", ["string", "Buffer", "TypedArray", "DataView"], buffer); } offset = offset >>> 0; @@ -562,12 +562,12 @@ Socket.prototype.send = function (buffer, offset, length, port, address, callbac if (typeof buffer === "string") { list = [Buffer.from(buffer)]; } else if (!ArrayBuffer.isView(buffer)) { - throw $ERR_INVALID_ARG_TYPE("buffer", ["Buffer", "TypedArray", "DataView", "string"], buffer); + throw $ERR_INVALID_ARG_TYPE("buffer", ["string", "Buffer", "TypedArray", "DataView"], buffer); } else { list = [buffer]; } } else if (!(list = fixBufferList(buffer))) { - throw $ERR_INVALID_ARG_TYPE("buffer list arguments", ["Buffer", "TypedArray", "DataView", "string"], buffer); + throw $ERR_INVALID_ARG_TYPE("buffer list arguments", ["string", "Buffer", "TypedArray", "DataView"], buffer); } if (!connected) port = validatePort(port, "Port", false); diff --git a/test/js/node/child_process/child_process-node.test.js b/test/js/node/child_process/child_process-node.test.js index 710eb5be67..c4ca225ebc 100644 --- a/test/js/node/child_process/child_process-node.test.js +++ b/test/js/node/child_process/child_process-node.test.js @@ -657,7 +657,7 @@ describe("fork", () => { code: "ERR_INVALID_ARG_TYPE", name: "TypeError", message: expect.stringContaining( - `The "modulePath" argument must be of type string, Buffer or URL. Received `, + `The "modulePath" argument must be of type string, Buffer, or URL. Received `, ), }), ); diff --git a/test/js/node/test/parallel/needs-test/test-assert.js b/test/js/node/test/parallel/needs-test/test-assert.js index a18ee87a38..aa302f1f74 100644 --- a/test/js/node/test/parallel/needs-test/test-assert.js +++ b/test/js/node/test/parallel/needs-test/test-assert.js @@ -1049,7 +1049,7 @@ test('Additional asserts', () => { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', // message: 'The "error" argument must be of type Object, Error, Function or RegExp. Received: "Error message"', - message: 'The "error" argument must be of type Object, Error, Function or RegExp.' + invalidArgTypeHelper('Error message'), + message: 'The "error" argument must be of type Object, Error, Function, or RegExp.' + invalidArgTypeHelper('Error message'), } ); @@ -1059,7 +1059,7 @@ test('Additional asserts', () => { () => assert.throws(() => {}, input), { code: 'ERR_INVALID_ARG_TYPE', - message: 'The "error" argument must be of type Object, Error, Function or RegExp.' + invalidArgTypeHelper(input) + message: 'The "error" argument must be of type Object, Error, Function, or RegExp.' + invalidArgTypeHelper(input) } ); @@ -1318,7 +1318,7 @@ test('Additional assert', () => { '\n' + "+ 'test test'\n" + "- 'test foobar'\n" + - ' ^\n', + ' ^\n', } ); @@ -1597,5 +1597,3 @@ test('assert/strict exists', () => { /* eslint-enable no-restricted-syntax */ /* eslint-enable no-restricted-properties */ - - diff --git a/test/js/node/test/parallel/test-dgram-send-bad-arguments.js b/test/js/node/test/parallel/test-dgram-send-bad-arguments.js index 83151538a4..7257a0aca6 100644 --- a/test/js/node/test/parallel/test-dgram-send-bad-arguments.js +++ b/test/js/node/test/parallel/test-dgram-send-bad-arguments.js @@ -35,7 +35,7 @@ function checkArgs(connected) { { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', - message: /The "buffer" argument must be of type (string or an instance of Buffer, TypedArray, or DataView|Buffer, TypedArray, DataView or string)\. Received undefined/ + message: 'The "buffer" argument must be of type string, Buffer, TypedArray, or DataView. Received undefined' } ); @@ -95,7 +95,7 @@ function checkArgs(connected) { { code: 'ERR_BUFFER_OUT_OF_BOUNDS', name: 'RangeError', - message: /"offset" is outside of buffer bounds|Attempt to access memory outside buffer bounds/, + message: '"offset" is outside of buffer bounds', } ); @@ -104,7 +104,7 @@ function checkArgs(connected) { { code: 'ERR_BUFFER_OUT_OF_BOUNDS', name: 'RangeError', - message: /"length" is outside of buffer bounds|Attempt to access memory outside buffer bounds/, + message: '"length" is outside of buffer bounds', } ); @@ -113,7 +113,7 @@ function checkArgs(connected) { { code: 'ERR_BUFFER_OUT_OF_BOUNDS', name: 'RangeError', - message: /"length" is outside of buffer bounds|Attempt to access memory outside buffer bounds/, + message: '"length" is outside of buffer bounds', } ); } @@ -129,7 +129,7 @@ function checkArgs(connected) { { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', - message: /The "buffer" argument must be of type (string or an instance of Buffer, TypedArray, or DataView|Buffer, TypedArray, DataView or string)\. Received type number \(23\)/, + message: 'The "buffer" argument must be of type string, Buffer, TypedArray, or DataView. Received type number (23)' } ); @@ -139,7 +139,8 @@ function checkArgs(connected) { { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', - message: /The "?buffer list arguments(" argument)? must be of type (string or an instance of Buffer, TypedArray, or DataView|Buffer, TypedArray, DataView or string)\. Received an instance of Array/ + message: 'The "buffer list arguments" argument must be of type string, Buffer, TypedArray, or DataView. ' + + 'Received an instance of Array' } ); }