fix: improve error handling in repo_create and repo_insert (#474)
Some checks are pending
Development Build / extract-version (push) Waiting to run
Development Build / test-build (push) Blocked by required conditions
Development Build / summary (push) Blocked by required conditions

* 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.
This commit is contained in:
Luis Novo 2026-01-25 15:11:51 -03:00 committed by GitHub
parent 4e411e0488
commit a329806a33
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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 []