chore: replace sqlx error with custom DatabaseError

This commit is contained in:
nathan 2023-10-13 11:19:33 +08:00
parent 7b77805d8d
commit fa3d679770
5 changed files with 44 additions and 32 deletions

View File

@ -15,6 +15,9 @@ pub enum DatabaseError {
#[error(transparent)]
UuidError(#[from] uuid::Error),
#[error(transparent)]
SqlxError(sqlx::Error),
#[error("Storage space not enough")]
StorageSpaceNotEnough,
@ -35,7 +38,7 @@ impl From<sqlx::Error> for DatabaseError {
fn from(value: sqlx::Error) -> Self {
match value {
Error::RowNotFound => DatabaseError::RecordNotFound,
_ => DatabaseError::Internal(value.into()),
_ => DatabaseError::SqlxError(value),
}
}
}

View File

@ -1,3 +1,4 @@
use database_entity::database_error::DatabaseError;
use database_entity::AFBlobMetadata;
use rust_decimal::prelude::ToPrimitive;
use sqlx::types::Decimal;
@ -10,7 +11,7 @@ pub async fn is_blob_metadata_exists(
pool: &PgPool,
workspace_id: &Uuid,
file_id: &str,
) -> Result<bool, sqlx::Error> {
) -> Result<bool, DatabaseError> {
let exists: (bool,) = sqlx::query_as(
r#"
SELECT EXISTS (
@ -35,8 +36,8 @@ pub async fn insert_blob_metadata(
workspace_id: &Uuid,
file_type: &str,
file_size: i64,
) -> Result<AFBlobMetadata, sqlx::Error> {
sqlx::query_as!(
) -> Result<AFBlobMetadata, DatabaseError> {
let metadata = sqlx::query_as!(
AFBlobMetadata,
r#"
INSERT INTO af_blob_metadata
@ -53,7 +54,8 @@ pub async fn insert_blob_metadata(
file_size
)
.fetch_one(pg_pool)
.await
.await?;
Ok(metadata)
}
#[instrument(level = "trace", skip_all, err)]
@ -61,8 +63,8 @@ pub async fn delete_blob_metadata(
pg_pool: &PgPool,
workspace_id: &Uuid,
file_id: &str,
) -> Result<AFBlobMetadata, sqlx::Error> {
sqlx::query_as!(
) -> Result<AFBlobMetadata, DatabaseError> {
let metadata = sqlx::query_as!(
AFBlobMetadata,
r#"
DELETE FROM af_blob_metadata
@ -73,7 +75,8 @@ pub async fn delete_blob_metadata(
file_id,
)
.fetch_one(pg_pool)
.await
.await?;
Ok(metadata)
}
#[instrument(level = "trace", skip_all, err)]
@ -81,8 +84,8 @@ pub async fn get_blob_metadata(
pg_pool: &PgPool,
workspace_id: &Uuid,
file_id: &str,
) -> Result<AFBlobMetadata, sqlx::Error> {
sqlx::query_as!(
) -> Result<AFBlobMetadata, DatabaseError> {
let metadata = sqlx::query_as!(
AFBlobMetadata,
r#"
SELECT * FROM af_blob_metadata
@ -92,14 +95,15 @@ pub async fn get_blob_metadata(
file_id,
)
.fetch_one(pg_pool)
.await
.await?;
Ok(metadata)
}
#[instrument(level = "trace", skip_all, err)]
pub async fn get_workspace_usage_size(
pool: &PgPool,
workspace_id: &Uuid,
) -> Result<u64, sqlx::Error> {
) -> Result<u64, DatabaseError> {
let row: (Option<Decimal>,) =
sqlx::query_as(r#"SELECT SUM(file_size) FROM af_blob_metadata WHERE workspace_id = $1;"#)
.bind(workspace_id)

View File

@ -1,4 +1,5 @@
use anyhow::{Context, Error};
use anyhow::Context;
use database_entity::database_error::DatabaseError;
use sqlx::PgPool;
use tracing::instrument;
@ -6,7 +7,7 @@ pub async fn update_user_name(
pool: &PgPool,
uuid: &uuid::Uuid,
name: &str,
) -> Result<(), sqlx::Error> {
) -> Result<(), DatabaseError> {
sqlx::query!(
r#"
UPDATE af_user
@ -27,7 +28,7 @@ pub async fn create_user_if_not_exists(
user_uuid: &uuid::Uuid,
email: &str,
name: &str,
) -> Result<bool, Error> {
) -> Result<bool, DatabaseError> {
let affected_rows = sqlx::query!(
r#"
INSERT INTO af_user (uuid, email, name)
@ -46,7 +47,7 @@ pub async fn create_user_if_not_exists(
.execute(pool)
.await
.context(format!(
"Fail to insert user. uuid: {}, name: {}, email: {}",
"Fail to insert user to db. uuid: {}, name: {}, email: {}",
user_uuid, name, email
))?
.rows_affected();
@ -54,7 +55,7 @@ pub async fn create_user_if_not_exists(
Ok(affected_rows > 0)
}
pub async fn uid_from_uuid(pool: &PgPool, gotrue_uuid: &uuid::Uuid) -> Result<i64, sqlx::Error> {
pub async fn uid_from_uuid(pool: &PgPool, gotrue_uuid: &uuid::Uuid) -> Result<i64, DatabaseError> {
let uid = sqlx::query!(
r#"
SELECT uid FROM af_user WHERE uuid = $1

View File

@ -3,13 +3,14 @@ use sqlx::{
PgPool,
};
use database_entity::database_error::DatabaseError;
use database_entity::{AFRole, AFUserProfileView, AFWorkspace, AFWorkspaceMember};
pub async fn select_all_workspaces_owned(
pool: &PgPool,
owner_uuid: &Uuid,
) -> Result<Vec<AFWorkspace>, sqlx::Error> {
sqlx::query_as!(
) -> Result<Vec<AFWorkspace>, DatabaseError> {
let workspaces = sqlx::query_as!(
AFWorkspace,
r#"
SELECT * FROM public.af_workspace WHERE owner_uid = (
@ -19,14 +20,15 @@ pub async fn select_all_workspaces_owned(
owner_uuid
)
.fetch_all(pool)
.await
.await?;
Ok(workspaces)
}
pub async fn select_user_is_workspace_owner(
pg_pool: &PgPool,
user_uuid: &Uuid,
workspace_uuid: &Uuid,
) -> Result<bool, sqlx::Error> {
) -> Result<bool, DatabaseError> {
let exists = sqlx::query_scalar!(
r#"
SELECT EXISTS(
@ -50,7 +52,7 @@ pub async fn insert_workspace_members(
workspace_id: &uuid::Uuid,
member_emails: &[String],
role: AFRole,
) -> Result<(), sqlx::Error> {
) -> Result<(), DatabaseError> {
sqlx::query!(
r#"
INSERT INTO public.af_workspace_member (workspace_id, uid, role_id)
@ -74,7 +76,7 @@ pub async fn delete_workspace_members(
pool: &PgPool,
workspace_id: &uuid::Uuid,
member_emails: &[String],
) -> Result<(), sqlx::Error> {
) -> Result<(), DatabaseError> {
sqlx::query!(
r#"
DELETE FROM public.af_workspace_member
@ -94,8 +96,8 @@ pub async fn delete_workspace_members(
pub async fn select_workspace_members(
pg_pool: &PgPool,
workspace_id: &uuid::Uuid,
) -> Result<Vec<AFWorkspaceMember>, sqlx::Error> {
sqlx::query_as!(
) -> Result<Vec<AFWorkspaceMember>, DatabaseError> {
let members = sqlx::query_as!(
AFWorkspaceMember,
r#"
SELECT af_user.email, af_workspace_member.role_id AS role
@ -106,14 +108,15 @@ pub async fn select_workspace_members(
workspace_id
)
.fetch_all(pg_pool)
.await
.await?;
Ok(members)
}
pub async fn select_user_profile_view_by_uuid(
pool: &PgPool,
user_uuid: &Uuid,
) -> Result<Option<AFUserProfileView>, sqlx::Error> {
sqlx::query_as!(
) -> Result<Option<AFUserProfileView>, DatabaseError> {
let user_profile = sqlx::query_as!(
AFUserProfileView,
r#"
SELECT *
@ -122,5 +125,6 @@ pub async fn select_user_profile_view_by_uuid(
user_uuid
)
.fetch_optional(pool)
.await
.await?;
Ok(user_profile)
}

View File

@ -45,9 +45,9 @@ impl actix_web::error::ResponseError for AppError {
//
impl From<anyhow::Error> for AppError {
fn from(err: anyhow::Error) -> Self {
match err.downcast_ref::<AppError>() {
None => AppError::new(ErrorCode::Unhandled, err.to_string()),
Some(err) => err.clone(),
match err.downcast::<AppError>() {
Err(err) => AppError::new(ErrorCode::Unhandled, err.to_string()),
Ok(err) => err,
}
}
}