refactor/fix: now using pgx errors for postgres error checking instead of trying to look up the error code;

feat: implemented working custom validation;
fix: authservice begin/complete registration
This commit is contained in:
2025-07-05 03:08:00 +03:00
parent 0a51727af8
commit 8319afc7ea
9 changed files with 126 additions and 39 deletions

View File

@@ -23,8 +23,8 @@ import (
"easywish/internal/models"
"easywish/internal/utils"
"easywish/internal/utils/enums"
"errors"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5"
"go.uber.org/zap"
)
@@ -57,7 +57,7 @@ func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequ
if user, err = db.TXQueries.CreateUser(db.CTX, request.Username); err != nil {
if errs.IsPgErr(err, pgerrcode.UniqueViolation) {
if errors.Is(err, pgx.ErrNoRows) {
a.log.Warn(
"Attempted registration for a taken username",
zap.String("username", request.Username),
@@ -69,13 +69,13 @@ func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequ
return false, errs.ErrServerError
}
if _, err := db.TXQueries.GetUserByEmail(db.CTX, *request.Email); err == nil {
if _, emailerr := db.TXQueries.GetUserByEmail(db.CTX, *request.Email); emailerr == nil {
a.log.Warn(
"Attempted registration for a taken email",
zap.String("email", *request.Email))
return false, errs.ErrEmailTaken
} else if !errs.IsPgErr(err, pgerrcode.NoData) {
} else if !errors.Is(emailerr, pgx.ErrNoRows) {
a.log.Error(
"Failed to check if email is not taken",
zap.String("email", *request.Email),
@@ -139,7 +139,7 @@ func (a *authServiceImpl) RegistrationComplete(request models.RegistrationComple
user, err = db.TXQueries.GetUserByUsername(db.CTX, request.Username)
if err != nil {
if errs.IsPgErr(err, pgerrcode.NoData) {
if errors.Is(err, pgx.ErrNoRows) {
a.log.Warn(
"Could not find user attempting to complete registration with given username",
zap.String("username", request.Username),
@@ -161,7 +161,7 @@ func (a *authServiceImpl) RegistrationComplete(request models.RegistrationComple
})
if err != nil {
if errs.IsPgErr(err, pgerrcode.NoData) {
if errors.Is(err, pgx.ErrNoRows) {
a.log.Warn(
"User supplied unexistent confirmation code for completing registration",
zap.String("username", user.Username),