paritytech / paritytech/polkadot-cli

feat: structured error output with error codes and context

Open
#65 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
TypeScript
Stars
10
Forks
2
Avg merge
12h 35m
Merged PRs (30d)
4

Description

Parent Epic: #63

Summary

Add error codes, context objects, and JSON error output so agents can programmatically handle failures.

Current State

All errors are plain text on stderr via console.error("Error: " + message). Agents must string-match to distinguish connection failures from validation errors from dispatch errors. Exit code is always 1.

Proposed Changes

Error class hierarchy (src/utils/errors.ts)
export class CliError extends Error {
  code: string;
  exitCode: number;
  context?: Record<string, unknown>;
  constructor(message: string, code = "E_CLI", context?: Record<string, unknown>) { ... }
}

export class ConnectionError extends CliError {
  constructor(message: string, context?: Record<string, unknown>) {
    super(message, "E_CONNECTION", context);
    this.exitCode = 3;
  }
}

export class MetadataError extends CliError { ... }      // E_METADATA, exit 4
export class ValidationError extends CliError { ... }    // E_VALIDATION, exit 2
export class AccountError extends CliError { ... }       // E_ACCOUNT, exit 5
export class TxDispatchError extends CliError { ... }    // E_TX_DISPATCH, exit 10
JSON error output (src/cli.ts)

When --output json is active, handleError emits structured JSON to stderr:

{"error":true,"code":"E_CONNECTION","message":"Failed to connect to wss://rpc.polkadot.io","context":{"chain":"polkadot"}}
Granular exit codes
Code Meaning
0 Success
1 General/unexpected error
2 Validation error (bad args, unknown pallet)
3 Connection error (RPC unreachable)
4 Metadata error
5 Account error
10 TX dispatch error (on-chain failure)
11 TX rejected (not included)
Propagate error codes through codebase

Update existing throw new CliError(...) calls in tx.ts, query.ts, chain.ts, account.ts to use appropriate subclasses with context.

Files to Modify

  • src/utils/errors.ts — extend error classes
  • src/cli.ts — JSON error output in handleError, use err.exitCode
  • src/commands/tx.ts — use TxDispatchError for dispatch failures
  • src/commands/*.ts — use appropriate error subclasses

Verification

  • Trigger each error type and verify echo $? returns the correct exit code
  • Run with --output json and verify JSON error on stderr parses with jq

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading src/utils/errors.ts and src/cli.ts, especially handleError, then trace the existing throw sites in src/commands/tx.ts and the other command files. Verify each listed failure category with the relevant command, check exit codes via echo $?, and confirm --output json produces stderr that parses with jq.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.