From a329806a33644043a6aa2d604f35c523e98e1d61 Mon Sep 17 00:00:00 2001 From: Luis Novo Date: Sun, 25 Jan 2026 15:11:51 -0300 Subject: [PATCH] fix: improve error handling in repo_create and repo_insert (#474) * fix: improve error handling in repo_create and repo_insert When SurrealDB encounters an error (e.g., schema validation failure), it may return a string error message instead of the expected record. Previously, this caused a confusing "'str' object has no attribute 'items'" error in base.py:save(). This change adds error string detection to repo_create() and repo_insert(), raising a RuntimeError with the actual SurrealDB error message. This helps debug issues like #469 by showing the underlying database error. Related to #469 * fix: preserve error details in repo_insert RuntimeError handling The RuntimeError with SurrealDB error message was being caught by the broad 'except Exception' block and replaced with a generic 'Failed to create record' message. Now RuntimeError is caught separately and re-raised, preserving the actual error details for debugging. --- open_notebook/database/repository.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/open_notebook/database/repository.py b/open_notebook/database/repository.py index 0d9c0b4..a150977 100644 --- a/open_notebook/database/repository.py +++ b/open_notebook/database/repository.py @@ -90,7 +90,11 @@ async def repo_create(table: str, data: Dict[str, Any]) -> Dict[str, Any]: data["updated"] = datetime.now(timezone.utc) try: async with db_connection() as connection: - return parse_record_ids(await connection.insert(table, data)) + result = parse_record_ids(await connection.insert(table, data)) + # SurrealDB may return a string error message instead of the expected record + if isinstance(result, str): + raise RuntimeError(result) + return result except RuntimeError as e: logger.error(str(e)) raise @@ -168,7 +172,16 @@ async def repo_insert( """Create a new record in the specified table""" try: async with db_connection() as connection: - return parse_record_ids(await connection.insert(table, data)) + result = parse_record_ids(await connection.insert(table, data)) + # SurrealDB may return a string error message instead of the expected records + if isinstance(result, str): + raise RuntimeError(result) + return result + except RuntimeError as e: + if ignore_duplicates and "already contains" in str(e): + return [] + logger.error(str(e)) + raise except Exception as e: if ignore_duplicates and "already contains" in str(e): return []