Skip to content

refactor: centralize error handling with typed error classes - #24

Merged
wyattjoh merged 6 commits into
mainfrom
wyattjoh/errors
Mar 11, 2026
Merged

refactor: centralize error handling with typed error classes#24
wyattjoh merged 6 commits into
mainfrom
wyattjoh/errors

Conversation

@wyattjoh

@wyattjoh wyattjoh commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

What

  • Introduce a typed error hierarchy (CliError, UserAbortError, ApiError, BapiError, PlapiError) in src/lib/errors.ts with standardized exit codes
  • Add a single top-level error handler in cli.ts that catches all typed errors and formats output consistently
  • Add --verbose flag for detailed API error output
  • Add SIGINT handler that exits with code 130
  • Add oxlint configuration (.oxlintrc.json)

Why

Error handling was scattered across every command — each had its own try/catch + console.error + process.exit with inconsistent formatting, exit codes, and messaging. This made it difficult to maintain consistent UX and meant every new command had to reimplement error handling. Centralizing it ensures uniform behavior and reduces per-command boilerplate.

How

  • Created src/lib/errors.ts with a class hierarchy: CliError for general CLI errors, ApiError (with BapiError/PlapiError subclasses) for HTTP failures, and UserAbortError for prompt cancellations. Helper functions usageError() and userAbort() provide ergonomic throwing.
  • Wrapped program.parseAsync() in a main() function with a centralized catch that dispatches on error type — clean exit for aborts, red-formatted messages for CLI errors, labeled API error details for API failures.
  • Updated all commands to throw typed errors instead of handling exits locally.
  • Updated all tests to assert on thrown errors rather than spying on process.exit.

AIE-626

Replace scattered try/catch + console.error + process.exit patterns
with a typed error hierarchy (CliError, ApiError, BapiError, PlapiError,
UserAbortError) and a single top-level error handler in cli.ts.

Commands now throw typed errors instead of handling exits locally,
making error behavior consistent and testable. Adds --verbose flag
for detailed API error output and SIGINT handling.
Add `context` field to `ApiError` and `withApiContext()` utility to
attach human-readable operation context at call sites without verbose
try/catch blocks. Drop internal "Platform API"/"Backend API" labels
from the global error handler.
Bun's test runner doesn't fully clear process.exitCode when set to
undefined, causing the test file to exit with code 1 despite all
assertions passing.
@wyattjoh
wyattjoh marked this pull request as ready for review March 10, 2026 20:19
Comment thread src/cli.ts
.action(deploy);

program.parse();
function formatApiBody(body: string, verbose: boolean): string {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

consider making this a method on ApiError

@brkalow brkalow left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, do you think there are any meaningful additions to AGENTS.md to be made to ensure this pattern is followed?

Comment thread .oxlintrc.json
Comment thread src/lib/errors.ts Outdated
Comment thread src/lib/errors.ts
…ndling

- Rename usageError/userAbort to throwUsageError/throwUserAbort to make
  the never-returning throw behavior explicit at call sites
- Auto-append .md to Clerk docs URLs in agent mode so agents receive
  raw markdown instead of rendered HTML
@wyattjoh
wyattjoh merged commit bb011ca into main Mar 11, 2026
2 checks passed
@wyattjoh
wyattjoh deleted the wyattjoh/errors branch March 11, 2026 16:24
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.

4 participants