Skip to content

fix(hrana): populate rawCode on errors from remote databases - #357

Open
OsamaAnsar wants to merge 1 commit into
tursodatabase:mainfrom
OsamaAnsar:fix/hrana-error-raw-code
Open

fix(hrana): populate rawCode on errors from remote databases#357
OsamaAnsar wants to merge 1 commit into
tursodatabase:mainfrom
OsamaAnsar:fix/hrana-error-raw-code

Conversation

@OsamaAnsar

Copy link
Copy Markdown

Closes #117.

Errors from remote databases (hrana / SQL-over-HTTP) were always constructed with rawCode: undefined, while errors from a local sqlite3 database carry it. The SQL-over-HTTP protocol only sends the string error code (proto.Error is { message, code }), so this derives the numeric code from the string when it is a SQLITE_* one.

Changes

  • sqlite_error_codes.ts (new) — moves the shared SQLITE_* code table and mapToBaseCode out of the node-only sqlite3.ts so hrana.ts (which also runs on the web) can use them without pulling in libsql. Adds mapToRawCode(code): resolves a base name (SQLITE_CONSTRAINT → 19) or an extended name (SQLITE_CONSTRAINT_PRIMARYKEY → 19) to the base numeric code, and returns undefined for anything not recognised, including the client's own non-SQLite codes (HRANA_PROTO_ERROR, SERVER_ERROR, …).
  • hrana.tsmapHranaError now passes mapToRawCode(code) instead of undefined.
  • sqlite3.ts — imports the moved helpers; behaviour unchanged.

Tests

  • __tests__/sqlite_error_codes.test.ts (new) — unit tests for mapToBaseCode / mapToRawCode, including extended-code resolution, non-SQLite codes, and a round-trip.
  • __tests__/client.test.ts — the constraint-error cases now assert rawCode is defined and rawCode & 0xff === 19, which holds for both the local and remote backends.

tsc --noEmit and npm run build (CJS + ESM) pass; prettier clean.

Remote (hrana/HTTP) errors were always constructed with rawCode undefined,
while local sqlite3 errors carry it. The SQL-over-HTTP protocol only sends
the string error code, so derive the numeric one from it: extract the
shared SQLITE_* table and mapToBaseCode into sqlite_error_codes.ts and add
mapToRawCode, which resolves base and extended code names to the base
numeric code and returns undefined for non-SQLite codes.

Closes tursodatabase#117
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.

Remote databases don't support raw error code

1 participant