From 1473d67fcbb47a434a848abc5b7c444b0fd93f4a Mon Sep 17 00:00:00 2001 From: Ian Bull Date: Fri, 27 Sep 2024 13:55:51 -0700 Subject: [PATCH] refactor(ext/node): align error messages Aligns the error messages in the ext/node folder to be in-line with the Deno style guide. https://github.com/denoland/deno/issues/25269 --- ext/node/polyfills/_fs/_fs_writeFile.ts | 2 +- ext/node/polyfills/_util/std_asserts.ts | 4 ++-- ext/node/polyfills/_utils.ts | 2 +- ext/node/polyfills/internal/crypto/_randomBytes.ts | 2 +- ext/node/polyfills/internal/crypto/_randomInt.ts | 6 +++--- ext/node/polyfills/internal/crypto/cipher.ts | 4 ++-- ext/node/polyfills/internal/crypto/diffiehellman.ts | 2 +- ext/node/polyfills/internal/crypto/scrypt.ts | 4 ++-- ext/node/polyfills/internal/crypto/sig.ts | 8 ++++++-- ext/node/polyfills/path/_posix.ts | 2 +- ext/node/polyfills/path/_win32.ts | 4 ++-- ext/node/polyfills/v8.ts | 6 +++--- tests/node_compat/test/parallel/test-v8-serdes.js | 6 +++--- tests/unit_node/_fs/_fs_writeFile_test.ts | 2 +- 14 files changed, 29 insertions(+), 25 deletions(-) diff --git a/ext/node/polyfills/_fs/_fs_writeFile.ts b/ext/node/polyfills/_fs/_fs_writeFile.ts index dd324815b96dfc..e18240b78b9c29 100644 --- a/ext/node/polyfills/_fs/_fs_writeFile.ts +++ b/ext/node/polyfills/_fs/_fs_writeFile.ts @@ -42,7 +42,7 @@ export function writeFile( optOrCallback instanceof Function ? undefined : optOrCallback; if (!callbackFn) { - throw new TypeError("Callback must be a function."); + throw new TypeError("Callback must be a function"); } pathOrRid = pathOrRid instanceof URL ? pathFromURL(pathOrRid) : pathOrRid; diff --git a/ext/node/polyfills/_util/std_asserts.ts b/ext/node/polyfills/_util/std_asserts.ts index 78b749f549342d..a65cfdf40aba2c 100644 --- a/ext/node/polyfills/_util/std_asserts.ts +++ b/ext/node/polyfills/_util/std_asserts.ts @@ -120,7 +120,7 @@ export function equal(c: unknown, d: unknown): boolean { !(ObjectPrototypeIsPrototypeOf(WeakMapPrototype, a) && ObjectPrototypeIsPrototypeOf(WeakMapPrototype, b)) ) return false; - throw new TypeError("cannot compare WeakMap instances"); + throw new TypeError("Cannot compare WeakMap instances"); } if ( ObjectPrototypeIsPrototypeOf(WeakSetPrototype, a) || @@ -130,7 +130,7 @@ export function equal(c: unknown, d: unknown): boolean { !(ObjectPrototypeIsPrototypeOf(WeakSetPrototype, a) && ObjectPrototypeIsPrototypeOf(WeakSetPrototype, b)) ) return false; - throw new TypeError("cannot compare WeakSet instances"); + throw new TypeError("Cannot compare WeakSet instances"); } if (seen.get(a) === b) { return true; diff --git a/ext/node/polyfills/_utils.ts b/ext/node/polyfills/_utils.ts index b50c113e14f3eb..0734814354b4c6 100644 --- a/ext/node/polyfills/_utils.ts +++ b/ext/node/polyfills/_utils.ts @@ -179,7 +179,7 @@ export function validateIntegerRange( if (value < min || value > max) { throw new Error( - `${name} must be >= ${min} && <= ${max}. Value was ${value}`, + `${name} must be >= ${min} && <= ${max}: received ${value}`, ); } } diff --git a/ext/node/polyfills/internal/crypto/_randomBytes.ts b/ext/node/polyfills/internal/crypto/_randomBytes.ts index e1dc5c5ac8fc5d..ed8fdad71be39d 100644 --- a/ext/node/polyfills/internal/crypto/_randomBytes.ts +++ b/ext/node/polyfills/internal/crypto/_randomBytes.ts @@ -11,7 +11,7 @@ export const MAX_SIZE = 4294967295; function generateRandomBytes(size: number) { if (size > MAX_SIZE) { throw new RangeError( - `The value of "size" is out of range. It must be >= 0 && <= ${MAX_SIZE}. Received ${size}`, + `The value of "size" is out of range, it must be >= 0 && <= ${MAX_SIZE}: received ${size}`, ); } diff --git a/ext/node/polyfills/internal/crypto/_randomInt.ts b/ext/node/polyfills/internal/crypto/_randomInt.ts index 7f4d703ad46132..704f360b7fd74a 100644 --- a/ext/node/polyfills/internal/crypto/_randomInt.ts +++ b/ext/node/polyfills/internal/crypto/_randomInt.ts @@ -35,15 +35,15 @@ export default function randomInt( !Number.isSafeInteger(min) || typeof max === "number" && !Number.isSafeInteger(max) ) { - throw new Error("max or min is not a Safe Number"); + throw new Error('"max" or "min" is not a Safe Number'); } if (max - min > Math.pow(2, 48)) { - throw new RangeError("max - min should be less than 2^48!"); + throw new RangeError("max - min must be less than 2^48"); } if (min >= max) { - throw new Error("Min is bigger than Max!"); + throw new Error('"min" is bigger than "max"'); } min = Math.ceil(min); diff --git a/ext/node/polyfills/internal/crypto/cipher.ts b/ext/node/polyfills/internal/crypto/cipher.ts index c1c5ce8901bef4..3d2081bf583e4b 100644 --- a/ext/node/polyfills/internal/crypto/cipher.ts +++ b/ext/node/polyfills/internal/crypto/cipher.ts @@ -189,7 +189,7 @@ export class Cipheriv extends Transform implements Cipher { this.#needsBlockCache = !(cipher == "aes-128-gcm" || cipher == "aes-256-gcm"); if (this.#context == 0) { - throw new TypeError("Unknown cipher"); + throw new TypeError(`Unknown cipher: ${cipher}`); } } @@ -347,7 +347,7 @@ export class Decipheriv extends Transform implements Cipher { this.#needsBlockCache = !(cipher == "aes-128-gcm" || cipher == "aes-256-gcm"); if (this.#context == 0) { - throw new TypeError("Unknown cipher"); + throw new TypeError(`Unknown cipher: ${cipher}`); } } diff --git a/ext/node/polyfills/internal/crypto/diffiehellman.ts b/ext/node/polyfills/internal/crypto/diffiehellman.ts index a439306a9718b3..32498241bab3c8 100644 --- a/ext/node/polyfills/internal/crypto/diffiehellman.ts +++ b/ext/node/polyfills/internal/crypto/diffiehellman.ts @@ -1190,7 +1190,7 @@ export class ECDH { const c = ellipticCurves.find((x) => x.name == curve); if (c == undefined) { - throw new Error("invalid curve"); + throw new Error(`Invalid curve: ${curve}`); } this.#curve = c; diff --git a/ext/node/polyfills/internal/crypto/scrypt.ts b/ext/node/polyfills/internal/crypto/scrypt.ts index ce8649bbe2674f..e61b86d8e803b9 100644 --- a/ext/node/polyfills/internal/crypto/scrypt.ts +++ b/ext/node/polyfills/internal/crypto/scrypt.ts @@ -69,7 +69,7 @@ export function scryptSync( const blen = p * 128 * r; if (32 * r * (N + 2) * 4 + blen > maxmem) { - throw new Error("exceeds max memory"); + throw new Error('"scryptSync" exceeds max memory'); } const buf = Buffer.alloc(keylen); @@ -104,7 +104,7 @@ export function scrypt( const blen = p * 128 * r; if (32 * r * (N + 2) * 4 + blen > maxmem) { - throw new Error("exceeds max memory"); + throw new Error('"scryptSync" exceeds max memory'); } try { diff --git a/ext/node/polyfills/internal/crypto/sig.ts b/ext/node/polyfills/internal/crypto/sig.ts index a05f16478d93f1..56f9bebce73e48 100644 --- a/ext/node/polyfills/internal/crypto/sig.ts +++ b/ext/node/polyfills/internal/crypto/sig.ts @@ -259,7 +259,9 @@ export function signOneShot( let result: Buffer; if (op_node_get_asymmetric_key_type(handle) === "ed25519") { if (algorithm != null && algorithm !== "sha512") { - throw new TypeError("Only 'sha512' is supported for Ed25519 keys"); + throw new TypeError( + `Only 'sha512' is supported for Ed25519 keys: received '${algorithm}'`, + ); } result = new Buffer(64); op_node_sign_ed25519(handle, data, result); @@ -314,7 +316,9 @@ export function verifyOneShot( let result: boolean; if (op_node_get_asymmetric_key_type(handle) === "ed25519") { if (algorithm != null && algorithm !== "sha512") { - throw new TypeError("Only 'sha512' is supported for Ed25519 keys"); + throw new TypeError( + `Only 'sha512' is supported for Ed25519 keys: received '${algorithm}'`, + ); } result = op_node_verify_ed25519(handle, data, signature); } else if (algorithm == null) { diff --git a/ext/node/polyfills/path/_posix.ts b/ext/node/polyfills/path/_posix.ts index bf0b91d488bd8a..1b2373a5a9f1c5 100644 --- a/ext/node/polyfills/path/_posix.ts +++ b/ext/node/polyfills/path/_posix.ts @@ -39,7 +39,7 @@ export function resolve(...pathSegments: string[]): string { // deno-lint-ignore no-explicit-any const { Deno } = globalThis as any; if (typeof Deno?.cwd !== "function") { - throw new TypeError("Resolved a relative path without a CWD."); + throw new TypeError("Resolved a relative path without a CWD"); } path = Deno.cwd(); } diff --git a/ext/node/polyfills/path/_win32.ts b/ext/node/polyfills/path/_win32.ts index 11c82e0eeee855..2aa7c7d86a6fdc 100644 --- a/ext/node/polyfills/path/_win32.ts +++ b/ext/node/polyfills/path/_win32.ts @@ -46,14 +46,14 @@ export function resolve(...pathSegments: string[]): string { path = pathSegments[i]; } else if (!resolvedDevice) { if (typeof Deno?.cwd !== "function") { - throw new TypeError("Resolved a drive-letter-less path without a CWD."); + throw new TypeError("Resolved a drive-letter-less path without a CWD"); } path = Deno.cwd(); } else { if ( typeof Deno?.env?.get !== "function" || typeof Deno?.cwd !== "function" ) { - throw new TypeError("Resolved a relative path without a CWD."); + throw new TypeError("Resolved a relative path without a CWD"); } path = Deno.cwd(); diff --git a/ext/node/polyfills/v8.ts b/ext/node/polyfills/v8.ts index 5849f3ccc94afd..10bfefbb8decb1 100644 --- a/ext/node/polyfills/v8.ts +++ b/ext/node/polyfills/v8.ts @@ -102,7 +102,7 @@ export function serialize(value: any) { export function deserialize(buffer: Buffer | ArrayBufferView | DataView) { if (!isArrayBufferView(buffer)) { throw new TypeError( - "buffer must be a TypedArray or a DataView", + "Cannot deserialize: 'buffer' must be a 'TypedArray' or a 'DataView'", ); } const der = new DefaultDeserializer(buffer); @@ -141,7 +141,7 @@ export class Serializer { writeRawBytes(source: ArrayBufferView): void { if (!isArrayBufferView(source)) { throw new TypeError( - "source must be a TypedArray or a DataView", + "Cannot write bytes: 'source' must be a 'TypedArray' or a 'DataView'", ); } op_v8_write_raw_bytes(this[kHandle], source); @@ -169,7 +169,7 @@ export class Deserializer { constructor(buffer: ArrayBufferView) { if (!isArrayBufferView(buffer)) { throw new TypeError( - "buffer must be a TypedArray or a DataView", + "Cannot construct 'Deserializer': 'buffer' must be a 'TypedArray' or a 'DataView'", ); } this.buffer = buffer; diff --git a/tests/node_compat/test/parallel/test-v8-serdes.js b/tests/node_compat/test/parallel/test-v8-serdes.js index 175f5546c3f845..ba6001b00702e6 100644 --- a/tests/node_compat/test/parallel/test-v8-serdes.js +++ b/tests/node_compat/test/parallel/test-v8-serdes.js @@ -265,15 +265,15 @@ const objects = [ serializer.writeHeader(); assert.throws( () => serializer.writeRawBytes(INVALID_SOURCE), - /^TypeError: source must be a TypedArray or a DataView$/, + /^TypeError: Cannot write bytes: 'source' must be a 'TypedArray' or a 'DataView'$/, ); assert.throws( () => v8.deserialize(INVALID_SOURCE), - /^TypeError: buffer must be a TypedArray or a DataView$/, + /^TypeError: Cannot deserialize: 'buffer' must be a 'TypedArray' or a 'DataView'$/, ); assert.throws( () => new v8.Deserializer(INVALID_SOURCE), - /^TypeError: buffer must be a TypedArray or a DataView$/, + /^TypeError: Cannot construct 'Deserializer': 'buffer' must be a 'TypedArray' or a 'DataView'$/, ); } diff --git a/tests/unit_node/_fs/_fs_writeFile_test.ts b/tests/unit_node/_fs/_fs_writeFile_test.ts index 2733a2df0bdd86..b22de9dc84042c 100644 --- a/tests/unit_node/_fs/_fs_writeFile_test.ts +++ b/tests/unit_node/_fs/_fs_writeFile_test.ts @@ -31,7 +31,7 @@ Deno.test("Callback must be a function error", function fn() { writeFile("some/path", "some data", "utf8"); }, TypeError, - "Callback must be a function.", + "Callback must be a function", ); });