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.
This commit is contained in:
parent
4e411e0488
commit
a329806a33
1 changed files with 15 additions and 2 deletions
|
|
@ -90,7 +90,11 @@ async def repo_create(table: str, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
data["updated"] = datetime.now(timezone.utc)
|
data["updated"] = datetime.now(timezone.utc)
|
||||||
try:
|
try:
|
||||||
async with db_connection() as connection:
|
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:
|
except RuntimeError as e:
|
||||||
logger.error(str(e))
|
logger.error(str(e))
|
||||||
raise
|
raise
|
||||||
|
|
@ -168,7 +172,16 @@ async def repo_insert(
|
||||||
"""Create a new record in the specified table"""
|
"""Create a new record in the specified table"""
|
||||||
try:
|
try:
|
||||||
async with db_connection() as connection:
|
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:
|
except Exception as e:
|
||||||
if ignore_duplicates and "already contains" in str(e):
|
if ignore_duplicates and "already contains" in str(e):
|
||||||
return []
|
return []
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue