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.

Debugging with Stack Traces

All BavardException classes include an optional stackTrace property that preserves the original stack trace of the error. This is particularly useful for debugging TransactionException or QueryException where the error might originate deep within the database driver.

dart
try {
  await DatabaseManager().transaction((txn) async {
    // ... complex logic that might fail ...
  });
} on TransactionException catch (e) {
  print('Transaction failed: ${e.message}');
  
  // Access the original stack trace to find the root cause
  if (e.stackTrace != null) {
    print(e.stackTrace);
  }
}

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. Crafted with ❤️ for the Dart ecosystem.