Skip to content

Commit

Permalink
Add missing code to TextDecoder "Invalid byte sequence" error (#9700
Browse files Browse the repository at this point in the history
)

* Fix missing `ERR_ENCODING_INVALID_ENCODED_DATA` code in TextDecoder

* Update text-decoder.test.js

---------

Co-authored-by: Jarred Sumner <[email protected]>
  • Loading branch information
Jarred-Sumner and Jarred-Sumner authored Mar 29, 2024
1 parent 1ad6a3d commit 4512a04
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/bun.js/webcore/encoding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 8 additions & 1 deletion test/js/web/encoding/text-decoder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 4512a04

Please sign in to comment.