Skip to content

Errors carry a code, a hint and the Java cause, with a subclass for each common cause - #483

Merged
tae898 merged 1 commit into
mainfrom
feat/475-error-codes
Nov 24, 2026
Merged

tae898 merged 1 commit into
mainfrom
feat/475-error-codes

Conversation

@tae898

@tae898 tae898 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

An ArcadeDBError now says what went wrong and what to do next, in a form a program (or a model that writes the program) can read. It has code (a short stable name, such as duplicate_key), hint (one sentence), cause (the Java exception class that decided the code) and to_dict(), and there is a subclass for each cause a program is likely to branch on. ArcadeDBError stays the base class, so every except ArcadeDBError keeps working.

try:
    with db.transaction():
        db.command("sql", "INSERT INTO Memory SET key = 'a'")
except DuplicateKeyError as e:
    print(e.code)  # duplicate_key
    print(e.hint)  # A unique index already holds this key: use another value, or update the record that has it (...).

The class and the code come from one table, _RULES in src/arcadedb/exceptions.py. A row is a Java class name, an optional message pattern, the code, the class and the hint. ArcadeDBError(text) picks the class itself from the Java exception being handled (or, with none, from the class names and wording in the text), so none of the 165 raise sites changed. The native build builds its errors from the chain of the reply (_native/database.py and _native/extras.py, 3 lines each), so both builds use the same table. A failure that no row knows stays ArcadeDBError with code = "unknown" and prints exactly the text it printed before.

The codes and classes (hints are on the new page how-to/errors.adoc):

Code Class
duplicate_key DuplicateKeyError
record_not_found RecordNotFoundError
transaction_not_begun TransactionError
database_exists DatabaseExistsError
database_not_found DatabaseNotFoundError
database_locked DatabaseLockedError
database_closed DatabaseClosedError
syntax_error QuerySyntaxError
timeout QueryTimeoutError
conflict ConflictError
type_not_found, already_exists, schema SchemaError
validation, type_mismatch ValidationError
database_read_only, query_not_idempotent, missing_parameter ArcadeDBError (code only)

I collected the Java classes and texts by running a probe of 54 cases on both wheels (26.12.1.dev0, JVM and native); the literals are in the tests.

Where I deviated from the design, and why

  • str(error) of a classified error is [code] hint followed by the old text, so the Java class and message stay at its end. An unknown error keeps its text unchanged. This is the one visible change for existing code: a program that compares str(e) for equality, or that checks startswith, sees the new head. Searches ("Transaction not begun" in str(e), pytest.raises(..., match=...)) still work. e.args is unchanged. Three docs places changed for this reason: the printed output of the quickstart and of databases and queries, and one startswith of the import page (now in). One existing test, test_native_tx_crossings.py, pinned the exact text and now checks the tail and the code.
  • The codes are names, not SQLSTATE. The embedded engine has no SQLSTATE on its exceptions (I searched the classes of com.arcadedb.exception in the 26.12.1 snapshot for an SQLSTATE or an error code and found none). Its new ErrorCategory enum is for the server's status codes and the native library does not expose it, so I did not build on it.
  • The subclasses are QuerySyntaxError and QueryTimeoutError, not SyntaxError and TimeoutError, so they do not shadow the builtins.
  • The causes database_read_only, query_not_idempotent and missing_parameter have a code and a hint but no subclass: a program rarely branches on them, and a subclass later is additive.
  • hint is None for unknown, not a filler sentence.
  • The classification reads sys.exc_info() in ArcadeDBError.__new__, which is what makes the 165 raise sites work unchanged. The alternative is an explicit factory at each site. If you prefer that, the table and the tests stay as they are.

The hint for transaction_not_begun and #479

