Skip to content

jsg: Match C++ JSG's TypeError messages in Rust JSG - #8754

Merged
nathanwhit merged 2 commits into
mainfrom
nathanwhit/rust-jsg-error-messages
Nov 25, 2026
Merged

nathanwhit merged 2 commits into
mainfrom
nathanwhit/rust-jsg-error-messages

Conversation

@nathanwhit

@nathanwhit nathanwhit commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Two fixes that make Rust JSG's TypeError messages match C++ JSG's, split out of #8529.

  1. jsg: Throw C++ JSG's Illegal invocation message from Rust callbacks. Accessors, inspect properties, wildcard interceptors and Fast API methods threw a bare "Illegal invocation", where C++ JSG and V8 add a link to the docs. jsg::Error::new_illegal_invocation() has C++ JSG's message, and each of those callbacks throws it.
  2. jsg: Name jsg::JsString 'String' in type errors, as C++ JSG does. C++ names a jsg::JsString parameter 'String' and a kj::String one 'string'. Rust JSG named both 'string'.

Compatibility

No compatibility flag. The first commit changes only the message of a TypeError, to the one C++ JSG and V8 already throw for the same mistake. The second changes nothing Workers can see, since no Rust API exposed to them takes a jsg::JsString yet.

Testing

resource_this checks the whole Illegal invocation message for methods and accessors, and js_string checks the 'String' name. The Rust JSG tests and compile tests pass.

@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 f3a28743.

This review, of 6933b029

✅ Approved — No issues found. The Illegal invocation message matches C++ JSG's kIllegalInvocation exactly, and the JsString rename only changes the text of the type error.

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


✅ All checks passed

📋 Code Quality: ✅ LGTM

new_illegal_invocation() matches C++ JSG's kIllegalInvocation (src/workerd/jsg/resource.h:42-44). Every throw site that used the bare message has been updated, and the tests check the full message. The JsString::class_name() change affects only the type-error text, and the js_string test covers it.


📝 Documentation: ✅ LGTM

The doc comments match the code. No documentation needs updating.


Reviewed commit: 6933b029

A Rust JSG callback that V8 calls without a signature check (an
accessor, an inspect property, a wildcard interceptor, a Fast API
method) and that receives a wrong `this` threw "Illegal invocation".
C++ JSG, and V8's signature check as patched in patches/v8, throw
"Illegal invocation: function called with incorrect `this` reference.
See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors
for details." So a method and an accessor of the same Rust resource
threw different messages.

Add jsg::Error::new_illegal_invocation(), with C++ JSG's message, and
throw it from each of those callbacks. resource_this's
wrong_this_throws_a_type_error now checks the whole message, for
methods and accessors alike.

Compatibility: no compatibility flag. Only the message of a TypeError
changes, to the one C++ JSG and V8 already throw for the same mistake.
A missing jsg::JsString argument threw "parameter 1 is not of type
'string'". C++ JSG names a jsg::JsString parameter 'String', after
v8::String, and a kj::String one 'string', so the Rust error differed
from the C++ one for the same parameter. Name jsg::JsString 'String';
a String parameter stays 'string', as for kj::String.

Compatibility: no change to the API surface exposed to Workers, none
of whose Rust APIs takes a jsg::JsString yet.
@nathanwhit
nathanwhit force-pushed the nathanwhit/rust-jsg-error-messages branch from 6933b02 to f3a2874 Compare November 23, 2026 19:35

@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 Illegal invocation message now matches C++ JSG's in every Rust callback that threw the bare text, and the JsString rename only changes the text of the type error.

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

Only the text of two TypeErrors changes. No check, control flow or input handling changes. The new message is a fixed string, and the throw sites still return early as they did before.


📋 Code Quality: ✅ LGTM

Every Rust throw site that used the bare "Illegal invocation" now calls Error::new_illegal_invocation(): the accessor and method paths, the constructor path, the wildcard interceptor and Fast API. Tests that check contains("Illegal invocation") still pass with the longer message. JsString::class_name() now returns "String". It is used only for the type-error text, and js_string.rs covers it.


📝 Documentation: ✅ LGTM

The doc comments match the code, and no documentation needs updating.


Reviewed commit: f3a28743

@nathanwhit
nathanwhit merged commit f4db330 into main Nov 25, 2026
35 of 39 checks passed
@nathanwhit
nathanwhit deleted the nathanwhit/rust-jsg-error-messages branch November 25, 2026 18:43
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