Skip to content

Name the field or index in Rust JSG struct field and array element errors - #8632

Merged
littledivy merged 1 commit into
mainfrom
littledivy/2026-11-18-rjsg-struct-field-errors
Nov 19, 2026
Merged

littledivy merged 1 commit into
mainfrom
littledivy/2026-11-18-rjsg-struct-field-errors

Conversation

@littledivy

@littledivy littledivy commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

When a #[jsg_struct] field or a Vec element had the wrong type, Rust JSG failed with the inner conversion's error and didn't say where it happened: { list: 1 } gave "Expected Array but got number". C++ JSG names the field or the index: "Incorrect type for the 'list' field on 'Outer': the provided value is not of type 'Array'." and "Incorrect type for array element 1: ...". I ran the same values through a C++ method and a Rust method with the same signature, and this accounted for about 70 of the 100 message diffs.

In this patch, struct fields and Vec elements convert with try_from_js. A value that isn't of the expected type gets C++ JSG's message, built by one helper, Error::new_incorrect_type. Dict entries and method arguments now use it too. An error thrown during conversion still passes through unchanged. A struct's from_js now goes through its try_from_js, so a nested struct no longer checks the value's type twice. The README also notes two differences that remain:

  • A Rust String is a USVString, so a lone surrogate comes back as one U+FFFD. C++ JSG's WTF-8 kj::String gives three.
  • If script shortens an array during conversion, Vec follows the new length. C++ JSG reads the length once.

@littledivy
littledivy requested review from a team as code owners November 18, 2026 18:28

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superseded by the review of f2b0c834.

This review, of b9e89302

✅ Approved — No issues found: the field and index messages match C++ JSG's, errors thrown during conversion still pass through unchanged, and the new messages are covered by tests.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No issues found. The error messages hold only compile-time field and class names and numeric indexes. A value of the wrong type is still rejected.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found.


⚡ Performance: ✅ LGTM

No issues found.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: b9e89302

@guybedford guybedford left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I checked that every try_from_js override accepts exactly what its from_js does, so this only changes messages, and the three messages match typeErrorMessage() in jsg/util.c++. A few nits inline, none blocking.

Comment thread src/rust/jsg/wrappable.rs Resolved
Comment thread src/rust/jsg/lib.rs Resolved
Comment thread src/rust/jsg-macros/lib.rs Resolved
…rors

A `#[jsg_struct]` field or a `Vec` element of the wrong type failed with the error of the
inner conversion, without saying where: `{ list: 1 }` gave "Expected Array but got number".
C++ JSG names the field or the index: "Incorrect type for the 'list' field on 'Outer': the
provided value is not of type 'Array'." and "Incorrect type for array element 1: ...". In a
differential run of the same values through C++ and Rust methods, this made about 70 of 100
message diffs.

In this patch the struct macro and `Vec::try_from_js` convert with `try_from_js` and report a
value that isn't of the type with C++ JSG's message, the way `Dict` entries and method
arguments already do. An error thrown while converting passes through unchanged. The README
also notes two differences that stay: a Rust `String` is a USVString, so a lone surrogate
comes back as one U+FFFD where C++ JSG's WTF-8 `kj::String` gives three, and `Vec`
conversion follows the array's current length when script shortens it.
@littledivy
littledivy force-pushed the littledivy/2026-11-18-rjsg-struct-field-errors branch from b9e8930 to f2b0c83 Compare November 19, 2026 06:41

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Approved — No issues found: the approver's three nits are fixed, the field, index and argument messages are built in one place and match C++ JSG, and errors thrown during conversion still pass through unchanged.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No issues found. The messages contain only compile-time field and class names, numeric indexes, and the Dict key, which earlier messages already included. A value of the wrong type is still rejected.


📖 AGENTS.md: ✅ LGTM

No issues found.


📋 Code Quality: ✅ LGTM

No issues found. The try_from_js doc, the method-argument message (now TypeErrorContext::MethodArgument) and the single source for the field name are all fixed at this commit. These paths behave correctly: Option fields, Vec holes and length changes, and coercing primitives.


⚡ Performance: ✅ LGTM

No issues found. Messages are built only when conversion fails (#[cold]). A nested struct now checks the value's type once instead of twice.


📝 Documentation: ✅ LGTM

No issues found.


Reviewed commit: f2b0c834

@littledivy
littledivy merged commit e0c0872 into main Nov 19, 2026
24 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-18-rjsg-struct-field-errors branch November 19, 2026 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants