Exceptions
Ryx's exception hierarchy mirrors Django's for familiarity.
Hierarchyโ
RyxError
โโโ DatabaseError # SQL / driver errors
โโโ PoolNotInitialized # ryx.setup() not called
โโโ DoesNotExist # .get() found 0 rows
โโโ MultipleObjectsReturned # .get() found >1 rows
โโโ FieldError # Unknown field in query
โโโ ValidationError # Field / model validation failure
.errors: dict[str, list[str]]
Model-Specific Exceptionsโ
Each model gets its own DoesNotExist and MultipleObjectsReturned:
try:
post = await Post.objects.get(pk=999)
except Post.DoesNotExist:
print("Post not found")
try:
post = await Post.objects.get(slug="duplicate")
except Post.MultipleObjectsReturned:
print("Multiple posts match")
ValidationErrorโ
from ryx import ValidationError
try:
await product.full_clean()
except ValidationError as e:
print(e.errors)
# โ {
# "name": ["Ensure this value has at least 3 characters."],
# "price": ["Ensure this value is greater than or equal to 0."],
# }
# Merge with another ValidationError
merged = e.merge(other_error)
Catching Database Errorsโ
from ryx import DatabaseError
try:
await raw_execute("INVALID SQL")
except DatabaseError as e:
print(f"Database error: {e}")