refactor(errors): unify error hierarchy with ContextError pattern by betegon · Pull Request #21 · getsentry/cli · GitHub
Skip to content

refactor(errors): unify error hierarchy with ContextError pattern#21

Merged
betegon merged 5 commits into
mainfrom
refactor/error-hierarchy
Jan 19, 2026
Merged

refactor(errors): unify error hierarchy with ContextError pattern#21
betegon merged 5 commits into
mainfrom
refactor/error-hierarchy

Conversation

@betegon

@betegon betegon commented Jan 19, 2026

Copy link
Copy Markdown
Member

Summary

Standardizes error handling across all CLI commands using a unified error hierarchy. Introduces ContextError for missing context (org, project) with consistent formatting and helpful suggestions, plus Zod schemas for API response validation.

Changes

  • Add central error handler in lib/errors.ts with CliError, AuthError, ApiError, and ContextError classes
  • Standardize all commands to use ContextError for missing org/project context
  • Add Zod schemas for Sentry API types with lenient validation (only core fields required)
  • Add shared output utilities (writeJson, writeLines) for consistent formatting
  • Fix TypeScript strict mode errors with proper null checks
  • Add CONTRIBUTING.md documenting command patterns

Test plan

  • npm run typecheck passes with no errors
  • npm run build completes successfully
  • Manual testing of commands with missing context shows helpful errors

🤖 Generated with Claude Code

betegon and others added 5 commits January 19, 2026 18:18
Add ContextError class for consistent "missing context" error messages
across all commands, following gh CLI patterns.

Changes:
- Add ContextError to error hierarchy in lib/errors.ts
- Remove positional arg from `project list` (breaking change)
- Update all get/list commands to use ContextError
- Update tests for new `--org` flag syntax

BREAKING CHANGE: `sentry project list <org>` no longer works.
Use `sentry project list --org <org>` instead.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Documents the CLI command conventions for future contributors:
- List vs get command patterns
- Context resolution order (flags > config > DSN)
- Common flags reference
- ContextError usage for consistent errors

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Update error messages to mention both auto-detection options:
- Run from a directory with a Sentry-configured project
- Set SENTRY_DSN environment variable

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…constant

Add DEFAULT_CONTEXT_ALTERNATIVES constant in errors.ts and use it as
the default value for ContextError constructor. This removes repeated
alternatives arrays from 5 command files.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add null checks for optional Sentry API fields (level, status, project,
  metadata, tags, dateCreated, etc.)
- Fix Stricli flag configs: use placeholder instead of variableName,
  remove optional from variadic flags, use string defaults for numberParser
- Cast this.process to NodeJS.Process for exitCode access
- Update formatStatusIcon/formatStatusLabel to accept undefined

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@betegon betegon marked this pull request as ready for review January 19, 2026 19:12
@betegon betegon merged commit ebf1b10 into main Jan 19, 2026
2 checks passed
@betegon betegon deleted the refactor/error-hierarchy branch January 19, 2026 19:12
BYK added a commit that referenced this pull request Jun 23, 2026
Fixes 5 open Dependabot alerts:

| Alert | Package | Severity | Fix |
|-------|---------|----------|-----|
| #10 | vite@8.0.13 (fs.deny bypass) | High | → 8.0.16 |
| #9 | vite@8.0.13 (NTLM hash leak) | Moderate | → 8.0.16 |
| #22 | form-data@4.0.5 (CRLF injection) | Moderate | → 4.0.6 |
| #14 | js-yaml@4.1.1 docs (DoS) | Moderate | → 4.2.0 |
| #13 | @babel/core@7.29.0 docs (arbitrary file read) | Low | → 7.29.6 |

**Accepted as tolerable risk:**
- Alert #21: js-yaml@3.14.2 in main lockfile — `gray-matter` pins
`^3.13.1` with no v3 patch backport; dev-only transitive dep used at
build time

All patches applied via `pnpm.overrides` in respective `package.json`
files. `check:deps` ✓, `lint` ✓, `typecheck` has a pre-existing
unrelated error in `src/commands/api.ts`.
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.

1 participant