Skip to content

Fail a Rust worker interface with the kj::Exception itself - #8728

Merged
bartlomieju merged 1 commit into
mainfrom
biwanczuk/rust-worker-kj-exception
Nov 21, 2026
Merged

bartlomieju merged 1 commit into
mainfrom
biwanczuk/rust-worker-kj-exception

Conversation

@bartlomieju

@bartlomieju bartlomieju commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

A Rust worker::Interface or kj::http::Service fails with a
KjError, which keeps only an exception's type, description, location
and details. A C++ exception that passes through one, such as from a
C++ worker wrapped by CxxWorkerInterface, loses its context, remote
trace and stack trace. Rust interfaces should be able to stand in for
C++ ones without that loss, which porting
WorkerInterface::fromException() and similar needs.

The first commit lets a KjException be cloned with
kj::Exception::clone(). The second makes KjException the error of
worker::Result and of Service::request() and connect(), spelled
kj::http::ServiceResult. Code that builds a KjError converts it at
the trait method with ?. The kj crate's own wrappers, such as
ServiceResponse::send(), and the Rust server's listener logging still
use KjError; moving those is a separate change.

A Worker sees no difference, since JavaScript still gets only the type
and description; C++ callers now get the full exception. A new C++ test
fails all eight events of a wrapped C++ worker with an exception that
carries a context, a remote trace and a stack trace, and checks each
comes back whole. The PromisedInterface tests check the same for the
clone every later event gets. Both fail when the exception is reduced to
a KjError. The full test suite passes.

@bartlomieju
bartlomieju requested review from a team as code owners November 20, 2026 09:37

@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 9bb840ea.

This review, of 3900b4c8

✅ Approved — I found no problems. The clone bridge pairs new with the existing delete drop, and the trait error-type change is converted the same way at every implementation and listener.

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

I found no security concerns. What reaches JavaScript is still only the exception's type and description, and the extra context and traces only show up in server-side logs.


📖 AGENTS.md: ✅ LGTM

Nothing in this pull request makes the existing contributor guidance wrong.


📋 Code Quality: ✅ LGTM

I found no correctness or maintainability problems. KjException::clone() goes through kj::Exception::clone(). The memory it allocates with new is freed by the existing dropInPlace delete. The KjError → KjException conversions at the trait boundaries in kj-hyper, the worker crate and the server services are applied the same way everywhere.


⚡ Performance: ✅ LGTM

The change only affects error paths: at most one exception clone per failure. It adds no cost to successful requests.


📝 Documentation: ✅ LGTM

The new doc comments match the behaviour.


Reviewed commit: 3900b4c8

@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 9ab77185.

This review, of 9bb840ea

✅ Approved — No findings. The clone bridge pairs new with the existing delete drop, and the KjError to KjException conversions are applied the same way at every trait implementation and listener.

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

I found no security concerns. What reaches JavaScript is still only the exception's type and description; the extra context and traces only reach C++ callers and server-side logs.


📖 AGENTS.md: ✅ LGTM

Nothing in this pull request makes the existing contributor guidance out of date.


📋 Code Quality: ✅ LGTM

I found no problems.

  • Clone bridge: cxxbridge1$kjException$clone creates the copy with new kj::Exception(err->clone()), and the existing dropInPlace frees it with delete, so the two match. test_kj_exception_clone checks that the copy is a separate allocation and that the type, description, location and details carry over.
  • PromisedInterface::new: IntoKjException is implemented for KjException, for KjError and for any std::error::Error. These impls don't overlap, because KjException is not an Error.
  • Trait boundaries: every KjError is turned into a KjException at the trait method, with .into() or ?. The helper methods behind them (the kj-hyper client and the disk, bucket and cache services) still return KjError.
  • Tests: the new C++ test checks all eight events through CxxWorkerInterface, request() and connect() included. The PromisedInterface tests now check that the context, remote trace and stack trace survive the clone.

⚡ Performance: ✅ LGTM

Only error paths change. The extra work is one kj::Exception::clone() per failed event on PromisedInterface, plus the conversions at the trait boundaries. Successful requests cost nothing extra.


📝 Documentation: ✅ LGTM

The new doc comments match the behaviour.


Reviewed commit: 9bb840ea

Comment thread src/rust/worker/test.c++ Resolved
worker::Result and kj::http::Service's request() and connect() failed
with a KjError, which keeps only an exception's type, description,
location and details. A C++ exception that passed through a Rust
interface, such as a C++ worker wrapped by CxxWorkerInterface, came
back without its context, remote trace and stack trace. Their error is
now the KjException, which reaches C++ as the exception it is. Service
implementations spell it kj::http::ServiceResult.

KjError remains the error of the code around them: an implementation
that builds a KjError converts it at the trait method with ?, which
gives an error without a location the location of the conversion.
kj-hyper's client and the disk, bucket and cache services keep their
bodies as KjError methods and convert once. A listener that drives an
interface turns a failure back into a KjError to log it, as it did
before. PromisedInterface::new() takes a startup future that fails
with any error that converts to a KjException, and gives an error
without a location the location of the call.

A KjException can now be cloned with kj::Exception::clone(), which
copies everything the exception holds, its context, remote trace and
stack trace included, so a Rust holder of a KjException can hand out
copies without converting through KjError.
@bartlomieju
bartlomieju force-pushed the biwanczuk/rust-worker-kj-exception branch from 9bb840e to 9ab7718 Compare November 21, 2026 08:11

@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 findings. The only change since the approved review is the new C++ test, which correctly checks that the full exception comes back from all eight events.

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 security concerns. JavaScript still gets only the exception's type and description.


📖 AGENTS.md: ✅ LGTM

Nothing in this pull request makes the existing contributor guidance out of date.


📋 Code Quality: ✅ LGTM

The new test sets up all eight events and its exception checks correctly. startFailure() now adds a context, a remote trace and a stack trace, and expectStartFailure checks for exactly those.


⚡ Performance: ✅ LGTM

Only test code changed since the last review.


📝 Documentation: ✅ LGTM

No documentation issues.


Reviewed commit: 9ab77185

@bartlomieju
bartlomieju merged commit 2d93f47 into main Nov 21, 2026
36 of 37 checks passed
@bartlomieju
bartlomieju deleted the biwanczuk/rust-worker-kj-exception branch November 21, 2026 09:10
KastanDay pushed a commit to KastanDay/workerd that referenced this pull request Nov 21, 2026
cloudflare#8728 changed worker::Result to carry a KjException, and cloudflare#8727, which
merged after it, still returns a KjError from AlarmOwnedElsewhere, so
main no longer compiles workerd-server. Convert the error where it is
made.
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.

3 participants