| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
Demonstrates comprehensive database error handling patterns using SQLx and masterror.
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
| Back | FazBrowse Home | New Git URL |