simplify error check logic

Signed-off-by: Alexander Onnikov <Alexander.Onnikov@xored.com>
This commit is contained in:
Alexander Onnikov
2025-09-11 23:34:20 +07:00
parent 55b9a4d54e
commit 17463dd657
2 changed files with 3 additions and 9 deletions
+3 -1
View File
@@ -6,6 +6,7 @@ use bytes::{Bytes, BytesMut};
use futures::stream::StreamExt;
use futures_util::Stream;
use size::Size;
use tokio_postgres::error::SqlState;
use tracing::*;
use crate::handlers::ApiError;
@@ -167,7 +168,8 @@ async fn make_blob(pool: &Pool, s3_key: &String, hash: &str) -> Result<Option<St
match postgres::insert_blob(&pool, &s3_key, &hash).await {
Ok(_) => break Ok(None),
Err(e) => {
if postgres::is_unique_constraint_violation(&e) {
if matches!(e, DbError::Db(ref db_err) if db_err.code() == Some(&SqlState::UNIQUE_VIOLATION))
{
debug!("concurrent upload detected");
if let Some(s3_key_found) = postgres::find_blob_by_hash(&pool, &hash).await? {
-8
View File
@@ -4,7 +4,6 @@ use bb8_postgres::PostgresConnectionManager;
use bytes::Bytes;
use serde::de::DeserializeOwned;
use tokio_postgres::NoTls;
use tokio_postgres::error::SqlState;
use tokio_postgres::{self as pg};
use tracing::*;
@@ -209,10 +208,3 @@ pub async fn set_part<D: serde::Serialize>(
Ok(())
}
pub fn is_unique_constraint_violation(e: &DbError) -> bool {
match e {
DbError::Db(e) => e.code() == Some(&SqlState::UNIQUE_VIOLATION),
_ => false,
}
}