This commit is contained in:
Brandon Thomas committed 2026-05-13 20:57:51 -04:00
1 parent b781c3caad
commit 441241ecf7
3 files changed
+26 -17

No files matched your search

@@ -13,6 +13,7 @@ pub enum DatabaseInsertError {
/// An uncategorized non-database error occurred.
/// This will likely result in a 500 for the user.
#[deprecated(note = "This is not very semantic. Get rid of it. Stop using it.")]
AnyhowError(anyhow::Error),
}
@@ -1,3 +1,4 @@
use crate::errors::database_insert_error::DatabaseInsertError;
use sqlx::{Executor, MySql};
use tokens::tokens::user_referral_codes::UserReferralCodeToken;
use tokens::tokens::users::UserToken;
@@ -11,7 +12,7 @@ pub struct CreateReferralCodeArgs<'a> {
pub async fn create_referral_code<'e, 'c: 'e, E>(
args: CreateReferralCodeArgs<'_>,
mysql_executor: E,
) -> Result<UserReferralCodeToken, sqlx::Error>
) -> Result<UserReferralCodeToken, DatabaseInsertError>
where
E: 'e + Executor<'c, Database = MySql>,
{
@@ -33,7 +34,8 @@ VALUES (?, ?, ?, ?)
args.owner_user_token.as_str(),
)
.execute(mysql_executor)
.await?;
.await
.map_err(DatabaseInsertError::from)?;
Ok(token)
}
@@ -8,6 +8,7 @@ use utoipa::ToSchema;
use artcraft_api_defs::user_referral_codes::create_referral_code::{CreateReferralCodeRequest, CreateReferralCodeResponse};
use crate::http_server::common_responses::advanced_common_web_error::AdvancedCommonWebError;
use crate::state::server_state::ServerState;
use mysql_queries::errors::database_insert_error::DatabaseInsertError;
use mysql_queries::queries::user_referral_codes::create_referral_code::{create_referral_code, CreateReferralCodeArgs};
use mysql_queries::queries::user_referral_codes::list_referral_codes_for_user::list_referral_codes_for_user;
@@ -84,7 +85,7 @@ pub async fn create_referral_code_handler(
}
// Insert the code. If code_lowercase is already taken, we get a duplicate key error.
let token = create_referral_code(
let result = create_referral_code(
CreateReferralCodeArgs {
owner_user_token: user_token,
code: &code,
@@ -93,18 +94,23 @@ pub async fn create_referral_code_handler(
&mut *mysql_connection,
).await;
match token {
Ok(token) => Ok(Json(CreateReferralCodeResponse {
success: true,
token,
code,
code_lowercase,
})),
Err(sqlx::Error::Database(db_err)) if db_err.code().as_deref() == Some("23000") => {
Err(AdvancedCommonWebError::BadInputWithSimpleMessage(
"This referral code is already in use".to_string(),
))
}
Err(e) => Err(AdvancedCommonWebError::from(e)),
}
let token = match result {
Ok(token) => token,
Err(err) => return match err {
DatabaseInsertError::DuplicateKeyError => {
return Err(AdvancedCommonWebError::BadInputWithSimpleMessage(
"This referral code is already in use".to_string(),
));
},
DatabaseInsertError::SqlxError(e) => Err(AdvancedCommonWebError::from(e)),
DatabaseInsertError::AnyhowError(e) => Err(AdvancedCommonWebError::from_anyhow_error(e)),
},
};
Ok(Json(CreateReferralCodeResponse {
success: true,
token,
code,
code_lowercase,
}))
}