Skip to content

refactor(errors): return Result type from withAuthGuard, expand auto-login to expired tokens#359

Merged
BYK merged 3 commits intomainfrom
refactor/with-auth-guard-result-type
Mar 5, 2026
Merged

refactor(errors): return Result type from withAuthGuard, expand auto-login to expired tokens#359
BYK merged 3 commits intomainfrom
refactor/with-auth-guard-result-type

Conversation

@BYK
Copy link
Member

@BYK BYK commented Mar 5, 2026

Replace withAuthGuard's fallback+onError API with a discriminated Result type (AuthGuardResult<T>) that gives callers direct access to caught errors. This eliminates mutable error variables and closure-based error handlers while keeping the same auth-propagation semantics.

Changes

  • withAuthGuard API redesign — now returns { ok: true, value: T } | { ok: false, error: unknown } instead of accepting fallback and onError parameters. Exported types: AuthGuardSuccess<T>, AuthGuardFailure, AuthGuardResult<T>.
  • Convert 4 remaining manual instanceof AuthError catch patternsregion.ts (×2), project/list.ts handleExplicit, issue/list.ts fetchIssuesForTarget
  • Expand auto-login flowbin.ts and app.ts now also trigger interactive login on AuthError("expired"), with context-aware messaging
  • Update telemetry — suppress Sentry error capture for expired tokens (expected state, not a bug)
  • Update tests — rewrite withAuthGuard unit and property-based tests for new Result type API

Impact

  • 12 files changed, net −45 lines
  • All 15 existing withAuthGuard call sites migrated
  • 8 instanceof AuthError patterns remain (3 infrastructure, 5 intentionally kept for specialized semantics)
  • onError callback removed entirely (was used by only 2 of 15 call sites)

@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Trace

Other

  • (api) Add --data/-d flag and auto-detect JSON body in fields by BYK in #320
  • (formatters) Render all terminal output as markdown by BYK in #297
  • (install) Add Sentry error telemetry to install script by BYK in #334
  • (issue-list) Global limit with fair distribution, compound cursor, and richer progress by BYK in #306
  • (log-list) Add --trace flag to filter logs by trace ID by BYK in #329
  • (logger) Add consola-based structured logging with Sentry integration by BYK in #338
  • (project) Add project create command by betegon in #237
  • (upgrade) Add binary delta patching via TRDIFF10/bsdiff by BYK in #327
  • Support SENTRY_AUTH_TOKEN and SENTRY_TOKEN env vars for headless auth by BYK in #356
  • Improve markdown rendering styles by BYK in #342

Bug Fixes 🐛

Api

  • Use numeric project ID to avoid "not actively selected" error by betegon in #312
  • Use limit param for issues endpoint page size by BYK in #309
  • Auto-correct ':' to '=' in --field values with a warning by BYK in #302

Formatters

  • Expand streaming table to fill terminal width by betegon in #314
  • Fix HTML entities and escaped underscores in table output by betegon in #313

Setup

  • Suppress agent skills and welcome messages on upgrade by BYK in #328
  • Suppress shell completion messages on upgrade by BYK in #326

Upgrade

  • Detect downgrades and skip delta attempt by BYK in #358
  • Check GHCR for nightly version existence instead of GitHub Releases by BYK in #352
  • Replace Bun.mmap with arrayBuffer on all platforms by BYK in #343
  • Replace Bun.mmap with arrayBuffer on macOS to prevent SIGKILL by BYK in #340
  • Use MAP_PRIVATE mmap to prevent macOS SIGKILL during delta upgrade by BYK in #339

Other

  • (ci) Generate JUnit XML to silence codecov-action warnings by BYK in #300
  • (install) Fix nightly digest extraction on macOS by BYK in #331
  • (logger) Inject --verbose and --log-level as proper Stricli flags by BYK in #353
  • (nightly) Push to GHCR from artifacts dir so layer titles are bare filenames by BYK in #301
  • (project create) Auto-correct dot-separated platform to hyphens by BYK in #336
  • (region) Resolve DSN org prefix at resolution layer by BYK in #316
  • (test) Handle 0/-0 in getComparator anti-symmetry property test by BYK in #308
  • (trace-logs) Timestamp_precise is a number, not a string by BYK in #323

Documentation 📚

  • Document SENTRY_URL and self-hosted setup by BYK in #337

Internal Changes 🔧

