Fixed err lib

This commit is contained in:
Franklin 2023-10-27 20:42:34 -04:00
parent 55b1ad0029
commit 8f251aa80e

View File

@ -32,9 +32,12 @@ pub async fn register_user<'a>(
match x_u_res_db_or_res!(get_credential(transaction, credential_dto.credential.clone()).await) {
None => {}
Some(_) => {
return Err(Error::new(trace!()).error_type(ErrorType::Service(
ServiceError::AlreadyExistsError(String::from("Credential")),
)));
return Err(Error::new(trace!()).error_type(ErrorType::Service {
error:
ServiceError::AlreadyExistsError{
message: String::from("Credential")
},
}));
}
};
}
@ -65,21 +68,21 @@ pub async fn authenticate_user<'a>(
) -> Result<User, Error> {
let persisted_user = match x_u_res_db_or_res!(get_user_with_id(conn, &user.id).await) {
None => {
return Err(Error::new(trace!()).error_type(ErrorType::Service(ServiceError::NotFoundError(format!("User with id: {}", user.id)))));
return Err(Error::new(trace!()).error_type(ErrorType::Service { error: ServiceError::NotFoundError { message: format!("User with id: {}", user.id) } } ));
}
Some(persisted_user) => persisted_user,
};
match x_u_res_db_or_res!(validate_user_token(conn, &user.id, user.auth_token.clone()).await) {
None => return Err(Error::new(trace!()).error_type(ErrorType::Service(ServiceError::NotFoundError(format!("Auth Token {}", user.auth_token))))),
None => return Err(Error::new(trace!()).error_type(ErrorType::Service { error: ServiceError::NotFoundError { message: format!("Auth Token {}", user.auth_token) } } )),
Some(persisted_token) => {
// Check if persisted_token expired
if Utc::now().timestamp_millis() - persisted_token.last_updated.timestamp_millis()
> AUTH_TOKEN_EXPIRATION_TIME_MILLIS
{
Err(Error::new(trace!()).error_type(ErrorType::Service(ServiceError::IncorrectCredentialError(String::from(
Err(Error::new(trace!()).error_type(ErrorType::Service { error: ServiceError::IncorrectCredentialError{ message: String::from(
"Auth Token Expired. Use refresh token to get a new one.",
)))))
)}}))
} else {
// Not expired
Ok(persisted_user)
@ -95,7 +98,7 @@ pub async fn refresh_auth_token<'a>(
) -> Result<Token, Error> {
let _persisted_user = match x_u_res_db_or_res!(get_user_with_id(conn, &user.id).await) {
None => {
return Err(Error::new(trace!()).error_type(ErrorType::Service(ServiceError::NotFoundError(format!("User with id: {}", user.id)))));
return Err(Error::new(trace!()).error_type(ErrorType::Service { error: ServiceError::NotFoundError { message: format!("User with id: {}", user.id)}}));
}
Some(persisted_user) => persisted_user,
};
@ -106,7 +109,7 @@ pub async fn refresh_auth_token<'a>(
let new_auth_token = tokens.remove(0);
Ok(x_u_res_db_or_res!(update_token(conn, user.refresh_token, new_auth_token).await))
} else {
Err(Error::new(trace!()).error_type(ErrorType::Service(ServiceError::UnexpectedError(String::from("No tokens were created.")))))
Err(Error::new(trace!()).error_type(ErrorType::Service { error: ServiceError::UnexpectedError { message: String::from("No tokens were created.")}}))
}
}
@ -121,9 +124,9 @@ pub async fn reset_password(
// Change pass
Ok(u_res_or_res!(change_password(conn, persisted_user, &user.new_password).await))
} else {
Err(Error::new(trace!()).error_type(ErrorType::Service(ServiceError::IncorrectCredentialError(String::from(
Err(Error::new(trace!()).error_type(ErrorType::Service { error: ServiceError::IncorrectCredentialError { message: String::from(
"Password incorrect.",
)))))
)}}))
}
}
@ -137,7 +140,7 @@ pub async fn force_reset_password<'a>(
let persisted_user = match x_u_res_db_or_res!(get_user_with_id(conn, user_id).await) {
None => {
error!("Serious error. User doesn't exist but credentials pointing to the user do.");
return Err(Error::new(trace!()).error_type(ErrorType::Service(ServiceError::NotFoundError(format!("User with id: {user_id}")))));
return Err(Error::new(trace!()).error_type(ErrorType::Service { error: ServiceError::NotFoundError { message: format!("User with id: {user_id}")}}));
}
Some(persisted_user) => persisted_user,
};
@ -149,7 +152,7 @@ pub async fn password_login(
user: UserLoginPayload,
) -> Result<Token, Error> {
let persisted_user_credential = match x_u_res_db_or_res!(get_credential(conn, user.credential.clone()).await) {
None => return Err(Error::new(trace!()).error_type(ErrorType::Service(ServiceError::NotFoundError(format!("Credential {}", user.credential))))),
None => return Err(Error::new(trace!()).error_type(ErrorType::Service { error: ServiceError::NotFoundError { message: format!("Credential {}", user.credential)}})),
Some(persisted_credential) => persisted_credential,
};
let persisted_user_opt =
@ -157,10 +160,10 @@ pub async fn password_login(
if let Some(_) = persisted_user_opt {
Ok(u_res_or_res!(create_token_for_user(conn, persisted_user_credential.user_id).await))
} else {
Err(Error::new(trace!()).error_type(ErrorType::Service(ServiceError::NotFoundError(format!(
Err(Error::new(trace!()).error_type(ErrorType::Service { error: ServiceError::NotFoundError { message: format!(
"User with id: {}",
persisted_user_credential.user_id
)))))
)}}))
}
}
@ -185,18 +188,18 @@ async fn create_token_for_user<'a>(
auth_token: match tokens.get(0) {
None => {
error!("Tokens were not created.");
return Err(Error::new(trace!()).error_type(ErrorType::Service(ServiceError::UnexpectedError(String::from(
return Err(Error::new(trace!()).error_type(ErrorType::Service { error: ServiceError::UnexpectedError { message: String::from(
"Tokens were not created.",
)))));
)}}));
}
Some(token) => token.clone(),
},
refresh_token: match tokens.get(1) {
None => {
error!("Tokens were not created.");
return Err(Error::new(trace!()).error_type(ErrorType::Service(ServiceError::UnexpectedError(String::from(
return Err(Error::new(trace!()).error_type(ErrorType::Service { error: ServiceError::UnexpectedError { message: String::from(
"Tokens were not created.",
)))));
)}}));
}
Some(token) => token.clone(),
},
@ -216,7 +219,7 @@ async fn validate_user_password<'a>(
let persisted_user = match x_u_res_db_or_res!(get_user_with_id(conn, user_id).await) {
None => {
error!("Serious error. User doesn't exist but credentials pointing to the user do.");
return Err(Error::new(trace!()).error_type(ErrorType::Service(ServiceError::NotFoundError(format!("User with id: {user_id}")))));
return Err(Error::new(trace!()).error_type(ErrorType::Service { error: ServiceError::NotFoundError { message: format!("User with id: {user_id}")}}));
}
Some(persisted_user) => persisted_user,
};