Repository navigation
Description
Background
@cipherstash/recipher is the TypeScript SDK on the Stack Encrypt engine (the Rust crate stack-encrypt, which encrypts and decrypts values). Its Rust half is at languages/typescript/packages/recipher-js: the Node addon recipher-js (crate/) and the value bridge recipher-value-napi (value-napi/), which reads JavaScript values into the engine's value model and builds them back.
The engine's errors are miette diagnostics: each has a stable code (such as stack_encrypt::aead), a message, and often help, structured fields and causes. The SDK hands an error to the caller as a RecipherError value.
Problem
The addon describes errors a second time, in three places.
ErrorKindandkind_of(crate/src/error.rs). The addon defines nine "kinds" of its own (credential,zerokms,openand so on) and amatchthat sorts every engine error variant into one. A new engine variant falls intointernalunless someone remembers to add it. The code already says what the error is, more exactly.- The same nine kinds, listed again in TypeScript (
KINDSinsrc/error.ts), to check what the native build returned. - The SDK's own errors are string literals. The addon gives 39 errors a code of its own by writing
"recipher::not_a_row"and a message at each place one is raised. Nothing but a source-scanning test ties them together.
Every language SDK will meet the same choice, so the answer here is the pattern for the others: do not re-describe what the engine's diagnostic already says.
Proposal
- Remove
kind. DeleteErrorKind,kind_ofandKINDS. ARecipherErroris the diagnostic and nothing else:code,message,help,url,severity,fields,causes, andfieldandrecordwhere the SDK names them. A caller switches oncode. - Make the SDK's own errors a Rust enum that derives
miette::Diagnostic, with the code, message and help on each variant, as the engine does. Raise an error by naming a variant. - Convert in one place. One function turns any diagnostic (the engine's, the value bridge's, the SDK's own) into the object JavaScript receives.
- Replace the source-scanning test with one that builds every variant and checks its code, as
packages/stack-encrypt/src/codes.rsdoes. - Update the README, the plan (docs(plans): the TypeScript SDK on Stack Encrypt, a core with EQL as an extension #1747) and the tests that assert on
kind.
This is a breaking change to the package's API. The package is private and unreleased, so it needs no changeset.
Relationship to other work
- From the review of feat(recipher-js): walking skeleton of the TypeScript SDK #1774.
- feat(recipher-js): an error keeps the engine's help, URL, fields and causes #1791 made the error carry the whole diagnostic. This removes the one part the SDK still invents.
- Depends on the value bridge returning typed errors, so that its refusals convert the same way.
Activity
feat(recipher-js): walking skeleton of the TypeScript SDK
feat(recipher-js): walking skeleton of the TypeScript SDK