Api

  • Upgrade @sentry/api to 0.21.0, remove raw HTTP pagination workarounds by BYK in #321
  • Wire listIssuesPaginated through @sentry/api SDK for type safety by BYK in #310

Other

  • (craft) Add sentry-release-registry target by BYK in #325
  • (errors) Return Result type from withAuthGuard, expand auto-login to expired tokens by BYK in #359
  • (project create) Migrate human output to markdown rendering system by BYK in #341
  • (telemetry) Add child spans to delta upgrade for bottleneck identification by BYK in #355
  • (upgrade) Use copy-then-mmap for zero JS heap during delta patching by BYK in #344

🤖 This preview updates automatically when you update the PR.

@BYK BYK marked this pull request as ready for review March 5, 2026 18:52
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

BYK added 2 commits March 5, 2026 18:56
… patterns

Add withAuthGuard<T>(fn, fallback, onError?) to src/lib/errors.ts that
encapsulates the 'try op, rethrow AuthError, return fallback' pattern
used across 16 call sites.

Changes:
- Add withAuthGuard helper function to errors.ts with JSDoc
- Convert 14 clean catch blocks (rethrow auth, return fallback)
- Convert 2 catch blocks using onError callback (project/create.ts
  stores listTeamsError, resolve-target.ts throws ResolutionError on 404)
- Add unit tests (9 cases) and property-based tests (3 properties)
- Expand executeWithAutoAuth in bin.ts to also handle AuthError('expired')
  in addition to 'not_authenticated', triggering interactive login flow
- Update app.ts to re-throw 'expired' AuthErrors for the auto-login flow

Files converted:
- src/lib/api-client.ts (4 sites)
- src/lib/resolve-target.ts (4 sites including fetchProjectId with onError)
- src/lib/org-list.ts (2 sites)
- src/commands/project/list.ts (4 sites)
- src/commands/project/view.ts (1 site)
- src/commands/project/create.ts (1 site with onError)
…login to expired tokens

Replace withAuthGuard's fallback+onError API with a discriminated
Result type (AuthGuardResult<T>) that gives callers direct access to
caught errors. This eliminates mutable error variables and closure-based
error handlers while keeping the same auth-propagation semantics.

Changes:
- withAuthGuard now returns { ok, value } | { ok, error } instead of
  accepting fallback and onError parameters
- Export AuthGuardSuccess<T>, AuthGuardFailure, AuthGuardResult<T> types
- Convert 4 remaining manual instanceof AuthError catch patterns
  (region.ts, project/list.ts handleExplicit, issue/list.ts
  fetchIssuesForTarget)
- Expand auto-login flow in bin.ts and app.ts to also trigger on
  AuthError("expired"), with context-aware messaging
- Update telemetry.ts to suppress Sentry capture for expired tokens

12 files changed, net -45 lines.
@BYK BYK force-pushed the refactor/with-auth-guard-result-type branch from 7ae1838 to 3ecdd80 Compare March 5, 2026 18:57
@github-actions
Copy link
Contributor

github-actions bot commented Mar 5, 2026

Codecov Results 📊

101 passed | Total: 101 | Pass Rate: 100% | Execution Time: 0ms

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

✅ Patch coverage is 83.72%. Project has 3526 uncovered lines.
✅ Project coverage is 81.61%. Comparing base (base) to head (head).

Files with missing lines (11)
File Patch % Lines
api-client.ts 61.85% ⚠️ 430 Missing
telemetry.ts 72.64% ⚠️ 142 Missing
resolve-target.ts 78.55% ⚠️ 136 Missing
list.ts 87.07% ⚠️ 95 Missing
list.ts 85.89% ⚠️ 56 Missing
region.ts 68.24% ⚠️ 27 Missing
view.ts 88.73% ⚠️ 23 Missing
app.ts 81.03% ⚠️ 22 Missing
org-list.ts 94.65% ⚠️ 19 Missing
create.ts 95.88% ⚠️ 10 Missing
errors.ts 97.12% ⚠️ 7 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    81.52%    81.61%    +0.09%
==========================================
  Files          127       127         —
  Lines        19227     19177       -50
  Branches         0         0         —
==========================================
+ Hits         15673     15651       -22
- Misses        3554      3526       -28
- Partials         0         0         —

Generated by Codecov Action

@BYK BYK merged commit 5755d04 into main Mar 5, 2026
20 checks passed
@BYK BYK deleted the refactor/with-auth-guard-result-type branch March 5, 2026 20:45
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