Demonstrates comprehensive database error handling patterns using SQLx and masterror.
- Connection Error Handling - Database connection failures
- Query Error Mapping - SQL errors to domain errors
- Constraint Violation Handling - Unique/foreign key violations
- Transaction Error Patterns - Rollback and commit handling
- Row Not Found - Handling missing data gracefully
cd examples/sqlx-database
cargo runThis example uses an in-memory SQLite database, so no external setup is required.
// Mapped to AppError::Database
sqlx::SqlitePool::connect("sqlite::memory:").await?// Mapped to AppError::NotFound
sqlx::query_as::<_, User>("SELECT * FROM users WHERE id = ?")
.bind(id)
.fetch_one(&pool)
.await?// Mapped to AppError::Conflict
sqlx::query("INSERT INTO users (id, email) VALUES (?, ?)")
.bind(id)
.bind(email)
.execute(&pool)
.await?let mut tx = pool.begin().await?;
// Operations...
tx.commit().await?; // Or tx.rollback() on errormasterror automatically converts SQLx errors:
| SQLx Error | AppError Kind | HTTP Status |
|---|---|---|
RowNotFound |
NotFound |
404 |
UniqueViolation |
Conflict |
409 |
ForeignKeyViolation |
Conflict |
409 |
ConnectionError |
Database |
500 |
| Other database errors | Database |
500 |
cargo testMIT