Skip to content

Error Handling

Bavard throws specific exceptions that you can catch to handle errors gracefully. All exceptions extend BavardException.

Common Exceptions

ExceptionDescription
ModelNotFoundExceptionThrown by findOrFail() or firstOrFail() when no record exists.
QueryExceptionThrown when a database query fails (syntax error, constraint violation).
TransactionExceptionThrown when a transaction fails or is rolled back.
InvalidQueryExceptionThrown when query construction is invalid (e.g., bad operator).
MassAssignmentExceptionThrown when trying to mass-assign a guarded attribute (if configured).
RelationNotFoundExceptionThrown when accessing an undefined relationship.
DatabaseNotInitializedExceptionThrown if DatabaseManager is used before setup.

Example

dart
try {
  final user = await User().query().findOrFail(999);
} on ModelNotFoundException catch (e) {
  print('User not found: ${e.message}');
} on QueryException catch (e) {
  print('Database error: ${e.message}');
  print('SQL: ${e.sql}');
}

Released under the MIT License.