Skip to content

feat(recipher-js): an error keeps the engine's help, URL, fields and causes - #1791

Closed
coderdan wants to merge 6 commits into
feat/recipher-js-recordsfrom
feat/recipher-js-diagnostics
Closed

coderdan wants to merge 6 commits into
feat/recipher-js-recordsfrom
feat/recipher-js-diagnostics

Conversation

@coderdan

@coderdan coderdan commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On top of #1776. A RecipherError now carries the engine's whole diagnostic, the way the Go SDK's Diagnostic does, instead of a code and a message with the help glued onto it.

The engine's errors are miette diagnostics: each has a stable code, and many have a help text, structured fields and a chain of causes. Before this change the TypeScript SDK kept the code, appended the help to message, read two of the fields (reason and field) and dropped the causes.

The package is still private and takes no changeset.

Changes

  • help and url are properties of their own. message is the engine's one line and nothing else, so a caller can show the help separately or leave it out. toString() prints the help on a second line.
  • severity is the diagnostic's, as in Go: error unless the engine's diagnostic states warning or advice. No engine error states one today, so it is always error.
  • fields holds every structured field the engine gives (ErrorPayload::payload), by snake_case name. The typed reason property is gone, since fields.reason is the same string. field and record are unchanged.
  • causes lists the errors behind this one, outermost first. A cause that is one of the engine's own errors gives its code and message. A cause from another library gives the fixed text an error from another library, and its own message is never copied: that text is not ours and can quote a value. This is the rule the Go guest's encoder follows (packages/stack-guest-abi/src/last_error.rs).
  • A test lists every code the SDK gives an error of its own (39 recipher::… codes). It reads the Rust and TypeScript sources, checks each code's form with the engine's diagnostic::is_code_of, and fails when a code is added without being added to the list.
  • napi's serde-json feature is on, so fields crosses as a plain object. thiserror is a dev-dependency for the test's probe errors. Both were already in the lockfile.

The README's error section now has an example and a table of the properties.

Verification

Run locally in a worktree, with the debug addon rebuilt:

  • cargo test --locked --all-features: 39 and 4 passed.
  • cargo fmt --all --check and cargo clippy --locked --all-targets --all-features -- -D warnings: clean.
  • vitest run: 29 passed. vitest run --config vitest.addon.config.ts: 76 passed.
  • tsc --noEmit: clean. Biome: clean.
  • native.d.ts is regenerated by build:debug and committed.

Not run: the release build, and anything on Linux or Windows. CI is the check for those.

Review notes

Related

One napi-rs 3 crate, recipher-js, defines the Cipher, Keyset and
Ciphertext classes and runs encrypt and decrypt of one string through the
engine. @napi-rs/cli v3 builds it and writes its TypeScript declarations
to the committed native.d.ts. The TypeScript holds Result, RecipherError,
the typed signatures over the native methods, and the adapter that turns
each native outcome into a Result, so no call throws.
keyset.encrypt takes every value in vitaminc's JavaScript mapping, an
array of them in one key request, and a JSON document made with json(..),
a native class that reads the document when it is made. Ciphertext
carries the kind it opens as, and Ciphertext.fromBytes(bytes, kind) reads
stored bytes as a kind, kinds.json among them. vitaminc's refusals each
get a code of their own. The binding interface is 2.
record(..), encrypt(..) and passthrough() declare a record as plain data.
The crate builds the engine's plan from it with the engine's own types,
once for each declaration, and encrypts and decrypts rows, and one field
or an array of one field, through dynamic::Planned. A refusal names the
record and the field. A decrypted row's inspect hides its values. The
binding interface is 3.
…causes

A RecipherError held the engine's code and a message with the help
appended to it, and two of the structured fields. It now holds the whole
diagnostic, as the Go SDK's does: help and url as properties of their own,
every structured field, and the cause chain. A cause from another library
is named as one and its message is never copied. A test lists every code
the SDK gives an error of its own and checks each one's form.
@changeset-bot

changeset-bot Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 35ab136

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

miette's Diagnostic has a severity, and the Go SDK's Diagnostic carries
it. A RecipherError now does too: error unless the engine's diagnostic
states warning or advice.
fields holds every structured field of the engine's error, the reason
token among them, so the typed reason property was the same string a
second way. It is removed. field and record stay: the SDK names them
where the engine does not.
@coderdan

Copy link
Copy Markdown
Contributor Author

Folded into the rewritten stack: the error now carries the whole diagnostic from #1774 on (#1774 for the string path, #1775 for values, #1776 for records). Previous head: 35ab136. The branch is kept.

@coderdan coderdan closed this Nov 24, 2026
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.

1 participant