Skip to content

fix: surface batch API errors in commands#159

Merged
scottlovegrove merged 3 commits into
mainfrom
fix/158-surface-batch-errors
May 13, 2026
Merged

fix: surface batch API errors in commands#159
scottlovegrove merged 3 commits into
mainfrom
fix/158-surface-batch-errors

Conversation

@goncalossilva
Copy link
Copy Markdown
Member

@goncalossilva goncalossilva commented Apr 8, 2026

Closes #158. The issue includes a repro.

@goncalossilva goncalossilva added the 🙋 Ask PR PR must be reviewed before merging label Apr 8, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses cases where CLI commands assume batched API responses always contain successful .data payloads, causing misleading internal crashes on API validation/request errors. It centralizes batch-response validation so commands surface the real API error message instead.

Changes:

  • Enhance assertBatchData to extract and display batch API error details (e.g., errorString) via CliError('API_ERROR').
  • Introduce buildBatchNameMap and refactor inbox/thread/conversation commands to use it (and assertBatchData) instead of directly accessing response.data.
  • Add regression tests ensuring batch failures (including per-entity user lookups) produce clear errors rather than TypeErrors.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/lib/api.ts Adds batch error-message extraction, strengthens assertBatchData, and introduces buildBatchNameMap to safely consume batch results.
src/commands/thread/view.ts Uses shared helpers to validate batch responses and fail fast with clear errors on user lookup failures.
src/commands/inbox.ts Validates inbox/unread batch results and uses buildBatchNameMap for channel name resolution, preventing .data.map crashes.
src/commands/conversation/view.ts Validates conversation/messages batch results and uses buildBatchNameMap for participant names.
src/commands/conversation/unread.ts Validates per-conversation batch lookups and uses buildBatchNameMap for participant names.
src/commands/conversation/helpers.ts Switches user-name map construction to buildBatchNameMap for consistent batch validation behavior.
src/__tests__/thread.test.ts Adds coverage for failed batched user lookup during thread view.
src/__tests__/inbox.test.ts Adds coverage for surfacing a batch API validation error (limit too high) in inbox.
src/__tests__/conversation.test.ts Adds coverage for failed batched user lookup during conversation view.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Member

@doistbot doistbot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR updates the CLI commands to properly surface batch API errors, improving observability and error awareness. However, the updated user lookup logic in the thread view inadvertently causes a hard failure for missing user entries instead of preserving the existing fallback behavior that gracefully skips null results.

Share FeedbackReview Logs

Comment thread src/commands/thread/view.ts Outdated
@scottlovegrove scottlovegrove force-pushed the fix/158-surface-batch-errors branch from cbdd9ec to 9b98e0b Compare May 13, 2026 12:50
scottlovegrove and others added 2 commits May 13, 2026 13:55
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
`buildBatchNameMap` was applied to user lookups in thread and conversation
views, which made a single deleted/missing user abort the whole command.
Add `buildOptionalBatchNameMap` that skips entries with `data: null` and a
success code while still surfacing real API errors, and use it for user
maps. Callers continue to fall back to `user:<id>` for missing entries.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@scottlovegrove scottlovegrove merged commit 3b30c3c into main May 13, 2026
4 checks passed
@scottlovegrove scottlovegrove deleted the fix/158-surface-batch-errors branch May 13, 2026 13:03
doist-release-bot Bot added a commit that referenced this pull request May 13, 2026
## [2.36.4](v2.36.3...v2.36.4) (2026-05-13)

### Bug Fixes

* surface batch API errors in commands ([#159](#159)) ([3b30c3c](3b30c3c))
@doist-release-bot
Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 2.36.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

scottlovegrove added a commit that referenced this pull request May 13, 2026
…/inbox

Address review feedback on PR #224 now that #159 is merged:

- Add `getOptionalBatchData` helper in `src/lib/api.ts` — returns `data` (or
  null) on success, throws via `assertBatchData` only on real API errors. Use
  for batch sub-requests where `data: null` with a success code is a valid
  empty state (e.g. `getUnread` when the workspace has zero unread threads).
- `channel/threads.ts`: use `assertBatchData` for primary `threadsResp` so a
  failed `getThreads` sub-request surfaces an API error instead of crashing
  with `TypeError: .map is not a function`. Use `getOptionalBatchData` for
  `unreadResp` to preserve the "no unread = empty" valid state.
- `inbox.ts`: switch `unreadResponse` from strict `assertBatchData` to
  `getOptionalBatchData` (fixes regression introduced by #159 where a
  workspace with zero unread threads would crash `tw inbox`).
- `channel/threads.test.ts`: update `setupClient` to accept `unread: null`,
  add `code: 200` to batch mock fixtures, refactor the regression test to
  use the helper, and add a new test for the threads-batch-error path.
- `inbox.test.ts`: add a regression test for the null-unread case.
- Document the four batch helpers and when to use each in AGENTS.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🙋 Ask PR PR must be reviewed before merging released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Some commands don’t handle errors and crash with misleading internal errors

4 participants