[auth] Throw exported, coded PatCredentialsError from PAT credentials#186
Merged
Merged
Conversation
|
Please ensure that the NEXT_CHANGELOG.md file is updated with any relevant changes. |
rauchy
approved these changes
Jun 4, 2026
## 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
b1275df to
3fdf1e6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🥞 Stacked PR
Use this link to review incremental changes.
Summary
Replaces the private, code-less
TokenRequiredErrorthrown by PAT credentials with an exported, codedPatCredentialsError, bringing PAT in line with the M2M and U2M credential error conventions.Why
The three credential strategies in
@databricks/sdk-auththrew inconsistent errors. M2M (newM2mCredentials) and U2M (newU2mCredentials) threw exported, coded errors (M2mCredentialsError/U2mCredentialsError) defined incredentials/errors.ts, each carrying acodediscriminant so callers can branch on the cause and a class that is re-exported from the credential barrels. PAT (newPatCredentials) instead threw a privateTokenRequiredErrordeclared inline inpat.tswith nocodefield and no export from any barrel. Callers could notinstanceof-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, andnpm run format:checkall pass for@databricks/sdk-authafter building@databricks/sdk-corefirst. The PAT test suite now assertsPatCredentialsErrorand itsTOKEN_REQUIREDcode instead of the removedTokenRequiredError.Co-authored-by: Isaac