The hint is "No transaction is open: wrap the write in with db.transaction():, or call db.set_auto_transaction(True) so that each statement commits on its own." It is true with auto-transaction off (today) and on (#479): with it on, the error only comes from commit() with nothing open or from set_auto_transaction(False), and both fixes still apply. The two PRs touch the same output line of quickstart.adoc; whichever merges second needs a one-line rebase.

What differs between the builds

The class and code are the same on both builds for all 42 live scenarios in tests/test_error_codes_parity.py (one set of literals, no branch on the backend). Three failures differ in what is below the code:

  • DROP PROPERTY and DROP INDEX of a missing name: the JVM engine throws SchemaException, the native one CommandExecutionException with the same message. A text row catches both, so the code is schema on both and cause differs.
  • An unknown SQL function: UnknownFunctionException on the JVM, CommandExecutionException on native. Both are unknown, and cause differs.
  • lookup_by_rid of a missing record: the JVM raises the engine's RecordNotFoundException, the native Python layer raises its own "record not found". Both are RecordNotFoundError; cause is None on native.

java_chain and native stay on the native build only.

Related issues

Closes #475

Docs

New page how-to/errors.adoc (in the nav after Transactions): the attributes, the agent recipe (except ArcadeDBError as e: e.code, e.hint, e.to_dict()), the table of codes with class, engine exception and hint, which error to retry, and what differs between the packages. tests/test_docs_errors_page.py runs its blocks and test_error_codes_table.py checks that every code, class and hint of the table is on the page. The outputs that print an error changed in quickstart.adoc and databases-and-queries.adoc. The Antora build has no warning.

Release note (draft)

Errors carry a stable code and a hint. ArcadeDBError has code, hint, cause and to_dict(), and the common causes are subclasses: DuplicateKeyError, RecordNotFoundError, TransactionError, DatabaseExistsError, DatabaseNotFoundError, DatabaseLockedError, DatabaseClosedError, QuerySyntaxError, QueryTimeoutError, ConflictError, SchemaError and ValidationError. Both packages give the same class and code for the same failure. except ArcadeDBError keeps working. The text of a classified error now starts with [code] hint and ends with the old text; an error with the code unknown prints exactly what it printed before. See the new page "Errors: codes, hints and classes".

Milestone

27.1.1, not 26.12.1. The change is additive in the API, but it is about 1,900 lines (1,200 of them tests), it changes str() of classified errors, and it touches the same docs lines as #479. That is more than I want in the 26.12.1 cut of 2 December. If you would rather have it there, the CI result below says whether the whole PR is green on both builds.

Tests

  • tests/test_error_codes_table.py: a literal (Java chain and text) for every row of the table, a test that fails when a row has no literal, the str and to_dict shape, hand-built subclasses, wrappers, pickle and copy.
  • tests/test_error_codes_parity.py: 42 live failures asserted on both builds with one set of literals, plus a second process that holds the lock.
  • tests/test_docs_errors_page.py: the blocks of the new page.
  • Local runs against the main wheels of 24 November with these Python files over them: the whole suite on the JVM wheel (4191 passed, 2 warnings) and on the native wheel (1 failed, 4242 passed, 117 skipped, 3 warnings; the skips are the counted NOTYET and NA-JVM kinds). The one native failure, test_native_tx_crossings.py::test_a_commit_without_begin_raises_the_engines_error_unchanged, pinned the exact text and is changed in this PR (endswith plus the code); I reran it and the new files on the native wheel afterwards (154 passed). CI builds its own wheels and runs the parity jobs.

Checklist

  • A regression test fails on the old code and passes on the new
  • The test suite that the change can affect passes (locally on both builds, CI to confirm)
  • uvx pre-commit run --files <changed files> passes

…ach common cause (closes #475)

An ArcadeDBError had one class and a Java text, so a program, or a model that writes the program, could only tell a duplicate key from a syntax error by searching the message. It now has `code`, `hint`, `cause` and `to_dict()`, and a subclass for duplicate key, record not found, transaction state, database exists, not found, locked and closed, syntax error, timeout, conflict, schema and validation. ArcadeDBError stays the base class.

One table (`_RULES` in exceptions.py) decides the class, the code and the hint: a Java class name, an optional message pattern, the code, the class, the hint. `ArcadeDBError(text)` classifies in `__new__` from the Java exception being handled, or from the class names in the text, so the 165 raise sites are unchanged. The native build classifies the chain of the reply (`_native/database.py`, `_native/extras.py`), so both builds give the same class and code. A failure that no row knows stays ArcadeDBError with code "unknown" and keeps its text.

The text of a classified error is `[code] hint` followed by the old text. Two places that compared the text exactly changed: the import page (`startswith` became `in`) and test_native_tx_crossings (`==` became `endswith` plus the code).

New page how-to/errors.adoc with the table and the agent recipe. Tests: a literal per row of the table, 42 live failures on both builds with one set of literals, the blocks of the new page.
@tae898 tae898 added this to the 27.1.1 milestone Nov 24, 2026
@tae898 tae898 added enhancement New feature or request native The arcadedb package (native library build) jvm The arcadedb-jvm package (JPype build) labels Nov 24, 2026
@tae898 tae898 self-assigned this Nov 24, 2026
@tae898
tae898 merged commit 426b1ca into main Nov 24, 2026
25 checks passed
@tae898
tae898 deleted the feat/475-error-codes branch November 24, 2026 17:25
@tae898 tae898 modified the milestones: 27.1.1, 26.12.1 Nov 24, 2026
@tae898

tae898 commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator Author

Merged, and moved from 27.1.1 to 26.12.1. I merged it into main ahead of the 2 December cut so that a week of main soaks it, and so that the Python API fitness study (humemai/mmdbbench#183) can measure the build with the error codes in it. The CI of this PR was green on both builds (25 checks, 0 not-success).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request jvm The arcadedb-jvm package (JPype build) native The arcadedb package (native library build)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Errors are one class with a Java text: no code, no hint, no subclass for a duplicate key or a conflict

1 participant