Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,27 @@ impl IntoResponse for AppError {

/// Convenience result alias used throughout the codebase.
pub type AppResult<T> = Result<T, AppError>;

/// True if `error` is a Postgres unique/primary-key constraint violation.
pub fn is_unique_violation(error: &sqlx::Error) -> bool {
matches!(error, sqlx::Error::Database(db_err) if db_err.is_unique_violation())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_conflict_status_and_code() {
let err = AppError::Conflict("Email".into());
let (status, code) = err.status_and_code();
assert_eq!(status, StatusCode::CONFLICT);
assert_eq!(code, "CONFLICT");
}

#[test]
fn test_is_unique_violation_non_db_error() {
let err = sqlx::Error::RowNotFound;
assert!(!is_unique_violation(&err));
}
}
9 changes: 8 additions & 1 deletion src/routes/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ pub async fn register(
.bind(req.full_name.trim())
.bind(&req.stellar_address)
.fetch_one(&state.pool)
.await?;
.await
.map_err(|e| {
if crate::error::is_unique_violation(&e) {
AppError::Conflict("Email".into())
} else {
AppError::Database(e)
}
})?;

let user = User::from(row);

Expand Down
8 changes: 1 addition & 7 deletions src/services/batch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
error::{AppError, AppResult},
error::{is_unique_violation, AppError, AppResult},
models::{
batch_payment::{BatchPaymentResult, SendBatchPaymentRequest},
transaction::TransactionStatus,
Expand All @@ -17,12 +17,6 @@ pub struct BatchPaymentService {
pool: PgPool,
}

/// True if `error` is a Postgres unique/primary-key violation — used to
/// detect losing the race to claim a `batch_submissions` row.
fn is_unique_violation(error: &sqlx::Error) -> bool {
matches!(error, sqlx::Error::Database(db_err) if db_err.is_unique_violation())
}

/// Decides what a failed submission attempt means for transaction status
/// (#30). The distinction is "did we get a definitive answer from Horizon
/// at all":
Expand Down