Skip to content

Shared value formatter for error messages and log lines (logger) #424

Open

Description

@ReinhardHatko

Problem

Turning an arbitrary value into one readable string for an error message or a log line is written again at each site, and each copy handles a different subset of the cases that break JSON.stringify or String():

  • The full rule set exists twice. describeValue in packages/signer/src/providers/host.ts (added in fix(signer): serialize object payloads in host error messages #423) handles bigints, byte arrays, nested Errors, cycles and a length cap. formatLogData in Coffer's lib/log.ts (paritytech/coffer) implements the same rules for its log lines.
  • Bigint-only JSON replacers:
    • stringifyArg in packages/contracts/src/errors.ts;
    • one in packages/individuality/src/as-person-codec.ts.
  • Byte-to-hex by hand in 26 places, among them:
    • address/src/h160.ts
    • cloud-storage/src/cid.ts
    • contracts/src/wrap.ts
    • host/src/testing.ts
    • individuality/src/airdrop-ids.ts
  • e instanceof Error ? e.message : String(e) in 17 places. It drops an error's cause and any object payload.

@parity/product-sdk-logger passes data to the console as a separate argument. Devtools shows it, but a host log file that keeps only text records it as [object Object]. The Polkadot Desktop log does this; it is how a noAllowance refusal went unrecorded in Coffer. So every product that wants readable logs writes its own formatter.

Proposal

  1. Export a value formatter from @parity/product-sdk-logger, for example formatLogValue(value, { maxChars, maxBytes }), with the rules of describeValue:
    • primitives via String;
    • objects as JSON;
    • bigints as decimal strings;
    • byte arrays as hex, cut after maxBytes with the length;
    • a nested Error as name, message and cause;
    • [seen] for repeated objects and cycles;
    • elided past maxChars.
  2. Use it in the signer's formatError in place of the private describeValue, and in stringifyArg.
  3. Optionally, offer a console handler that prints each entry as one string ([namespace] message {data}). Products could then drop their own handlers; Coffer's is in app/composables/logging.ts.

The byte-to-hex and error-message sites could move to the helper where they only produce text for humans. Sites that produce protocol values, such as CIDs and addresses, stay as they are.

Release

This is new public API in the logger: a minor changeset for @parity/product-sdk-logger, and per RELEASES.md, @parity/product-sdk at minor in the same changeset. It is planned as a follow-up once #423 merges, so that #423 stays a patch.

🤖 Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions