Skip to content

refactor(recipher-js): an error is its diagnostic and nothing else - #1807

Closed
coderdan wants to merge 1 commit into
refactor/recipher-typed-refusalsfrom
refactor/recipher-error-is-the-diagnostic
Closed

coderdan wants to merge 1 commit into
refactor/recipher-typed-refusalsfrom
refactor/recipher-error-is-the-diagnostic

Conversation

@coderdan

Copy link
Copy Markdown
Contributor

Summary

Closes #1800. On top of #1805.

A RecipherError is now the diagnostic and nothing else. The kind property is gone, and a caller switches on code. A diagnostic is the error type the Stack Encrypt engine uses: a stable code, a message, often help, structured fields and causes.

Before, the addon described each error twice. It sorted every engine error into one of nine kinds by hand, and the TypeScript listed the kinds again. The SDK's own 39 errors were string literals written where each was raised. Now the SDK's own errors are one Rust enum, declared the way the engine declares its errors, and one function converts any diagnostic into the object JavaScript receives.

The package is private and unreleased, so there is no changeset.

Changes

Rust (crate/src/)

  • ErrorKind and kind_of are deleted.
  • A new error::Error enum holds the SDK's own errors. It derives thiserror::Error and miette::Diagnostic. Each variant has its code, its message and, where there is advice, its help. A part that varies, such as a kind name, is a field of the variant.
  • RecipherErrorFields::diagnostic is the one conversion. The engine's errors, the value bridge's Refusal and the SDK's own enum all go through it. engine is a short form that adds the engine's structured fields. in_record and at_field set the two properties the SDK names itself.
  • The JSON writer's own error type (NotJson) is gone. The writer uses the enum directly.
  • The test that scanned the source for codes is replaced. The new test builds one of every variant, through a match with no wildcard, and checks each code with diagnostic::is_code_of("recipher", ..) and that no two variants share one.
  • thiserror moves from a dev-dependency to a dependency, at the version the engine uses. Cargo.lock does not change.
  • The binding interface number moves from 3 to 4, because the error object lost a property.

TypeScript (src/)

  • KINDS, kind and the ErrorKind export are deleted. fromNative now checks that the error has a string code and message.
  • internal(..) is now sdkError(..). It is used only when the native build did not load or did not answer as it should.
  • testCipher no longer raises an error of its own when reading its options throws. It passes no seed, and the native build refuses it with recipher::testing_seed.

Docs and tests

  • The README's "Results, not exceptions" section and table drop kind. Doc comments say the code instead of the kind.
  • Every test that asserted a kind now asserts the code. Where the next line already asserted the code, the kind line is removed.
  • The type test that switched over the kinds now checks that code is a string and that kind does not exist.

What changes for a caller

  • kind is removed, and so is the ErrorKind type.
  • Codes:
    • New: recipher::record_name, for a record name the engine refuses as part of a field path (empty, or with a /). Before, this was the engine's stack_encrypt::dynamic_plan with a message the SDK rewrote. The engine's message names the field even when the record name is at fault, so a code of its own is clearer.
    • Renamed: the TypeScript's recipher::threw (a call into the native build threw) is now recipher::native_threw. The addon already used recipher::threw for a different failure: a getter of the application's threw while a value was read. One code had two meanings.
    • Removed: recipher::unknown is no longer raised from TypeScript. A native error with no code is refused as recipher::outcome.
    • Every other code is unchanged.
  • Messages: where a message held advice, the advice is now in help. This affects recipher::undefined, uuid, not_a_scalar, plain_object, not_a_ciphertext, no_testing, not_a_record, reserved_field, null, no_field, engine_result, outcome and build_value. Some codes had several messages, and each now has one:
    • recipher::no_field: "the record declares no sealed field of that name", for a field that is missing, passed through or not named by a string.
    • recipher::not_a_record: "the value is not a record".
    • recipher::outcome: "the engine returned no result".
    • recipher::build_value: "the result could not be given to JavaScript". It no longer shows the Node-API status.
    • recipher::value: "the value cannot be encrypted: it is not one the engine reads". It no longer copies the text of the frame error, which comes from another library.
    • recipher::reserved_field no longer names the field in its message. error.field names it.
    • A field name that is not a plain segment now has the engine's message alone, with no SDK prefix. The code was already the engine's.
  • Fields: a ciphertext whose bytes are malformed (Ciphertext.fromBytes, or a stored row's ciphertext) now carries the engine's structured fields for that error.

TypeScript errors that remain

These four are raised in TypeScript because they must exist when there is no native build, or when it answers wrongly:

  • recipher::load: the native build did not load, or does not export its classes.
  • recipher::binding_interface: the native build implements another interface number.
  • recipher::native_threw: a native call threw, as napi does for a method called on something that is not its class.
  • recipher::outcome: the native build returned something that is not an outcome, or an error of an unknown shape.

Size

Before After
Non-test Rust in crate/src/ (excluding *_tests.rs and mod tests) 3,138 lines 2,995 lines
src/*.ts 1,476 lines 1,440 lines

error.rs grew from 254 to 362 non-test lines, because it now holds every message, help and code the SDK writes. Every file that raised an error shrank. The whole diff is 656 lines added and 898 removed, tests and native.d.ts included.

Verification

Run locally in the package directory, with the debug addon rebuilt:

  • cargo fmt --all --check: clean.
  • cargo test --locked --all-features: recipher-js 39 and 4 passed, recipher-value-napi 33 and 1 passed.
  • cargo clippy --locked --all-targets --all-features -- -D warnings: clean. Also clean without --all-features.
  • napi build ... --features testing --dts native.d.ts --no-const-enum: native.d.ts regenerated and committed.
  • tsup: built. vitest run: 29 passed. vitest run --config vitest.addon.config.ts: 76 passed. tsc --noEmit -p tsconfig.json: clean.
  • From the repository root: biome check languages/typescript/packages/recipher-js: clean. vitest run --config scripts/vitest.config.mjs: 96 files, 1,598 passed, 29 skipped.

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

Review notes

  • What dropping kind loses. No single code expresses a group. A caller who asked "is this a credential problem?" checked kind === 'credential'. That kind covered stack_encrypt::config and every stack_kms authentication code. Now the caller lists the codes. The same is true of zerokms, open, value and internal. I did not add a field to replace the groups. Every distinction a test made is still made by a code: a context mismatch and a foreign keyset, both refused before any key is requested, have the engine's codes stack_encrypt::context_mismatch and stack_encrypt::foreign_keyset.
  • One distinction is now only in the call. recipher::reserved_key was kind value when encrypting and kind open when decrypting. The code is the same both ways, so only the call that failed says which.
  • Codes shared across the two enums. The test checks that the addon's enum uses each code once. It does not compare against the value bridge's Refusal. Two codes appear in both, with nearly the same meaning: recipher::not_bytes (bytes given to Ciphertext.fromBytes, and a typed array the bridge refuses) and recipher::too_deep (a JSON document past 127 levels, and a value past the bridge's 128). recipher::outcome is used by the addon ("no result") and by the TypeScript ("not an outcome"). All three are bugs or near-synonyms, so I kept them.
  • recipher::unknown stays in the conversion as the code of a diagnostic with none. No error the addon converts lacks a code, so a caller never sees it.
  • The field of a field-name refusal is still the engine's payload field, as before.
  • docs/plans/2026-11-22-recipher-typescript-sdk.md is not changed here.

Related

The addon described every error twice. It sorted each engine error into
one of nine kinds with a match that sent any new engine variant to
`internal`, the TypeScript listed the same kinds again, and the SDK's own
39 errors were string literals at each place they were raised, tied
together only by a test that scanned the source. The diagnostic's code
already says what an error is, more exactly than a kind, and every
language SDK meets the same choice.

So `kind` is gone. The SDK's own errors are one enum that derives
`thiserror::Error` and `miette::Diagnostic`, with the code, message and
help on each variant, as the engine declares its errors, and an error is
raised by naming a variant. One function turns any diagnostic, the
engine's, the value bridge's or the SDK's own, into the object JavaScript
receives. A test builds one of every variant, through a match with no
wildcard, and checks each code's form and that none is used twice.

Removing a property changes the outcome's shape, so the binding
interface moves to 4.

Closes #1800.
@coderdan coderdan self-assigned this Nov 24, 2026
@changeset-bot

changeset-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: efcd81f

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

@coderdan

Copy link
Copy Markdown
Contributor Author

Folded into the rewritten stack: the error is the diagnostic with no kind from #1774 on. #1800 is now closed by #1774. Previous head: efcd81f. 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