Skip to content

Commit a4aa22b

Browse files
authored
fix: error handling for account creation flow (#7585)
1 parent 004536f commit a4aa22b

1 file changed

Lines changed: 25 additions & 24 deletions

File tree

  • apps/labrinth/src/routes/internal

‎apps/labrinth/src/routes/internal/flows.rs‎

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,25 +2003,26 @@ impl AccountRegisterFlow {
20032003
self,
20042004
transaction: &mut PgTransaction<'_>,
20052005
redis: &RedisPool,
2006-
) -> Result<ReadyAccountRegisterFlow, AccountRegisterValidateError> {
2007-
validator::Validate::validate(&self).map_err(|err| {
2008-
AccountRegisterValidateError::InvalidInput(
2006+
) -> Result<
2007+
Result<ReadyAccountRegisterFlow, AccountRegisterValidateError>,
2008+
ApiError,
2009+
> {
2010+
if let Err(err) = validator::Validate::validate(&self) {
2011+
return Ok(Err(AccountRegisterValidateError::InvalidInput(
20092012
validation_errors_to_string(err, None),
2010-
)
2011-
})?;
2013+
)));
2014+
}
20122015

20132016
if crate::database::models::DBUser::get(
20142017
&self.username,
20152018
&mut *transaction,
20162019
redis,
20172020
)
20182021
.await
2019-
.map_err(|err| {
2020-
AccountRegisterValidateError::InvalidInput(err.to_string())
2021-
})?
2022+
.wrap_internal_err("fetching existing user with username")?
20222023
.is_some()
20232024
{
2024-
return Err(AccountRegisterValidateError::UsernameTaken);
2025+
return Ok(Err(AccountRegisterValidateError::UsernameTaken));
20252026
}
20262027

20272028
let score =
@@ -2032,23 +2033,23 @@ impl AccountRegisterFlow {
20322033
.feedback()
20332034
.and_then(|x| x.warning())
20342035
.map(|w| w.to_string());
2035-
return Err(AccountRegisterValidateError::WeakPassword(feedback));
2036+
return Ok(Err(AccountRegisterValidateError::WeakPassword(
2037+
feedback,
2038+
)));
20362039
}
20372040

20382041
if !crate::database::models::DBUser::get_by_case_insensitive_email(
20392042
&self.email,
20402043
&mut *transaction,
20412044
)
20422045
.await
2043-
.map_err(|err| {
2044-
AccountRegisterValidateError::InvalidInput(err.to_string())
2045-
})?
2046+
.wrap_internal_err("fetching existing user with email")?
20462047
.is_empty()
20472048
{
2048-
return Err(AccountRegisterValidateError::DuplicateEmail);
2049+
return Ok(Err(AccountRegisterValidateError::DuplicateEmail));
20492050
}
20502051

2051-
Ok(ReadyAccountRegisterFlow { inner: self })
2052+
Ok(Ok(ReadyAccountRegisterFlow { inner: self }))
20522053
}
20532054
}
20542055

@@ -2165,16 +2166,16 @@ pub async fn validate_create_account_with_password(
21652166
pool: Data<PgPool>,
21662167
redis: Data<RedisPool>,
21672168
new_account: web::Json<NewAccount>,
2168-
) -> Result<(), AccountRegisterValidateError> {
2169-
let mut transaction = pool.begin().await.map_err(|err| {
2170-
AccountRegisterValidateError::InvalidInput(err.to_string())
2171-
})?;
2169+
) -> Result<Result<(), AccountRegisterValidateError>, ApiError> {
2170+
let mut transaction = pool
2171+
.begin()
2172+
.await
2173+
.wrap_internal_err("beginning transaction")?;
21722174

21732175
AccountRegisterFlow::from(new_account.into_inner())
21742176
.validate(&mut transaction, &redis)
2175-
.await?;
2176-
2177-
Ok(())
2177+
.await
2178+
.map(|r| r.map(drop))
21782179
}
21792180

21802181
/// Create account with a password.
@@ -2221,8 +2222,8 @@ pub async fn create_account_with_password(
22212222

22222223
let ready_flow = AccountRegisterFlow::from(new_account)
22232224
.validate(&mut transaction, &redis)
2224-
.await
2225-
.wrap_internal_err("validating ready flow")?;
2225+
.await?
2226+
.wrap_request_err("invalid account details")?;
22262227

22272228
let res = ready_flow
22282229
.execute(req, &mut transaction, &redis, &email)

0 commit comments

Comments
 (0)