From 4512a048201495b85b221e3d05d064bce6c46d0e Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Thu, 28 Mar 2024 22:06:40 -0700 Subject: [PATCH] Add missing `code` to TextDecoder `"Invalid byte sequence"` error (#9700) * Fix missing `ERR_ENCODING_INVALID_ENCODED_DATA` code in TextDecoder * Update text-decoder.test.js --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/bun.js/webcore/encoding.zig | 7 +++---- test/js/web/encoding/text-decoder.test.js | 9 ++++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/bun.js/webcore/encoding.zig b/src/bun.js/webcore/encoding.zig index 9dca159a0c5ef3..c47454687c51b8 100644 --- a/src/bun.js/webcore/encoding.zig +++ b/src/bun.js/webcore/encoding.zig @@ -652,10 +652,9 @@ pub const TextDecoder = struct { } else |err| { switch (err) { error.InvalidByteSequence => { - globalThis.throwValue( - globalThis.createTypeErrorInstance("Invalid byte sequence", .{}), - ); - return JSValue.zero; + const type_error = globalThis.createErrorInstanceWithCode(.ERR_ENCODING_INVALID_ENCODED_DATA, "Invalid byte sequence", .{}); + globalThis.throwValue(type_error); + return .zero; }, error.OutOfMemory => { globalThis.throwOutOfMemory(); diff --git a/test/js/web/encoding/text-decoder.test.js b/test/js/web/encoding/text-decoder.test.js index 3685a5f6da5e4a..a5367752e69c3d 100644 --- a/test/js/web/encoding/text-decoder.test.js +++ b/test/js/web/encoding/text-decoder.test.js @@ -230,7 +230,14 @@ describe("TextDecoder", () => { const decoder = new TextDecoder("utf-8", { fatal: true }); expect(() => { decoder.decode(new Uint8Array([0xc0])); // Invalid UTF8 - }).toThrow(TypeError); + }).toThrow(Error); + let err; + try { + decoder.decode(new Uint8Array([0xc0, 0x80])); // Invalid UTF8 + } catch (e) { + err = e; + } + expect(err.code).toBe("ERR_ENCODING_INVALID_ENCODED_DATA"); }); it("should not trim invalid byte sequences when fatal is false", () => {