Skip to content

[auth] Throw exported, coded PatCredentialsError from PAT credentials#186

Merged
parthban-db merged 1 commit into
mainfrom
parthban-db/stack/bugbash-bug12-pat-error
Jun 4, 2026
Merged

[auth] Throw exported, coded PatCredentialsError from PAT credentials#186
parthban-db merged 1 commit into
mainfrom
parthban-db/stack/bugbash-bug12-pat-error

Conversation

@parthban-db
Copy link
Copy Markdown
Contributor

@parthban-db parthban-db commented Jun 3, 2026

🥞 Stacked PR

Use this link to review incremental changes.


Summary

Replaces the private, code-less TokenRequiredError thrown by PAT credentials with an exported, coded PatCredentialsError, bringing PAT in line with the M2M and U2M credential error conventions.

Why

The three credential strategies in @databricks/sdk-auth threw inconsistent errors. M2M (newM2mCredentials) and U2M (newU2mCredentials) threw exported, coded errors (M2mCredentialsError/U2mCredentialsError) defined in credentials/errors.ts, each carrying a code discriminant so callers can branch on the cause and a class that is re-exported from the credential barrels. PAT (newPatCredentials) instead threw a private TokenRequiredError declared inline in pat.ts with no code field and no export from any barrel. Callers could not instanceof-check the error type or switch on a code the way they can for the other two strategies; the only signal was a string message. This finding makes PAT consistent with the established convention so error handling is uniform across all credential constructors.

How is this tested?

npm run build, npm test (95 tests pass), npm run test:browser (72 tests pass), npm run typecheck, npm run lint, and npm run format:check all pass for @databricks/sdk-auth after building @databricks/sdk-core first. The PAT test suite now asserts PatCredentialsError and its TOKEN_REQUIRED code instead of the removed TokenRequiredError.

Co-authored-by: Isaac

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 3, 2026

Please ensure that the NEXT_CHANGELOG.md file is updated with any relevant changes.
If this is not necessary for your PR, please include the following in your PR description:
NO_CHANGELOG=true
and rerun the job.

@parthban-db parthban-db requested a review from rauchy June 4, 2026 10:31
@parthban-db parthban-db marked this pull request as ready for review June 4, 2026 10:31
Comment thread packages/auth/src/credentials/errors.ts Outdated
## Summary

Replaces the private, code-less `TokenRequiredError` thrown by PAT credentials with an exported, coded `PatCredentialsError`, bringing PAT in line with the M2M and U2M credential error conventions.

## Why

The three credential strategies in `@databricks/sdk-auth` threw inconsistent errors. M2M (`newM2mCredentials`) and U2M (`newU2mCredentials`) threw exported, coded errors (`M2mCredentialsError`/`U2mCredentialsError`) defined in `credentials/errors.ts`, each carrying a `code` discriminant so callers can branch on the cause and a class that is re-exported from the credential barrels. PAT (`newPatCredentials`) instead threw a private `TokenRequiredError` declared inline in `pat.ts` with no `code` field and no export from any barrel. Callers could not `instanceof`-check the error type or switch on a code the way they can for the other two strategies; the only signal was a string message. This finding makes PAT consistent with the established convention so error handling is uniform across all credential constructors.

## What changed

### Interface changes

- **`PatCredentialsError`** — new exported error class in `credentials/errors.ts`, following the same shape as `M2mCredentialsError`: a `readonly code` field, a `(code, message)` constructor, and `name = 'PatCredentialsError'`. Re-exported from the credentials barrel (`credentials/index.ts`) and the browser barrel (`credentials/index.browser.ts`), matching how `M2mCredentialsError` is exposed from both.
- **`PatCredentialsErrorCode`** — new exported discriminant union `'TOKEN_REQUIRED' | 'TOKEN_MALFORMED'`. `TOKEN_REQUIRED` is thrown for an empty token. `TOKEN_MALFORMED` is reserved for a separate token-trimming change landing on its own branch and is not yet thrown here.

### Behavioral changes

`newPatCredentials('')` now throws `PatCredentialsError` (`code: 'TOKEN_REQUIRED'`) instead of the unexported `TokenRequiredError`. The thrown message (`token is required`) is unchanged; the error `name` changes from `TokenRequiredError` to `PatCredentialsError`. This is intentional — `TokenRequiredError` was never part of the public API surface, so no documented consumer depended on it.

### Internal changes

- Removed the private `TokenRequiredError` class from `pat.ts`; `newPatCredentials` now imports and throws `PatCredentialsError` from the shared `errors.ts` module.
- Updated `tests/credentials/pat.test.ts` to assert the exported `PatCredentialsError`, its `code`, and its `message` via a table-driven `it.each`, replacing the previous test that locked in the `TokenRequiredError` name. The test imports the error directly from `credentials/errors` (not the full barrel) for the same reason the existing PAT and M2M tests do: the full barrel pulls in Node-only credentials that cannot load in browser test runs.

## How is this tested?

`npm run build`, `npm test` (95 tests pass), `npm run test:browser` (72 tests pass), `npm run typecheck`, `npm run lint`, and `npm run format:check` all pass for `@databricks/sdk-auth` after building `@databricks/sdk-core` first. The PAT test suite now asserts `PatCredentialsError` and its `TOKEN_REQUIRED` code instead of the removed `TokenRequiredError`.

Co-authored-by: Isaac
@parthban-db parthban-db force-pushed the parthban-db/stack/bugbash-bug12-pat-error branch from b1275df to 3fdf1e6 Compare June 4, 2026 12:07
@parthban-db parthban-db added this pull request to the merge queue Jun 4, 2026
Merged via the queue into main with commit 7ccadff Jun 4, 2026
22 of 24 checks passed
@parthban-db parthban-db deleted the parthban-db/stack/bugbash-bug12-pat-error branch June 4, 2026 12:56
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