Skip to content

recipher-value-napi reports a refused value as free text, so the addon matches message prefixes — return a typed error with a code #1799

Done

Description

@coderdan

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.

When the value bridge refuses a value (a Map, an array with a hole, a bigint too large to store), it has to tell the addon why, so the addon can give the caller an error with a stable code.

Problem

The bridge reports a refusal as a Node-API status and a sentence of free text (JsFault, value-napi/src/error.rs). The addon then works out which refusal it was by comparing the start of that sentence against a table (WALK_REFUSALS in crate/src/js.rs):

("only plain objects", "recipher::not_plain", "a Map, a Set, a class instance ..."),
("sparse arrays", "recipher::sparse_array", "an array has a hole ..."),

A second table (VITAMINC_CODES) repeats the six ERR_… codes the bridge already defines in ConversionError, to attach the SDK's own message to each.

So one fact, which refusal happened, is written three times: in the bridge's text, in the addon's table of text prefixes, and in the addon's table of codes. If someone rewords a message in the bridge, the addon stops recognising it and reports the generic recipher::value. No test fails unless it covers that exact refusal.

This was unavoidable while the bridge was a crate from another repository. It lives here now (#1794), so the two sides can share a type.

Proposal

  1. Give the bridge one error enum for every refusal of a value, deriving miette::Diagnostic so each variant has a code and a message, as the engine's errors do. It covers the walk's refusals (not a plain object, a reserved property name, nested too deeply, a sparse array, bytes of the wrong kind, a type with no value, an object that changed while it was read, an unpaired surrogate) and the six scalar refusals in ConversionError.
  2. The addon reads the variant. Delete WALK_REFUSALS, VITAMINC_CODES and the matching on message text in crate/src/js.rs.
  3. Keep the rule that a thrown JavaScript error's own message is never passed on: it can quote the value.
  4. A test builds one of every variant and checks each has a code of the right form, as packages/stack-encrypt/src/codes.rs does.

The codes a caller sees should stay the same unless there is a reason to change one. If the six ERR_… codes become recipher::… names, say so in the pull request.

Relationship to other work

Activity

  1. added
    enhancement
    on Nov 24, 2026
  2. self-assigned this
    on Nov 24, 2026
  3. added 4 commits that reference this issue on Nov 24, 2026
    4881e04
    7d1b161
    9bea08e
    5146eb4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

SDKenhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions