Skip to content

Fix ApiError.code fallback for Databricks-specific error codes#181

Draft
parthban-db wants to merge 1 commit into
mainfrom
parthban-db/stack/bugbash-bug2-apierror-code
Draft

Fix ApiError.code fallback for Databricks-specific error codes#181
parthban-db wants to merge 1 commit into
mainfrom
parthban-db/stack/bugbash-bug2-apierror-code

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

Fixes ApiError.code resolving to Code.UNKNOWN for Databricks-specific errors and adds a new public ApiError.errorCode field carrying the raw error_code string.

Why

Databricks APIs return product-specific codes (e.g. a 404 with "error_code": "CATALOG_DOES_NOT_EXIST"), but fromHttpError only fell back to the HTTP status code when error_code was missing or an integer. For string codes it called codeFromString unconditionally, which returns Code.UNKNOWN for any non-canonical name, so code was UNKNOWN for essentially all real errors and the raw string was discarded entirely. This restores a meaningful canonical code and preserves the original string for direct matching.

What changed

  • fromHttpError now falls back to toCode(httpStatusCode) when a string error_code does not map to a canonical Code, so a 404 resolves to Code.NOT_FOUND; canonical strings and integer/missing codes are unchanged.
  • Adds public readonly ApiError.errorCode: string (and optional ApiErrorOptions.errorCode) holding the raw response string, or '' when none was present.
  • Unifies the string and non-string error_code branches into one path that computes the raw string once, maps it, then falls back on a miss.
  • Diverges from the Go SDK, which has the same canonical-code gap but already preserves the raw string on APIError.ErrorCode; this brings the TypeScript SDK to parity and adds the status-code fallback.

Validated: @databricks/sdk-core build, tests (357 pass), typecheck, and lint all clean.

@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.

## Summary

Fixes `ApiError.code` resolving to `Code.UNKNOWN` for every Databricks-specific error, and exposes the raw `error_code` string as a new `ApiError.errorCode` field so callers can match Databricks-specific codes precisely.

## Why

Databricks APIs return product-specific error codes in the `error_code` field (e.g. a 404 with `"error_code": "CATALOG_DOES_NOT_EXIST"`). `ApiError.fromHttpError` called `codeFromString(error_code)` unconditionally for string codes, but `codeFromString` only knows the canonical gRPC code names; any Databricks-specific string is a table miss and returns `Code.UNKNOWN`. Only the non-string branch (integer or missing `error_code`) fell back to `toCode(httpStatusCode)`. As a result, the canonical `code` was `UNKNOWN` for essentially all real Databricks errors, so a guard like `if (err.code === Code.NOT_FOUND)` was always false even on a genuine 404. Callers also had no way to recover the original `error_code` string, so they could not match on Databricks-specific codes at all.

This is a deliberate divergence from the Go SDK. The Go SDK has the identical canonical-code gap, but it always preserves the raw `error_code` string on a dedicated `APIError.ErrorCode` field. The TypeScript SDK had collapsed the error down to only the canonical `code`, dropping the raw string entirely. This PR closes the gap on both fronts: it adds the status-code fallback so the canonical `code` is meaningful, and it reintroduces the raw string as `errorCode` to match the information the Go SDK preserves.

## What changed

### Interface changes

- **`ApiError.errorCode: string`** — New public readonly field carrying the raw, Databricks-specific error code string from the response (e.g. `"CATALOG_DOES_NOT_EXIST"`). It is `''` when the response did not carry a string error code. Use it to match codes that have no canonical `Code` equivalent.
- **`ApiErrorOptions.errorCode?: string`** — New optional constructor option backing the field.

### Behavioral changes

- When `error_code` is a string that does not map to a canonical `Code` (i.e. `codeFromString` returns `Code.UNKNOWN`), `fromHttpError` now falls back to `toCode(httpStatusCode)` instead of leaving `code` as `UNKNOWN`. A 404 with a Databricks-specific `error_code` now resolves `code` to `Code.NOT_FOUND`. Known canonical string codes (e.g. `"NOT_FOUND"`) continue to map directly, and integer or missing `error_code` values continue to fall back to the status code. The raw string is preserved on the new `errorCode` field regardless of whether the canonical mapping succeeded.

### Internal changes

- Unified the string and non-string `error_code` paths in `fromHttpError`: the raw string (or `''`) is computed once, mapped through `codeFromString`, and falls back to `toCode(statusCode)` on a miss for both paths.

## How is this tested?

Updated `packages/core/tests/apierror/apierror.test.ts`. The case that previously locked in the bug (a Databricks-specific string code asserting `Code.UNKNOWN`) now asserts the corrected status-code fallback (`CATALOG_DOES_NOT_EXIST` on a 404 resolves to `Code.NOT_FOUND` and preserves `errorCode`). The `fromHttpError` runner now also asserts `errorCode` on every case, and the known-canonical-string cases assert their preserved raw string. `npm run build`, `npm test` (357 tests pass), `npm run typecheck`, and `npm run lint` for `@databricks/sdk-core` are all clean.

Co-authored-by: Isaac
@parthban-db parthban-db force-pushed the parthban-db/stack/bugbash-bug2-apierror-code branch from 8671804 to 357b416 Compare June 4, 2026 13:02
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