Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Note #21

Open
AverageOrangeCat opened this issue Oct 24, 2023 · 0 comments
Open

Note #21

AverageOrangeCat opened this issue Oct 24, 2023 · 0 comments

Comments

@AverageOrangeCat
Copy link
Member

Other unique parameters

If in the future other unique parameters are added like 'phone' then you could use 'constraint' value in the PgDatabaseError to customize the error message.

Database(PgDatabaseError { severity: Error, code: "23505", message: "duplicate key value violates unique constraint \"account_email_key\"", detail: Some("Key (email)=([email protected]) already exists."), hint: None, position: None, where: None, schema: Some("public"), table: Some("account"), column: None, data_type: None, constraint: Some("account_email_key"), file: Some("nbtinsert.c"), line: Some(666), routine: Some("_bt_check_unique") })

Something like:

if let Err(error) = AccountService::update(&pool, &write_key, &dto.email, &dto.password).await {
  match error.constraint {
      Some("account_email_key") => return Ok(HttpResponse::Conflict().body("Email already registered")),
      Some("account_phone_key") => return Ok(HttpResponse::Conflict().body("Phone already registered")),
      Some(_) => return Ok(HttpResponse::Internal().body("A database request has failed"))),
      None => return Ok(HttpResponse::Internal().body("A database request has failed")))
  };
}

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant