mod/user: rewrite some if statements as switches
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
leo 2023-05-03 22:42:08 +02:00
parent 0125bff8bd
commit 4f2302143d
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ

@ -27,15 +27,13 @@ func CreateUser(ctx context.Context, client *ent.Client, email, username, passwo
SetUsername(username).
SetPassword(digest).
Save(ctx)
if err != nil {
log.Infof("error querying user: %q", err)
if ent.IsConstraintError(err) {
log.Errorf("the username '%s' already exists", username)
return nil, errors.New("username is not unique")
}
switch {
case ent.IsConstraintError(err):
log.Errorf("the username '%s' already exists", username)
return nil, errors.New("username is not unique")
case err != nil:
return nil, fmt.Errorf("failed creating user: %w", err)
}
@ -58,13 +56,13 @@ func QueryUser(ctx context.Context, client *ent.Client, username string) (*ent.U
// `Only` fails if no user found,
// or more than 1 user returned.
Only(ctx)
if err != nil {
switch {
case ent.IsNotFound(err):
return nil, fmt.Errorf("user not found: %q", err)
case err != nil:
log.Warn("error querying user", "error", err, "username requested", username)
if ent.IsNotFound(err) {
return nil, fmt.Errorf("user not found: %q", err)
}
return nil, fmt.Errorf("failed querying user: %w", err)
}
@ -117,16 +115,18 @@ func UsernameExists(ctx context.Context, client *ent.Client, username string) (b
Query().
Where(user.Username(username)).
Only(ctx)
if err != nil {
if ent.IsNotFound(err) {
return false, nil
} else if ent.IsNotSingular(err) {
log.Errorf("apparently more than one user managed to acquire the username '%s', bailing", username)
return true, err
}
switch {
case ent.IsNotFound(err):
log.Infof("username '%s' not found", username)
return false, nil
case ent.IsNotSingular(err):
log.Errorf("apparently more than one user managed to acquire the username '%s', bailing", username)
return true, err
case err != nil:
log.Warn("failed to check whether user exists", "error", err, "username queried", username)
return false, fmt.Errorf("failed querying username: %w", err)
}
@ -135,7 +135,7 @@ func UsernameExists(ctx context.Context, client *ent.Client, username string) (b
return true, nil
}
log.Infof("username '%s' not found", username)
log.Warn("we should not have gotten here, apparently error was nil but so was usr...")
return false, nil
}
@ -150,16 +150,18 @@ func EmailExists(ctx context.Context, client *ent.Client, email string) (bool, e
Query().
Where(user.Email(email)).
Only(ctx)
if err != nil {
if ent.IsNotFound(err) {
return false, nil
} else if ent.IsNotSingular(err) {
log.Errorf("apparently more than one user managed to register using the email '%s', bailing", email)
return true, err
}
switch {
case ent.IsNotFound(err):
log.Infof("user email '%s' not found", email)
return false, nil
case ent.IsNotSingular(err):
log.Errorf("apparently more than one user managed to register using the email '%s', bailing", email)
return true, err
case err != nil:
log.Warn("failed to check whether email exists", "error", err, "email queried", email)
return false, fmt.Errorf("failed querying user email: %w", err)
}
@ -168,7 +170,7 @@ func EmailExists(ctx context.Context, client *ent.Client, email string) (bool, e
return true, nil
}
log.Infof("user email '%s' not found", email)
log.Warn("we should not have gotten here, apparently error was nil but so was usr...")
return false, nil
}