From 520bf6c9beedf83916837848ec6164eddac2d41c Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 10 Dec 2024 11:13:40 -0500 Subject: [PATCH] LibWeb: Return a better error message for invalid byte strings We are currently returning LibJS's invalid code point message, but not formatting it with the bad value. So we get something like: Unhandled JavaScript exception: [TypeError] Invalid code point {}, must be an integer no less than 0 and no greater than 0x10FFFF So not only is the error unformatted, but it's inaccurate; in this case, the byte cannot be larger than 255. --- Libraries/LibWeb/WebIDL/AbstractOperations.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Libraries/LibWeb/WebIDL/AbstractOperations.cpp b/Libraries/LibWeb/WebIDL/AbstractOperations.cpp index 5476c6cc790..0054fb51194 100644 --- a/Libraries/LibWeb/WebIDL/AbstractOperations.cpp +++ b/Libraries/LibWeb/WebIDL/AbstractOperations.cpp @@ -7,6 +7,7 @@ */ #include +#include #include #include #include @@ -232,9 +233,9 @@ JS::ThrowCompletionOr to_byte_string(JS::VM& vm, JS::Value value) auto x = TRY(value.to_string(vm)); // 2. If the value of any element of x is greater than 255, then throw a TypeError. - for (auto character : x.code_points()) { + for (auto [i, character] : enumerate(x.code_points())) { if (character > 0xFF) - return vm.throw_completion(JS::ErrorType::InvalidCodePoint); + return vm.throw_completion(MUST(String::formatted("Invalid byte 0x{:X} at index {}, must be an integer no less than 0 and no greater than 0xFF", character, x.code_points().byte_offset_of(i)))); } // 3. Return an IDL ByteString value whose length is the length of x, and where the value of each element is the value of the corresponding element of x.