refactor(application): use UniqueViolation in register — remove postgres constraint strings
This commit is contained in:
@@ -38,11 +38,15 @@ pub async fn register(
|
|||||||
.save(&user)
|
.save(&user)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| match e {
|
.map_err(|e| match e {
|
||||||
DomainError::Conflict(c) => match c.as_str() {
|
DomainError::UniqueViolation { field: "username" } => {
|
||||||
"users_username_key" => DomainError::Conflict("username taken".into()),
|
DomainError::Conflict("username taken".into())
|
||||||
"users_email_key" => DomainError::Conflict("email taken".into()),
|
}
|
||||||
_ => DomainError::Conflict("already exists".into()),
|
DomainError::UniqueViolation { field: "email" } => {
|
||||||
},
|
DomainError::Conflict("email taken".into())
|
||||||
|
}
|
||||||
|
DomainError::UniqueViolation { .. } => {
|
||||||
|
DomainError::Conflict("already exists".into())
|
||||||
|
}
|
||||||
other => other,
|
other => other,
|
||||||
})?;
|
})?;
|
||||||
events
|
events
|
||||||
@@ -136,7 +140,7 @@ mod tests {
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl UserWriter for ConflictOnSaveStore {
|
impl UserWriter for ConflictOnSaveStore {
|
||||||
async fn save(&self, _user: &User) -> Result<(), DomainError> {
|
async fn save(&self, _user: &User) -> Result<(), DomainError> {
|
||||||
Err(DomainError::Conflict("users_username_key".into()))
|
Err(DomainError::UniqueViolation { field: "username" })
|
||||||
}
|
}
|
||||||
async fn update_profile(
|
async fn update_profile(
|
||||||
&self,
|
&self,
|
||||||
@@ -178,7 +182,7 @@ mod tests {
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl UserWriter for EmailConflictOnSaveStore {
|
impl UserWriter for EmailConflictOnSaveStore {
|
||||||
async fn save(&self, _user: &User) -> Result<(), DomainError> {
|
async fn save(&self, _user: &User) -> Result<(), DomainError> {
|
||||||
Err(DomainError::Conflict("users_email_key".into()))
|
Err(DomainError::UniqueViolation { field: "email" })
|
||||||
}
|
}
|
||||||
async fn update_profile(
|
async fn update_profile(
|
||||||
&self,
|
&self,
|
||||||
|
|||||||
Reference in New Issue
Block a user