Skip to content

Commit

Permalink
bindings: make throwInvalidArgumentTypeValue print the value like the…
Browse files Browse the repository at this point in the history
… real ERR_INVALID_ARG_TYPE (#14804)
  • Loading branch information
nektro authored Nov 20, 2024
1 parent 363595f commit 61a3f08
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
5 changes: 2 additions & 3 deletions src/bun.js/bindings/bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3142,9 +3142,8 @@ pub const JSGlobalObject = opaque {
typename: []const u8,
value: JSValue,
) JSValue {
const ty_str = value.jsTypeString(this).toSlice(this, bun.default_allocator);
defer ty_str.deinit();
this.ERR_INVALID_ARG_TYPE("The \"{s}\" argument must be of type {s}. Received {}", .{ argname, typename, bun.fmt.quote(ty_str.slice()) }).throw();
var formatter = JSC.ConsoleObject.Formatter{ .globalThis = this };
this.ERR_INVALID_ARG_TYPE("The \"{s}\" argument must be of type {s}. Received {}", .{ argname, typename, value.toFmt(&formatter) }).throw();
return .zero;
}

Expand Down
11 changes: 8 additions & 3 deletions test/js/node/crypto/pbkdf2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,18 @@ testPBKDF2("password", "salt", 32, 32, "64c486c55d30d4c5a079b8823b7d7cb37ff0556f
testPBKDF2("", "", 1, 32, "f7ce0b653d2d72a4108cf5abe912ffdd777616dbbb27a70e8204f3ae2d0f6fad", "hex");

describe("invalid inputs", () => {
for (let input of ["test", {}, [], true, undefined, null]) {
for (let input of ["test", [], true, undefined, null]) {
test(`${input} is invalid`, () => {
expect(() => crypto.pbkdf2("pass", "salt", input, 8, "sha256")).toThrow(
`The "iteration count" argument must be of type integer. Received "${typeof input}"`,
`The "iteration count" argument must be of type integer. Received ${input}`,
);
});
}
test(`{} is invalid`, () => {
expect(() => crypto.pbkdf2("pass", "salt", {}, 8, "sha256")).toThrow(
`The "iteration count" argument must be of type integer. Received {}`,
);
});

test("invalid length", () => {
expect(() => crypto.pbkdf2("password", "salt", 1, -1, "sha256")).toThrow();
Expand Down Expand Up @@ -115,7 +120,7 @@ describe("invalid inputs", () => {
[Infinity, -Infinity, NaN].forEach(input => {
test(`${input} keylen`, () => {
expect(() => crypto.pbkdf2("password", "salt", 1, input, "sha256")).toThrow(
`The \"keylen\" argument must be of type integer. Received "number"`,
`The \"keylen\" argument must be of type integer. Received ${input}`,
);
});
});
Expand Down

0 comments on commit 61a3f08

Please sign in to comment.