Skip to content

feat(errors): say whether a retry can work, and what to run - #177

Merged
neoromantic merged 1 commit into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/actionable-errors
Nov 18, 2026
Merged

neoromantic merged 1 commit into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/actionable-errors

Conversation

@ertembiyik

Copy link
Copy Markdown
Contributor

What and why

Point 2 of #160. Every error now says whether running the same command again can work (error.retryable), and a rate limit says how long to wait before that, so an agent retries a flood wait instead of giving up or retrying blindly. The errors that had something actionable to say also suggest what to run. Refs #160

Before (tg api help.getNearestDc during a flood wait, exit 5):

{
  "success": false,
  "error": {
    "code": "RATE_LIMITED",
    "message": "Rate limited for help.getNearestDc. Wait 42s before retrying.",
    "details": { "method": "help.getNearestDc", "wait_seconds": 42 }
  }
}

After:

{
  "success": false,
  "error": {
    "code": "RATE_LIMITED",
    "message": "Rate limited for help.getNearestDc. Wait 42s before retrying.",
    "details": { "method": "help.getNearestDc", "wait_seconds": 42 },
    "suggestion": "Wait 42s, then run the same command again",
    "retryable": true
  }
}

A refused call says the opposite (tg api help.getNothing):

{
  "code": "TELEGRAM_ERROR",
  "message": "API call failed: METHOD_INVALID",
  "details": { "method": "help.getNothing", "telegramErrorCode": 400 },
  "retryable": false
}

Two errors that gained a suggestion:

Command Suggestion
tg chats list --account 99 (ACCOUNT_NOT_FOUND) Run tg accounts list to see the accounts, then pass an ID, @username or label to --account
tg send --to 123456 -m hello (PEER_ID_INVALID) Send to the user's @username or +phone once: tg caches the user, and its ID works after that

All output above is real, from the e2e fixtures with the fake client.

What changed:

  • retryable is on every error. It is true for RATE_LIMITED (after the wait), NETWORK_ERROR, and a media download that timed out waiting for another tg process's lock; false for everything else.
  • The wait stays where it was, in details.wait_seconds, with no second copy. All rate-limit sites already put it there; the docs now say it is always present on RATE_LIMITED.
  • One helper for rate limits. The nine copies of the RATE_LIMITED block in the commands are now reportRateLimit(err), and the two media cases share one builder, so the message, details and suggestion can't drift apart.
  • Suggestions say "run the same command again" instead of rebuilding the command line, which could echo --message text or a path.

Compatibility: codes, exit codes, messages and existing fields are unchanged. The one visible difference is the new key, so a script that deep-compares a whole error object would notice; five of the repo's own e2e assertions did, and are updated.

Counts on main at c44c203, by call site: 28 of 146 error() calls carried a suggestion; now 37 of 138 (fewer calls because of the helper). RATE_LIMITED went from 0 of 9 to all of them, ACCOUNT_NOT_FOUND from 0 of 8 to 7 of 8. I left INVALID_ARGS alone because almost every message already names the fix, and the TELEGRAM_ERROR catch-alls because there is nothing to add to Telegram's own text. The Unknown object undefined case from the issue is already fixed in 0.13.1.

Decisions you may want to change:

  • retryable is a fifth positional argument of error() with a default per code. An options object would read better if more overrides appear.
  • TELEGRAM_ERROR is always retryable: false, including Telegram's 500 and -503, which may pass on a retry. Classifying those needs the RPC error at each catch-all.

Found on the way, not changed here:

  • tg me turns any failure of getMe into AUTH_REQUIRED, so a network error there reports "not authorized".
  • A missing required flag prints citty's usage text, not a JSON INVALID_ARGS. Agents probably hit that more than any other argument error.

This touches src/utils/output.ts and src/commands/send.ts, as #176 and #175 do. I'll rebase whichever of the three lands later.

How I checked it

  • New e2e tests in src/__e2e__/api.e2e.test.ts drive the fake client to answer FLOOD_WAIT_42 (twice: the second run fails before calling Telegram, with the same shape) and PEER_ID_INVALID, and assert the retry signal, the wait and the exact suggestions. accounts and multi-account e2e tests cover the account suggestions.

  • Unit tests for error(), the account selector, the media download errors and the history command.

  • Not verified: the exact error the failed parallel downloads in the issue printed; I can't reproduce those sessions. Both ways a flood wait reaches a download are tested.

  • bun run check: clean. bun run test: 2304 pass, 0 fail. bun run test:e2e: 437 pass, 0 fail.

  • Tests pass locally and use made-up data only

  • README, docs and affected demos follow the product content standard (docs/api-design.md, docs/cli-commands.md, docs/rate-limiting.md, docs/media.md, README, the schema line in AGENTS.md, CHANGELOG)

  • Title follows the repo's commit style: Conventional Commits (feat: …, fix: …) unless AGENTS.md says otherwise

🤖 Generated with Claude Code

Every error has retryable: true when the same command, run again
unchanged, can succeed (a rate limit after its wait, a network error, a
download waiting on another tg process), false otherwise. A rate limit
suggests the wait, which stays in details.wait_seconds; the nine copies of
that block are one helper. An unknown --account and a numeric --to
Telegram refuses suggest what to run. Codes, exit codes and messages are
unchanged.

Refs neoromantic#160

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@neoromantic
neoromantic merged commit e05b86c into neoromantic:main Nov 18, 2026
5 checks passed
ertembiyik pushed a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
Resolves the conflicts with neoromantic#176/neoromantic#177 in CHANGELOG.md (both sections
kept) and src/commands/messages/search.ts (success(data, next) from main,
the snippet from the rendered item text from neoromantic#178).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
CHANGELOG: both sides' entries kept. Per-file error lines carry retryable (neoromantic#177) and the shared rate-limit wording.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
helpers.ts: a message item gets the transcript and the rich text (neoromantic#186). CHANGELOG: both sides' entries kept. Per-message error lines and the docs example carry retryable (neoromantic#177).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
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.

2 participants